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 the 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 the 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.
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 the smallest two ropes first and recur for remaining ropes. This approach is similar to Huffman Coding. We put the smallest ropes down the tree so that they can be repeated multiple times rather than the longer ropes.
So it forms a structure like a tree:
The sum contains sum of depth of each value. For array (2, 3, 4, 6) the sum is equal to 2 * 3 + 3 * 3 + 4 * 2 + 6 * 1 = 29 (According to the diagram).
- Create a min-heap and insert all lengths into the min-heap.
- Do following while the number of elements in min-heap is not one.
- Extract the minimum and second minimum from min-heap
- Add the above two extracted values and insert the added value to the min-heap.
- Maintain a variable for total cost and keep incrementing it by the sum of extracted values.
- Return the value of this total cost.
Following is the implementation of the above algorithm.
Total cost for connecting ropes is 29
- Time Complexity: O(nLogn), assuming that we use a O(nLogn) sorting algorithm. Note that heap operations like insert and extract take O(Logn) time.
- Auxiliary Complexity:O(n), The space required to store the values in min heap
Algorithmic Paradigm: Greedy Algorithm
A simple implementation with STL in C++
This uses priority_queue available in STL. Thanks to Pango89 for providing the below code. The approach and algorithm remains the same. The min Heap is replaced by a priority Queue.
Total cost for connecting ropes is 29
- Time Complexity: O(nLogn), assuming that we use a O(nLogn) sorting algorithm.
Note that heap operations like insert and extract take O(Logn) time.
- Auxiliary Complexity: O(n).
The space required to store the values in min heap
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum cost to connect all cities
- Minimum cost required to connect all houses in a city
- Minimum Cost using Dijkstra by reducing cost of an Edge
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Connect nodes at same level
- Connect nodes at same level using constant extra space
- Non-crossing lines to connect points in a circle
- Connect Nodes at same Level (Level Order Traversal)
- Minimum Cost to cut a board into squares
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Minimum cost for acquiring all coins with k extra coins allowed with every coin
- Minimum cost to make array size 1 by removing larger of pairs
- Minimum total cost incurred to reach the last station
- Minimum cost to process m tasks where switching costs
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimum cost to convert str1 to str2 with the given operations
- Minimum possible travel cost among N cities
- Minimum cost to traverse from one index to another in the String
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Minimum cost of reducing Array by merging any adjacent elements repetitively