Greedy Algorithms
Greedy Algorithms
Greedy Algorithms
Similar to dynamic programming, but simpler approach
Also used for optimization problems
Idea: When we have a choice to make, make the one
that looks best right now
Make a locally optimal choice in hope of getting a globally optimal
solution
Makes the choice that looks best at the moment in order
to get optimal solution.
Applications of Greedy
Algorithm
It is used in finding the shortest path.
It is used to find the minimum spanning tree using the
prim's algorithm or the Kruskal's algorithm.
It is used in a job sequencing with a deadline.
This algorithm is also used to solve the fractional
knapsack problem.
3
Fractional Knapsack Problem
Knapsack capacity: W
There are n items: the i-th item has value vi and weight
wi
Goal:
find xi such that for all 0 xi 1, i = 1, 2, .., n
wixi W and
xivi is maximum
Fractional Knapsack -
ExampleM=240
E.g.: 20
---
$80
Item 3 30 +
Item 2 50 50
20 $100
Item 1 30
20 +
10 10 $60
5. w w – xiwi