For the complete documentation index, see llms.txt. This page is also available as Markdown.

15. Greedy Algorithms

Exercises

15.1-1

Available in the latest revision of the IM.

15.1-2

Available in the latest revision of the IM.

Another, perhaps even clearer, approach is to tweak Theorem 15.1 to read as: "Consider any nonempty subproblem SkS_k, and let ama_m be an activity in SkS_k with the latest start time. Then ama_m is included in some maximum-size subset of mutually compatible activities of SkS_k."

The proof is virtually the same as for the original version from the book. This theorem ensures that making a greedy choice is a safe move.

15.1-3

Available in the latest revision of the IM.

15.1-4

Available in the latest revision of the IM.

15.1-5

Available in the latest revision of the IM.

15.2-1

Available in the latest revision of the IM.

15.2-2

Available in the latest revision of the IM.

15.2-3

In this variant, the items can be sorted such that as weight increases, value decreases.This means item 1 is the lightest item in the entire set, and it is the most valuable. Item 2 is the second lightest, and the second most valuable. There is zero trade-off to evaluate. If we have to choose between item ii and item jj (where i<ji < j), item ii is strictly superior—it has less (or equal) weight and gives more (or equal) value.

The running time of the core part (without sorting in line 1) is Θ(n)\Theta(n). The extra space complexity is Θ(1)\Theta(1) (not counting the set SS returned by the program).

15.2-4

Available in the latest revision of the IM.

15.2-5

Available in the latest revision of the IM.

★ 15.2-6

Available in the latest revision of the IM.

15.2-7

Available in the latest revision of the IM.

15.3-1

Available in the latest revision of the IM.

15.3-2

Available in the latest revision of the IM.

15.3-3

Available in the latest revision of the IM.

15.3-4

Available in the latest revision of the IM.

15.3-5 🌟

Available in the latest revision of the IM.

15.3-6

Available in the latest revision of the IM.

15.3-7

Available in the latest revision of the IM.

15.3-8

Available in the latest revision of the IM.

Lossless compression is fundamentally about maintaining a one-to-one correspondence between distinct input files and distinct output files. Consider all possible input files of exactly nn bits. Because each bit can be a 0 or 1, there are exactly 2n2^n distinct input files. Suppose a compression scheme guarantees every file is strictly shortened. The total number of possible output files across all these shorter lengths is

k=0n12k=20+21++2n1=2n1.\sum_{k=0}^{n-1} 2^k = 2^0 + 2^1 + \dots + 2^{n-1} = 2^n - 1.

By the pigeonhole principle, the scheme cannot be perfectly invertible (lossless).

15.4-1

Lines 9-19 find the block for eviction using the furthest-in-future strategy. The running time of this block is O(k(ni))O(k(n - i)), but the per-eviction cost can be significantly reduced by globally preprocessing the sequence in Θ(n)\Theta(n) time to store future indices for each block type. This can be saved inside a hash table. Furthermore, the cache configuration may be extended with a max-heap to efficiently find the next block to evict.

15.4-2

Available in the latest revision of the IM.

15.4-3

Available in the latest revision of the IM.

15.4-4

Available in the latest revision of the IM.

Problems

15-1 Coin changing

Available in the latest revision of the IM.

15-2 Scheduling to minimize average completion time

Available in the latest revision of the IM.

Last updated