Greedy Algorithm_Lab Manual
Greedy Algorithm_Lab Manual
Greedy Algorithms
Pseudocode
2. Knapsack Problem
Pseudocode
3. Counting Money
Suppose you want to count out a certain amount of money, using the fewest possible bills and
coins
Greedy algorithm to do this would be:
At each step, take the largest possible bill or coin that does not overshoot
Example: To make $6.39, you can choose:
• a $5 bill
• a $1 bill, to make $6
• 25¢ coin, to make $6.25
• A 10¢ coin, to make $6.35
• four 1¢ coins, to make $6.39
For US money, the greedy algorithm always gives the optimum solution
There are given n ropes of different lengths, we need to connect these ropes into one rope. The
cost to connect two ropes is equal to sum of their lengths.
We need to connect the ropes with minimum cost.