8 Greedy Algorithms
8 Greedy Algorithms
Greedy Algorithms
Atul Gupta
A Greedy Algorithm
An algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum In some problem, it finds an optimal(best) solution. While in some other (Hard) problems, it may yield a locally optimal solution that approximate the global optimal solution in a reasonable time For example, a greedy strategy for the traveling salesman problem
Making Change
determines the minimum number of coins to give while making change of 36 cents using only coins with values {1, 5, 10, 20}.
Making Change
in general the change-making problem requires dynamic programming or integer programming to find an optimal solution (For instance try the same with {10,9,2,1} However, most currency systems, including the Euro (pictured) and US Dollar, are special cases where the greedy strategy does find an optimum solution.)
Greedy algorithms
Greedy algorithms can be characterized as being 'short sighted', and as 'non-recoverable' A greedy algorithm never reconsiders its choices Nevertheless, they are useful because they are quick to think up and often give good approximations to the optimum
Greedy Algorithm
Solve optimally
finding minimum spanning trees (Kruskal's algorithm and Prim's algorithm ) finding single-source shortest paths (Dijkstra's algorithm) finding optimum codes (Huffman trees) Fractional Knapsac problem The activity selection problem
If A is an optimal solution to the original problem S, then A` = A - {1} is an optimal solution to the activity-selection problem S` = {i in S: s[i] >= f[1]}
Huffman Coding
Huffman coding is an encoding algorithm used for lossless data compression The technique works by creating a binary tree of nodes.
Huffman tree generated from the exact frequencies of the text "this is an example of a huffman tree".