Worked Examples: Greedy Algorithms

These examples are designed to step through the logical process of applying the algorithmic concepts.

Example 1: Activity Selection
❓ Problem: Why earliest finish?
💡 Solution: Leaves maximal time for future resources.
Example 2: Counter-Example
❓ Problem: Shortest duration first?
💡 Solution: Fails. [1-5], [4-7], [6-10]. Shortest [4-7] blocks both.
Example 3: Fractional Knapsack
❓ Problem: Strategy?
💡 Solution: Take item with max Value/Weight ratio.
Example 4: Huffman Optimality
❓ Problem: Prefix property solution.
💡 Solution: No code is prefix of another. Unambiguous.
Example 5: Huffman Cost
❓ Problem: Cost of tree?
💡 Solution: $\sum f_i \cdot depth_i$.
Example 6: Complexity
❓ Problem: Activity Select with Sort.
💡 Solution: $O(n \log n)$.