DAA Unit III
DAA Unit III
Greedy Method
• Greedy algorithm obtains an optimal solution by
making a sequence of decisions.
• Greedy Solution
Example:
Shortest Paths on a Special Graph
• Problem
– Find a shortest path from v0 to v3
Is the solution optimal?
• Greedy Solution
Example:
Shortest Paths on a Multi-stage Graph
Is the greedy solution optimal?
• Problem
– Find a shortest path from v0 to v3
Example:
Shortest Paths on a Multi-stage Graph
Is the greedy solution optimal?
• Problem
– Find a shortest path from v0 to v3
The
Theoptimal
optimalpath
path
Example:
Shortest Paths on a Multi-stage Graph
The
Theoptimal
optimalpath
path
The Fractional Knapsack
Problem
• Given: A set S of n items, with each item i having
– pi - a positive profit
– wi - a positive weight
• Goal: Choose items, allowing fractional amounts(xi),
to maximize total profit but with weight at most
m.
maximize ∑ pix i
1≤i≤n
subjected to ∑ wixi ≤ m
1≤i≤n
and 0 ≤ xi ≤ 1, 1≤i≤n
The Fractional Knapsack Problem
Greedy decision property:-
Select items in decreasing order of profit/weight.
“knapsack”
Solution:
Items: 1 ml of i5
1 2 3 4 5
•
wi : 4 ml 8 ml 2 ml 6 ml 1 ml • 2 ml of i3
• 6 ml of i4
pi : $12 $32 $40 $30 $50
• 1 ml of i2
10 ml
Value: 3 4 20 5 50
($ per ml)
• Solution vector
(x1,x2,x3,x4,x5)= (0,1/8,1,1,1)
• Profit =12*0 + 32*1/8 + 40*1 + 30*1 + 50*1
= 0+4+40+30+50
=124.
Greedy algorithm for the fractional Knapsack problem
Algorithm GreedyKnapsack(m,n)
//P[1:n] and w[1:n] contain the profits and weights
// respectively of the n objects ordered such that
//p[i]/w[i]>=p[i+1]/w[i+1].
//m is the knapsack size and x[1:n] is the solution
// Vector.
{
for i := 1 to n do x[i] := 0; // Initialize x.
U := m;
for i :=1 to n do
{
if ( w[i] > U ) then break;
x[i] := 1; U := U-w[i];
}
if ( i <= n) then x[i] := U/w[i];
}
If you do not consider the time to sort the items, then the time taken by the
above algorithm is O(n).
0/1 Knapsack Problem
• An item is either included or not included
into the knapsack.
Formally the problem can be stated as
maximize ∑ pixi
1≤i≤n
subjected to ∑ wixi ≤ m
1≤i≤n
and xi=0 or 1, 1 ≤ i ≤ n
Which items should be chosen
to maximize the amount of
money while still keeping the
overall weight under m kg ?
m
na l k na pssaacckkaalgoorritithhm
lg m
f r a c
IIsstthhee fractionati o l k na p
aappppliliccaabbllee??
• The greedy method works for fractional knapsack problem,
but it does not for 0/1 knapsack problem.
• Ex:-
30
item3 $120
item2
20
item1 $100
30 20 $100
20 $60
10 10
(a) (b)
• There are 3 items, the knapsack can hold 50 gms.
1 1
A B A B A B
5 2
4 2 4 4
6
D C D C D C
3 3 3
1 1
Undirected Graph A B A B
2 2
4
D C D C
3
…
Some Spanning Trees
Minimum Cost Spanning Tree / Minimum
Spanning Tree (MST)
1 1
A B A B
4
4 2 2
5
D C D C
3 3
• Computer Networks
– To find how to connect a set of
computers using the minimum amount of
wire.
MST-Prim’s Algorithm
• Start with minimum cost edge.
• Select next minimum cost edge (i,j) such that
i is a vertex already included in the tree, j is
a vertex not yet included.
• Continue this process until the tree has n - 1
edges.
Prim’s Algorithm
t [1:n-1,1:2]
8 7
2 3 4 1 2
1 9
2 1 1 2
1 11 9i 4 14 5 2 2 3
7 16
8 10
8 7 6 3 9
4 2
. 3 6
. 6 7
2 3 4 .
7 8
1 9 5 3 4
n-1 4 5
8 7 6
Pseudo code algorithm of Prim’s method
1. It starts with the min cost edge.
2. The next edge ( i, j ) to be added is such that i is
a vertex already in the tree and j is a vertex not
yet included, and the cost[i, j] is minimum.
3. To determine this edge efficiently, we associate
with each vertex j not yet included in the tree a
value near[j].
4. The value near[ j ] is a vertex in the tree such that
cost[ j, near[j] ] is minimum among all choices
for near[ j ].
Note: We define near[ j ] = 0 for all vertices j that are
already in the tree.
L
2 3
1 j cost[ j, near[j] ]
K
1 3 near[3]=2 cost[ 3,2 ]=8
9 mincost=cost[k,l];
10 t[1,1]=k; t[1,2]=l;
11 near[k]=near[l]=0;
19 t[i,1]=j; t[i,2]=near[j];
20 mincost=mincost+cost[j,near[j]];
21 near[j]=0;
8
7 16
10
8 7 2
6
4
2 3 4
1 9 5
8 7 6
9
4
5 5
3 8
4 9
3 6 8 6
2
1 2 2 4 4 7 7 8 8 9 14 16
10
1
9 7 7 3
3 8 2 1
Does not form cycle 4 6
6
7
3 4
2
9 5
1
8 7 6
Forms cycle
Disjoint Sets
v V1 V2 V5
V1V3
V1V3V4
V1V3V4V2
V1V5
V3 V4 V6 5) V1V3V4V6 28
45 45
50
2
10
5
50
2
10 5
1 1
15 15
10 35 35
20 20 30 20 10 20 30
15 3 15 3
3 4 6 3 4 6
Iteration S Dist[2 Dist[3] Dist[4] Dist[5] Dist[6]
]
Initial {1} 50 10 ∞ 45 ∞
1 { 1,3 } 50 10 25
45 ∞
2 { 1,3,4 } 45 10 25 45 28
3 { 1,3,4,6 } 45 10 25 45 28
4 { 1,3,4,5,6 } 45 10 25 45 28
SSSP-Dijkstra’s algorithm
• Dijkstra’s algorithm assumes that cost(e)0 for each e in the
graph.
• Maintains a set S of vertices whose Shortest Path from v ( source)
has been determined.
• a) Select the next minimum distance node u, which is not in S.
• b) for each node w adjacent to u do
if( dist[w]>dist[u]+cost[u,w]) ) then
dist[w]:=dist[u]+cost[u,w];
17 s[u]:=true; // Put u in S.
18 for ( each w adjacent to u with s[w]= false) do
19 // Uupdate distance
20 if( dist[w]>dist[u]+cost[u,w]) ) then
21 dist[w]:=dist[u]+cost[u,w];
22 }
23 }
Time complexity of Dijkstra’s Algorithm
• The for loop of line 7 takes o(n).
• The for loop of line 12 takes o(n).
– Each execution of this loop requires o(n) time at lines
15 and 18.
– So the total time for this loop is o(n2).
• Therefore, total time taken by this algorithm is o(n2).
Job sequencing with deadlines
We are given a set of n jobs.
Deadline di>=0 and a profit pi>0 are associated with each job i.
For any job profit is earned if and only if the job is completed by
its deadline.
To complete a job, a job has to be processed by a machine for one
unit of time.
Only one machine is available for processing jobs.
A feasible solution of this problem is a subset of jobs such that
each job in this subset can be completed by its deadline
The optimal solution is a feasible solution which will maximize
the total profit.
The objective is to find an order of processing of jobs which will
maximize the total profit.
Ex:-n=4, ( p1,p2,p3,p4 )= ( 100,10,15,27 )
( d1,d2,d3,d4 )= ( 2,1,2,1 )
day1 day2 day3 day4 day5
0 1 2 3 4 5
time
Ex:-n=4, ( p1,p2,p3,p4 )=( 100,10,15,27 )
( d1,d2,d3,d4 )=( 2,1,2,1 )
The maximum deadline is 2 units, hence the feasible solution set
must have <=2 jobs.
feasible processing
solution sequence value
Solution 3 is optimal.
Greedy Algorithm for job sequencing with
deadlines
1. Sort pi into decreasing order. After sorting
p1 p2 p3 … pi.
2. Add the next job i to the solution set if i can be
completed by its deadline.
3. Stop if all jobs are examined. Otherwise, go to step 2.
Ex:- 1) n=5, ( p1,…….,p5) = ( 20,15,10,5,1 ) and
( d1,…….d5) = ( 2,2,1,3,3 )
The optimal solution is {p1,p2,p4} with a profit of
40.
j[r+1] :=i;
k:=k+1;
}
}
return k;
}
Worst -case time taken by this algorithm is o(n2)
Time complexity analysis
• The while loop is iterated at most k times.
• If the conditional statement if is true, then the
inside for loop will be iterated k-r times.
• Therefore, while loop and inside for loop time
complexity is O(k).
• Hence, the total time for each iteration of outer
for loop is O(k). This loop is iterated n-1 times.
• If s is the final value of k, that is , s is the number
of jobs in the final solution, then the total time
needed by algorithm is O(sn). Since s <= n, the
worst – case time complexity of this algorithm is
O(n2).