Unit III Greedy
Unit III Greedy
Greedy Method
Greedy Method
The greedy method is the straight forward design technique applicable to variety of
applications.
The greedy approach suggests constructing a solution through a sequence of steps, each
expanding a partially constructed solution obtained so far, until a complete solution to the
problem is reached. On each step the choice made must be:
feasible, i.e., it has to satisfy the problem’s constraints
locally optimal, i.e., it has to be the best local choice among all feasible choices
available on that step
irrevocable, i.e., once made, it cannot be changed on subsequent steps of the
algorithm
As a rule, greedy algorithms are both intuitively appealing and simple.
Given an optimization problem, it is usually easy to figure out how to proceed in a
greedy manner, possibly after considering a few small instances of the problem. What is
usually more difficult is to prove that a greedy algorithm yields an optimal solution
(when it does).
Greedy General Method
Algorithm Greedy (a, n)
// a [1 ..n] contains the n inputs
{
solution :=ø;
for i := 1 to n do
{
x := Select (a);
if Feasible (solution, x) then solution := Union ( solution, x);
}
return solution;
}
Greedy General Method
Greedy method consists of 3 functions (steps).
1) Select: it selects an input from array a[] (candidate set) and puts in the variable x.
2) Feasible: it is a Boolean function which checks whether the selected input meets the
constraints or not.
3) Union: if the selected input i.e. 'x' makes the solution feasible, then x is included in the
solution and objective function get updated.
Characteristics of Greedy:
1) These algorithms are simple and straightforward and easy to implement.
2) They take decisions on the basis of information at hand without worrying about the
effect these decisions may have in the future.
3) They work in stages and never reconsider any decision.
Greedy General Method
Applications
Change making problem
Minimum Spanning Tree
Single source shortest path
Knapsack problem
Job Sequencing with deadlines
Optimal storage on tapes
Huffman codes
Change making problem
• Suppose, we want to make change
for an amount ‘A’ using fewest no
of currency notes. Assume the
available denominations are
Rs 1, 2, 5, 10, 20, 50, 100, 500, 1000.
• To make a change for A=Rs 28,
with the minimum number of notes,
one would first choose a note of
denomination Rs 20, 5, 2 and 1.
Change making problem
• In Greedy method the problems have 'n' inputs called as candidate set, from which a
subset is selected to form a solution for the given problem. Any subset that satisfies the
given constraints is called a feasible solution. We need to find a feasible solution that
maximizes or minimizes an objective function and such solution is called an optimal
solution.
• currency notes denomination set { 1000 ….1000 ,500….500, 100….100, 50…50, 20…
20,10…10,5…5,2..2,1…1}is candidate set.
• constraint is our solution make the exact target amount of cash. Hence, any feasible
solution i.e. sum of selected notes should be equal to target amount.
• objective function is our solution should consist of the fewest number of currency notes.
Hence, any optimal solution which is one of the feasible solutions that optimizes the
objective function. There can be more than one optimal solution.
Minimum Spanning Trees
Spanning Trees
• Given (connected) graph G(V,E),
a spanning tree T(V’,E’):
• Is a subgraph of G; that is, V’ V, E’ E.
• Spans the graph (V’ = V)
• Forms a tree (no cycle);
• So, E’ has |V| -1 edges
b 7 c
5 Acyclic subset of edges(E) that connects
a 1
all vertices of G.
3 -3
11
d e f
0 2 b c
5
a 1
3 -3
d e f
0
1 1 6
4
2
6 5
1 1 6
4
2
6 5
8 3
Final Cost: 1 + 3 + 4 + 1 + 1 = 10 2 3 4
1 1 6
4
2
6 5
1
10 5
1 1
8 3
2 3 4
1 1 6
4
2
6 5
8 3
2 3 4
1 1 6
4
2
6 5
6 5
1
Select edge with lowest cost
(2,5)
Find(2) = 2, Find (5) = 5
Union(2,5)
F= {{1},{2,5},{3},{4},{6}} 2 3 4
1 edge accepted
1
6 5
1
Select edge with lowest cost
(2,6)
Find(2) = 2, Find (6) = 6
Union(2,6)
F= {{1},{2,5,6},{3},{4}} 2 3 4
2 edges accepted
1
1
6 5
1
Select edge with lowest cost
(1,3)
Find(1) = 1, Find (3) = 3 1
Union(1,3)
F= {{1,3},{2,5,6},{4}} 2 3 4
3 edges accepted
1
1
6 5
1
Select edge with lowest cost
(5,6)
Find(5) = 2, Find (6) = 2 1
Do nothing
F= {{1,3},{2,5,6},{4}} 2 3 4
3 edges accepted
1
1
6 5
6 5
2- Min weight
X X2 X3 X4 𝟒 𝟒
1 ∑ 𝒘 𝒊 𝒙 𝒊 ≤𝟏𝟎 ∑ 𝒗 𝒊 𝒙𝒊
𝒊=𝟏 𝒊=𝟏
1 0 0 1 7+3 10 42+12=54
≤
Greedy Algorithms : Knapsack
capacity value weight item
42 7 1
2- Min weight 12 3 2
value weight item
40 4 3
W = 10
42 7 1 25 5 4
25 5 2
40 4 3
12 3 4
X1 X2 X3 X4 𝟒 𝟒
∑ 𝒘 𝒊 𝒙 𝒊 ≤𝟏𝟎 ∑ 𝒗 𝒊 𝒙𝒊
𝒊=𝟏 𝒊=𝟏
1 0 0 1 7+3 10 42+12=54
Greedy Algorithms : Knapsack
capacity
3- Ratio between the values and weights
value
(vweight
i / wi )
item
10
W = 10
40 4 1 12 3 2
40 4 3
6 42 7 2
25 5 4
5 25 5 3
4 12 3 4
X1 X2 X3 X4 𝟒 𝟒
∑ 𝒘 𝒊 𝒙 𝒊 ≤𝟏𝟎 ∑ 𝒗 𝒊 𝒙𝒊
𝒊=𝟏 𝒊=𝟏
Example:
The Fractional Knapsack Problem
• Given: A set S of n items, with each item i having
• Vi - a positive benefit (value)
• wi - a positive weight
• Goal: Choose items with maximum total benefit (value) but with
weight at most W.
• If we are allowed to take fractional amounts, then this is the
fractional knapsack problem.
• In this case, we let xi denote the amount we take of item i
• Constraint:
∑ 𝑥 𝑖 ≤𝑊 , 0 ≤ 𝑥 𝑖 ≤ 𝑤𝑖
𝑖∈𝑆
5-1-53
The Fractional Knapsack Algorithm
• Greedy choice: Keep taking item with highest value (benefit to
weight ratio) ∑ 𝑉 𝑖 ( 𝑥 𝑖 / 𝑤𝑖 )=∑ (𝑉 𝑖 / 𝑤𝑖 ) 𝑥 𝑖
𝑖∈𝑆 𝑖∈𝑆
• Since
Algorithm fractionalKnapsack(S, W)
Input: set S of items w/ benefit vi and weight wi; max. weight W
Output: amount xi of each item i to maximize benefit w/ weight at most W
5-1-55
Fractional Knapsack Example
Number of objects (n): 7 Knapsack capacity (W): 15
Object X1 X2 X3 X4 X5 X6 X7
Weight 2 3 5 7 1 4 1
Profit 10 5 15 7 6 18 3
Object X1 X2 X3 X4 Object X1 X2 X3 X4 x5
Weight 10 20 30 5 Weight 3 4 5 6 2
Profit 60 100 120 40
Profit 25 40 50 35 20