Coding · Heap (priority queue)
Always take the best available
How a heap turns a greedy choice into a fast loop, why picking the cheapest pair of ropes is safe, and what to do when options only open up as you go.
Why the cheapest pair first is safe
Suppose you're joining ropes and every join costs the combined length of the two pieces. I'd want you to notice that a rope's length gets paid again every time the piece it belongs to is joined, so a short rope that ends up deep inside the final rope is paid for several times over, and that's fine, whereas a long one paid three times is expensive. That's why you'd join the two shortest pieces first and put the result back, and it happens to be the same reasoning Huffman used for his codes.
With ropes of 1, 8, 3 and 5 the heap gives you 1 and 3 (cost 4), then 4 and 5 (cost 9), and finally 8 and 9 (cost 17), so the total is 30. If you'd joined 1 and 8 first and then 3 and 5 you'd pay 34, and joining them in the order they're written costs even more. You can check the 30 another way, since the 1 and the 3 are each paid three times, the 5 twice and the 8 once, which gives 3 + 9 + 10 + 8 = 30.
Join the cheapest pair
joinAll(ropes)
- I'd put every rope into a min-heap and keep a running total that starts at 0.
- While there's more than one piece left, you pop the two shortest and add their sum to the total.
- The joined piece then goes back into the heap, because it might well be among the shortest next time.
- When a single rope is left, the running total is the cheapest possible cost.
Each join is two pops and one push, and there are n − 1 joins, so you're doing about 3n heap operations at log n apiece.
Here are the ropes 1, 8, 3, 5 being joined two at a time, cheapest pair first, until the total reaches 30.
-
Step 1 / 4heap (sorted view)10315283cost0
Put all the ropes in a min-heap. The two shortest are always at hand.
-
Step 2 / 4heap (sorted view)405182cost4
Take the two shortest, 1 and 3. Joining them costs 4; the total is now 4. The new rope of 4 goes back into the heap.
-
Step 3 / 4heap (sorted view)8091cost13
Take the two shortest, 4 and 5. Joining them costs 9; the total is now 13. The new rope of 9 goes back into the heap.
-
Step 4 / 4heap (sorted view)170cost30
Take the two shortest, 8 and 9. Joining them costs 17; the total is now 30. The new rope of 17 goes back into the heap. One rope is left, so the minimum total is 30.
When the options open up as you go
In some questions you can't put everything into the heap at the start, because an option only becomes available once you've reached some threshold. The project question is a good example of this. You've got some capital, each project needs a minimum amount before you're allowed to start it, and finishing it adds its profit (the requirement isn't spent). What I'd do is sort the projects by their requirement and keep a pointer into that list, and then in every round you'd move the pointer forward, push the profit of each project you can now afford into a max-heap, and take the biggest profit available.
The greedy choice is safe for a similar reason to the ropes. Taking the biggest affordable profit can only raise your capital more than any other choice would, so it makes at least as many projects affordable for the next round. If the heap's empty in some round then nothing's affordable and your capital can't grow any further, so I'd stop there even when you've still got rounds left.
I've seen a lot of people subtract the requirement from their capital when they start a project. The statement treats it as a threshold you have to meet, so your capital only ever goes up.
Starting with capital 2 and 3 rounds, you'll see the profits 4, 3 and 5 taken in turn, which leaves you with 14.
-
Step 1 / 3projects by capital (need→profit)0→102→413→326→139→54profit heap (after pop)10round1capital6
Round 1: newly affordable: 0→1, 2→4. The heap offers profits 4, 1; take the largest, 4. Capital is now 6.
-
Step 2 / 3projects by capital (need→profit)0→102→413→326→139→54profit heap (after pop)1011round2capital9
Round 2: newly affordable: 3→3, 6→1. The heap offers profits 3, 1, 1; take the largest, 3. Capital is now 9.
-
Step 3 / 3projects by capital (need→profit)0→102→413→326→139→54profit heap (after pop)1011round3capital14
Round 3: newly affordable: 9→5. The heap offers profits 5, 1, 1; take the largest, 5. Capital is now 14. 3 projects done: the final capital is 14.