Explain the concept of Greedy Algorithms and provide an example where it is used.
Instruction: Discuss the principle behind greedy algorithms and illustrate with a specific example of its application.
Updated
Official answer
A greedy algorithm commits to a locally preferred choice without exhaustively comparing every future combination. Correctness requires a reason those choices preserve an optimal solution, often an exchange argument; local improvement alone is insufficient.
Huffman coding repeatedly merges the two smallest symbol/subtree weights. With frequencies 5, 7, 10 and 15, the merges have weights 12, 22 and 37. The weighted code length is their sum, 71 bits for 37 symbols under that frequency model, before storing the codebook or other metadata. A min-heap gives O(k log k) construction for k symbols.
Huffman produces an optimal binary prefix code for the supplied symbol frequencies. This is not a claim of best compression across every possible coding method, sequence model or file including overhead. Tied weights can yield different equally optimal trees.
For a greedy failure, making change for 6 with coins 1, 3 and 4 by always choosing the largest gives 4+1+1, while 3+3 uses fewer coins. Explain both the algorithm's local rule and the property that makes it correct for the particular problem, rather than assuming greedy choices always compose into a global optimum.