There are given n ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. We need to connect the ropes with minimum cost.
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38.
We strongly recommend that you click here and practice it, before moving on to the solution.
If we observe the above problem closely, we can notice that the lengths of the ropes which are picked first are included more than once in total cost. Therefore, the idea is to connect smallest two ropes first and recur for remaining ropes. This approach is similar to Huffman Coding. We put smallest ropes down the tree so that they can be repeated multiple times rather than the longer ropes.
Following is complete algorithm for finding the minimum cost for connecting n ropes.
Let there be n ropes of lengths stored in an array len[0..n-1]
1) Create a min heap and insert all lengths into the min heap.
2) Do following while number of elements in min heap is not one.
……a) Extract the minimum and second minimum from min heap
……b) Add the above two extracted values and insert the added value to the min-heap.
……c) Maintain a variable for total cost and keep incrementing it by the sum of extracted values.
3) Return the value of this total cost.
Following is the implementation of above algorithm.
Total cost for connecting ropes is 29
Time Complexity: Time complexity of the algorithm is O(nLogn) assuming that we use a O(nLogn) sorting algorithm. Note that heap operations like insert and extract take O(Logn) time.
Algorithmic Paradigm: Greedy Algorithm
A simple implementation with STL in C++
Following is a simple implementation that uses priority_queue available in STL. Thanks to Pango89 for providing below code.
Total cost for connecting ropes is 29
This article is compiled by Abhishek. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum cost to connect all cities
- Minimum Cost to cut a board into squares
- Minimum possible travel cost among N cities
- Minimum cost to merge numbers from 1 to N
- Minimum cost to traverse from one index to another in the String
- Minimum cost to process m tasks where switching costs
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimum total cost incurred to reach the last station
- Minimum cost to convert str1 to str2 with the given operations
- Minimum cost to make an Array a permutation of first N natural numbers
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Minimum cost of reducing Array by merging any adjacent elements repetitively
- Minimum cost to make array size 1 by removing larger of pairs
- Minimum cost for acquiring all coins with k extra coins allowed with every coin
- Connect nodes at same level
- Non-crossing lines to connect points in a circle
- Connect nodes at same level using constant extra space
- Connect Nodes at same Level (Level Order Traversal)
- Min Cost Path | DP-6