Skip to main content

Greed Is Good? Prove It!

  • Chapter
  • First Online:
Python Algorithms

Abstract

It’s not a question of enough, pal.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 79.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 99.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    No, it’s not to run away and buy comic books.

  2. 2.

    The idea for this version of the problem comes from Michael Soltys (see references in Chapter 4).

  3. 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. 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. 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. 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. 7.

    They might also have equal weights/frequencies; that doesn’t affect the argument.

  8. 8.

    By the way, did you know that the ZIP code of Huffman, Texas, is 77336?

  9. 9.

    You can, in fact, combine Borůvka’s algorithm with Prim’s to get a faster algorithm.

  10. 10.

    Do you see why the result cannot contain any cycles, as long as we assume positive edge weights?

  11. 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. 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. 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. 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. 15.

    Because the intervals don’t overlap, sorting by starting and finishing times is equivalent.

  16. 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

Authors

Rights and permissions

Reprints 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

Publish with us

Policies and ethics