Giter VIP home page Giter VIP logo

goit-algo-hw-08's Introduction

GoITNeo Algo HW-8

Task

There are several network cables of different lengths that need to be joined in pairs into one cable using connectors in a way that minimizes costs. The cost of connecting two cables is equal to their combined length, and the total cost is the sum of connecting all cables. The task is to find the order of joining that minimizes the total cost.

Solution

To minimize the cost we have to connect cables from the shortest pairs to the end. Do it iteratively and sum the cost of each connection. The easiest way to do it is to use a minimal heap.

So, we have such a short piece of code:

import heapq
from numpy import random

cables = list(random.randint(100, size=random.randint(64)))
heapq.heapify(cables)
cost = 0

print(f"All cables ({len(cables)} parts): {cables} ")
print(f"We expect to get a cable length: {sum(cables)}")

for _ in range(len(cables)-1):
    new_cable = heapq.heappop(cables) + heapq.heappop(cables)
    heapq.heappush(cables, new_cable)
    cost += new_cable

print(f"We got a cable length: {cables[0]}")
print(f"Total cost: {cost}")

And the output:

All cables (18 parts): [4, 4, 13, 30, 38, 47, 51, 55, 56, 87, 52, 54, 68, 92, 95, 57, 55, 75]
We expect to get a cable with length: 933
We got a cable with length: 933
Total cost: 3710

Conclusions

A heap structure is a very convenient method to make a priority queue. Module heapq provides functionality for a minimum-heap, but converting it to maximum-heap by inverting values is very easy.

goit-algo-hw-08's People

Contributors

maximus-ms avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.