Professional Documents
Culture Documents
Greedy Method
Greedy Method
, CSE, GNITS
GREEDY METHOD
UNIT-2
GREEDY METHOD
It is the most straight forward design technique, and it can be
applied to a wide variety of problems.
These problems have ‘n’ inputs and require us to obtain a
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
subset that satisfies some constraints.
Any subset that satisfies these constraints is called a Feasible
solution.
We need to find a feasible solution that either maximizes or
minimizes a given objective function.
A feasible solution that does this is called an Optimal
Solution.
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
GREEDY METHOD
GREEDY METHOD
The Greedy method suggests that one can devise an algorithm
that works in stages, considering one input at a time.
At each stage, a decision is made regarding whether a
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
particular input is in an optimal solution.
This is done by considering the inputs in an order determined
by some selection procedure.
If the inclusion of the next input into the partially constructed
optimal solution will result in an infeasible solution, then this
input is not added to the partial solution. Otherwise, it is added.
The selection procedure itself is based on some optimization
measure. This measure may be the objective function.
GREEDY METHOD
Several different optimization measures may be possible for a
given problem. Most of them, however, will result in
algorithms that generate suboptimal solution.
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
This version of the greedy technique is called the Subset
Paradigm.
We can describe the subset paradigm by considering the
control abstraction.
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
CONTROL ABSTRACTION OF GREEDY METHOD
KNAPSACK PROBLEM
The greedy method is applied to solve the knapsack problem.
We are given ‘n’ objects and a knapsack or bag.
Object ‘i’ has a weight ‘wi’ and the knapsack has a capacity
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
‘m’.
If a fraction xi , 0 x 1, of object ‘i’ is placed into a
i
knapsack, then a profit of ‘PiXi’ is earned.
The objective is to obtain a filling of the knapsack that
maximizes the total profit earned.
Since the knapsack capacity is m, we require the total weight
of all chosen objects to be atmost ‘m’.
THE KNAPSACK PROBLEM
Formally, the problem can be stated as:
‘n’ objects, each with a weight wi > 0
a profit pi > 0
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
capacity of knapsack: M
Maximize pi xi - 1
1 i n
Subject to w i xi M - 2
1 i n
and 0 xi 1, 1 i n - 3
The profit and weights are positive numbers.
A Feasible solution is any set (x 1,x2,…,xn) satisfying Eq’s 2 and 3.
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
The four feasible solutions are:
(X1,X2,X3) Σ Xi Wi Σ P i Xi
Solution -3 yields the maximum profit. This solution is optimal for the
given problem instance.
THE KNAPSACK PROBLEM
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
is few as the knapsack capacity is used up at a rapid rate.
So let us try with to be greedy with capacity and use it up as slowly as
possible. This requires us to consider the objects in order of increasing
weights Wi.
This too is suboptimal. This time, even though capacity is used slowly,
profits aren’t coming in rapidly enough.
o Our next attempt is an algorithm that strives to achieve a balance between
the rate at which profit increases and the rate at which capacity is used. This
means that the objects are considered in decreasing order of the ratio Pi/Wi.
This results in an optimal value.
EX:1 Let n=3, m=20, (p1,p2,p3) = (25,24,15) ,
(w1,w2,w3)= (18,15,10)
Case 1: Order objects in increasing order of their weights
Increasing order of 10 15 18
Weights ‘W’
Objects 3 2 1
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Profit 15 24 25
Capacity=20
Remaining Object Weight Fraction of Object Selected
Capacity Selected
Initial=20 3 10 1
20-10 = 10 2 15 Remaining capacity/Weight =
10/15 = 2/3
0 - -
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Objects 1 2 3
Capacity=20
Remaining Object Weight Fraction of Object Selected
Capacity Selected
Initial=20 1 18 1
20-18 = 2 2 15 Remaining capacity/Weight =
2/15
0 - - -
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Pi / Wi 25/18 = 1.3 24/15 = 1.6 15/10 = 1.5 Weight 15 10 18
Capacity=20
Increasing order 10 15 20 25
of Weights ‘W’
Objects 4 2 1 3
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Capacity=40 Profit 5 30 20 40
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Objects 25 15 20 10
Capacity=20
Remaining Object Weight Fraction of Object Selected
Capacity Selected
Initial=40 3 25 1
40-25 = 15 2 15 1
0 - - -
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Object 2 3 1 4
Pi / Wi 25/20 30/15 40/25 5/10 Weight 15 25 20 10
Capacity=20= 1 =2 = 1.6 = 0.5
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Case 2: Order objects in decreasing order of their profit. Pi=76.6
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Case 2: Order objects in decreasing order of their profit. Pi= 47
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
U:=m;
for i:=1 to n do // m is the knapsack size and
// x[1..n] is the solution vector
{
if (w[i] > U) then break;
x[i]:=1.0;
U:=U-w[i];
}
if (i<=n) then
x[i]:=U/w[i] ;
}
JOB SEQUENCING WITH DEADLINES
Given: Set of ‘n’ jobs. Associated with job ‘i’ is an integer deadline di >= 0
and a profit Pi>0.
For any job i the profit Pi is earned iff the job is completed by its deadline.
To complete a job, one has to process the job on a machine for one unit of
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
time.
Only one machine is available for processing jobs.
A feasible solution for this problem is a subset J of jobs such that each job
in this subset can be completed by its deadline.
The value of feasible solution J is the sum of the profits of the jobs in J, or
ΣiЄJ Pi.
An optimal solution is a feasible solution with maximum value. Since the
problem involves the identification of a subset. It fits the subset paradigm.
JOB SEQUENCING WITH DEADLINES
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Example : Let n = 4, (p1,p2,p3,p4) = (100,10,15,27), (d1,d2,d3,d4)
= (2,1,2,1)
Example : Let n = 4, (p1,p2,p3,p4) = (100,10,15,27),
(d1,d2,d3,d4) = (2,1,2,1)
S.No. Feasible Processing Profit value
Solution Sequence
0 1 2
(i) (1,2) (2,1) 110
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
(ii) (1,3) (1,3) or (3,1) 115
(iii) (1,4) (4,1) 127 is the optimal one
(iv) (2,3) (2,3) 25
(v) (3,4) (4,3) 42
(vi) (1) (1) 100
(vii) (2) (2) 10
(viii) (3) (3) 15
(ix) (4) (4) 27
To formulate a greedy algorithm, to obtain an optimal solution,
we must formulate an optimization measure to determine how
the next job is chosen.
As a first attempt, we can choose the objective function Σ iЄJ Pi
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
as our optimization measure.
Using this measure, the next job to include is the one that
increases Σ iЄJ Pi the most, subject to the constraint that the
resulting J is a feasible solution,
This requires us to consider the jobs in decreasing order of the
Pi’s.
GREEDY ALGORITHM TO OBTAIN AN OPTIMAL
SOLUTION
Example : Let n = 4, (p1,p2,p3,p4) = (100,10,15,27),
(d1,d2,d3,d4) = (2,1,2,1). Solve the problem using Greedy
Method.
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Sol: Consider the jobs in the decreasing order of profits
(p1,p2,p3,p4) = (100, 27, 15, 10)
0 1 2
(d1,d2,d3,d4) = ( 2, 1, 2, 1)
J = { 1 } is a feasible one
J = { 1, 4 } is a feasible one with processing sequence(4,1)
J = { 1, 3, 4 } is not feasible
J = { 1, 2, 4 } is not feasible
J = { 1, 4 } is optimal
EX:2 Job 1 2 3 4 5 6 7
Deadline 3 1 1 3 1 3 2
Profit 40 35 30 25 20 15 10
0 J2
J1- Max. time is 3, so select the slot 2-3.
1
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
J2- Max. time is 1, so select the slot 0-1.
J3- Max. time is 1,slot 0-1 is not free , so discard it. J4
J4- Max. time is 3, so select the slot 1-2.
J5- Max. time is 1, slot 0-1 is not free, so discard it. 2
J6- Max. time is 3, slot 0-1,1-2,and 2-3 are not free so 3
discard. J1
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Profit 40 35 30 25 20 20 0 J3
1
J5
J4
Solution vector = {3,5,4,6}
Pi=30+35+40+20 = 125 3
4 J6
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
JOB SEQUENCING WITH DEADLINES
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
JOB SEQUENCING WITH DEADLINES
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
MINIMUM COST SPANNING TREE
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
MINIMUM COST SPANNING TREE
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
MINIMUM COST SPANNING TREE
MINIMUM COST SPANNING TREE
There are 2 ways to find the minimum cost spanning tree.
Kruskal’s Algorithm
Prim’s algorithm
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
PRIM’S ALGORITHM
A Greedy method to obtain a minimum cost spanning tree
builds this tree edge by edge.
The next edge to include is chosen according to some
optimization criterion.
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
The simplest such criterion is to choose an edge that results in
a minimum increase in the sum of the costs of the edges so far
included.
There are 2 possible ways to interpret this criterion.
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
also in Near[j].
3. Now consider the tree edge (1,6) and check ‘j’, i.e., j is near to which
vertex?
the cost of (2,1) is ? , the cost of (2,6) is ? And check which is minimum?
Accordingly update Near[j].
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
1,5,4,2 6 10 ∞ ∞ ∞ 25 ∞ ∞
,0 7 ∞ 14 ∞ 18 24 ∞ ∞
Tree Edge
J Near[j]
i K,l
1 6 0 0 0 0 0
1 (1,6)
2 2 1 1 1 3 0 1 2
(5,6)
1 1 4 0 0 10 14 16
3 (4,5) 3
(3,4) 4 1 5 0 0 0 6 7
4 3
5 (2,3) 5 6 0 0 0 0
25 12
6 (7,2) 6 1 0 0 0 0 0 5 4
5 22
7 1 4 4 2
Min Cost(T)= 99
EX:2 1 2 3 4 5 6
10 50 1 ∞ 10 ∞ 30 ∞ ∞
1 2
40 3 2 10 ∞ 50 ∞ 40 25
30
25 3 ∞ 50 ∞ ∞ 35 15
35
5
4 4 30 ∞ ∞ ∞ ∞ 20
6 55
15 5 ∞ 40 35 ∞ ∞ 55
20
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
6 ∞ 25 15 20 55 ∞
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
that section of highway.
EX: To travel from A to B
Is there a path from A to B?
Is there any other path from A to B which is the shortest path?
Given a directed graph G=( V, E), a weighting function cost for the edges of
G, and a source vertex ‘V0’. The problem is to determine the shortest paths
from V0 to all the remaining vertices of G. It is assumed that all the
weights are positive.
The shortest path between V 0 and some other node ‘V’ is an ordering among a
subset of the edges. Hence, this problem fits the Ordering paradigm.
Single Source Shortest Path
To formulate a greedy –based algorithm to generate the shortest paths, we
must conceive of a multi-stage solution to the problem and also of an
optimization measure.
One possibility is to build shortest paths one by one. As an optimization
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
measure, we can use the sum of the lengths of all paths so far generated.
For this measure, to be minimized, each individual path must be of
minimum length.
If we have already constructed ‘i’ shortest paths, then using this
optimization measure, the next path to be constructed should be the next
shortest minimum length path.
Single Source Shortest Path
The Greedy way to generate the shortest paths from V 0 to the remaining
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS
Find the cost adjacency matrix
if i=j, then mark it as -1
if there is no edge mark it as ∞
V=1 S[1] S[2] S[3] S[4] S[5] S[6]
F F F F F F
GNITS
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
Cost Adjacency Matrix Dist:
1 2 3 4 5 6 1 2 3 4 5 6
1 -1 50 45 10 ∞ ∞ -1 50 45 10 ∞ ∞
2 ∞ -1 10 15 ∞ ∞ 45 25
3 ∞ ∞ -1 ∞ 30 ∞
4 20 ∞ ∞ -1 15 ∞
5 ∞ 20 35 ∞ -1 ∞
6 ∞ ∞ ∞ ∞ 3 -1
Prepared By: R.Pallavi Reddy , Asst.Prof., CSE,
GNITS