Abstract
It’s not a question of enough, pal.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
No, it’s not to run away and buy comic books.
- 2.
The idea for this version of the problem comes from Michael Soltys (see references in Chapter 4).
- 3.
To be on the safe side, just let me emphasize that this greedy solution would not work in general, with an arbitrary set of weights. The distinct powers of two are key here.
- 4.
If we view each object individually, this is often called 0-1 knapsack because we can take 0 or 1 of each object.
- 5.
Not only is it unimportant whether zero means left or right, it is also unimportant which subtrees are on the left and which are on the right. Shuffling them won’t matter to the optimality of the solution.
- 6.
If a future version of the heapq library lets you use a key function, such as in list.sort, you’d no longer need this tuple wrapping at all, of course.
- 7.
They might also have equal weights/frequencies; that doesn’t affect the argument.
- 8.
By the way, did you know that the ZIP code of Huffman, Texas, is 77336?
- 9.
You can, in fact, combine Borůvka’s algorithm with Prim’s to get a faster algorithm.
- 10.
Do you see why the result cannot contain any cycles, as long as we assume positive edge weights?
- 11.
Going back and forth between this representation and one where you have edges both ways isn’t really hard, but I’ll leave the details as an exercise for the reader.
- 12.
We’re sorting m edges, but we also know that m is O(n 2), and (because the graph is connected), m is Ω(n). Because Θ(lg n 2) = Θ(2.lg n) = Θ(lg n), we get the result.
- 13.
Actually, the difference is deceptive. Prim’s algorithm is based on traversal and heaps—concepts we’ve already dealt with—while Kruskal’s algorithm introduced a new disjoint set mechanism. In other words, the difference in simplicity is mostly a matter of perspective and abstraction.
- 14.
As I mentioned when discussing Kruskal’s algorithm, adding and removing such redundant reverse edges is quite easy, if you need to do so.
- 15.
Because the intervals don’t overlap, sorting by starting and finishing times is equivalent.
- 16.
Versions of this problem can be found in Soltys’ book (see “References” in Chapter 4) and that of Cormen et al. (see “References” in Chapter 1). My proof closely follows Soltys’s, while Cormen et al. choose to prove that the problem forms a matroid, which means that a greedy algorithm will work on it.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2014 Magnus Lie Hetland
About this chapter
Cite this chapter
Hetland, M.L. (2014). Greed Is Good? Prove It!. In: Python Algorithms. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-0055-1_7
Download citation
DOI: https://doi.org/10.1007/978-1-4842-0055-1_7
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-0056-8
Online ISBN: 978-1-4842-0055-1
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)