For many problems, making greedy choices leads to an optimal solution. These algorithms are applicable to optimization problems.
In a greedy algorithm, in each step, we will make a locally optimum solution such that it will lead to a globally optimal solution. Once a choice is made, we cannot retract it in later stages.
Proving the correctness of a greedy algorithm is very important, since not all greedy algorithms lead to globally optimum solution.
For ex- consider the problem where you are given coins of certain denomination and asked to construct certain amount of money in inimum number of coins.
Let the coins be of 1, 5, 10, 20 cents
If we want change for 36 cents, we select the largest possible coin first (greedy choice).
According to this process, we select the coins as follows-
20
20 + 10
20 + 10 + 5
20 + 10 + 5 + 1 = 36.
For coins of given denomination, the greedy algorithm always works.
But in general this is not true.
Consider the denomination as 1, 3, 4 cents
To make 6 cents, according to greedy algorithm the selected coins are 4 + 1 + 1
But, the minimum coins needed are only 2 (3 + 3)
Hence, greedy algorithm is not the correct approach to solve the 'change making' problem.
Infact, we can use dynamic programming to arrive at optimal solution to this problem.
Read more at http://gonitsora.com/algorithm-types-and-classification/#TdZOUE8KelEImvTA.99