Module_2
Module_2
Greedy Strategy
• Greedy choice
• Optimal substructure
Greedy choice
• Greedy approach construct solution through
sequence of steps.
• Each step decision is taken based on the
greedy choice – a decision is made whether the
selected input leads to an optimal solution or
not.
• If the data chosen in any step is optimal, then it
is added to the partial solution set and is fixed.
• If it is infeasible eliminate that data from
solution set.
• Application of greedy choice at each step
reduces the problem size.
Optimal substructure
• If a fraction xi , (0 ≤ xi ≤ 1) of object i is
placed to the knapsack, then a profit of pi
xi is earned.
• Objective is to obtain a filling into the
knapsack that maximize the total profit
earned.
Maximize ---objective
Subject to ----Constraint
and 0 ≤ xi ≤ 1, 0 ≤ i ≤n
Fractional Knapsack Problem
Algorithm greedyfractionalknapsack(m,n)
/*m – capacity of knapsack
n – number of objects
Algorithm
greedyfractionalknapsack(m,n)
{
for i=1 to n
x[i]=0.0
U=m
for i= 1 to n
{
if( w[i] > U)
break;
x[i] =1.0
U=U - w[i]
}
if(i≤n)
x[i]=U/w[i]
Fractional Knapsack Problem
{
Algorithm for i=1 to n
greedyfractionalknapsack(m,n) x[i]=0.0
U=m
/*m – capacity of knapsack
for i= 1 to n
n – number of objects {
p[1..n] and w[1..n] contains if( w[i]>U)
break;
the profits and weights of x[i]=1.0
objects respectively. U=U-w[i]
The objects are arranged in }
the decreasing order of if(i≤n)
x[i]=U/w[i]
(profit/weight).
}
X[1..n] is the solution set.*/
Analysis of Fractional Knapsack Problem
Weight 13 8 6
Solution
Find P/W of each item
object A B C
profit (p) 19 13 9
Weight 13 8 6
(w)
PROFIT(p/ 1.46 1.6 1.5
w) 2
Capacity of knapsack is 20
Arrange the item in the decreasing order of
p/w
object B C A
profit (p) 13 9 19 Iteration 1
i=1, U=20
Weight (w) 8 6 13
W[1]=8<20
PROFIT(p/w) 1.6 1.5 1.46 X[1]=1.0, U=20-
2 8=12
Iteration 2
i=2, U=12
X 1.0 1.0 0.46 W[2]=6<12
index 1 2 3 X[2]=1.0,
U=12-6=6
Total Profit Iteration 3
earned =Σxi pi i=3, U=6
=1*13+1*9+0.46 W[3]=13>6
X[3]= 6/13 =.46
*19
=30.74
Minimum cost Spanning Tree (MST)
• A minimum spanning tree of G is a spanning tree of G
having a minimum cost.
• A MST can be generated using Prim’s or Kruskal’s
Algorithm.
• Prim’s and Kruskal’s Algorithm are greedy algorithms.
• To apply these algorithms, the given graph must be
weighted, connected and undirected
Minimum cost Spanning Tree (MST)
•If all the edge weights are distinct, then both the
algorithms are guaranteed to find the same MST.
•If all the edge weights are not distinct, then both the
algorithms may not always produce the same MST.
Step-01:
Randomly choose any vertex.
• one vertex connecting to the edge having least weight
is usually selected.
Step-02:
•Find all the edges that connect the tree to new vertices.
•Find the least weight edge among those edges and include
it in the existing tree.
•If including that edge creates a cycle, then reject that edge
and look for the next least weight edge.
Step-03:
Keep repeating step-02 until all the vertices are included
and Minimum Spanning Tree (MST) is obtained.
Prim’s Algorithm-
Algorithm MST_PRIM(G,w,r)
{
1. for (each u
{
2. Key[u]=
3. P[u]= Nil
}
4. key[r] =0
5. Q= V[G]
6. while (Q!=)
{
7. u= Extract_min(Q)
8. for (each v Adj[u])
9. if( v Q and w(u,v) < key[v] )
{
10. P[v] = u
11. key[v] = w(u,v)
}
}
}
Prim’s Algorithm
Step 5 Queue
creation
Min. Priority queue
Q
Verte A B C D
x
Key 0 ∞ ∞ ∞
step 6 – while loop -constructing the MST
Algorithm exit from the loop when the Q
become empty
VERTEX
A B C D
Key[v] 0 ∞64
10 ∞
98
∞7
P[v] Nil A D
A A
D
Priority queue A B C D
key[v]
key[v] w(uv)
w(uv)
u
uu =D
=A
=B
=C V=A,B,D
V=A,B,C
V=B,C,D
V=A,C,D key[B] =∞ w(AB)
A ,C and
key[B] D are
=10
=64 not in =64
w(CB)
w(DB)
key[C] =∞
queue
=13
=10 w(AC)=9
key[D]
(key[C] =∞
No change)
=9 w(AD)=7
A and D are not in
w(DC)=8
queue
A not in Queue
Time Complexity Analysis
• Run time of prim’s algorithm depends on how
we implement priority queue
• To get the minimum weight edge, if we use min
heap as a priority queue, operations like
extracting minimum element and decreasing
key value takes O(logV) time.
• So, overall time complexity
= O(V log V + E logV) = O(E logV)
Time Complexity Analysis
• This time complexity can be improved and
reduced using Fibonacci heap.
• If we use Fibonacci heap as a priority queue,
operations like extracting minimum element
takes O(log V) time and decreasing key value
takes O(1) time.
• So, overall time complexity
= O(E + V logV) = O(V log V)
Prim’s Algorithm Time Complexity-
Step-2:
Take the edge with the lowest weight and use it to
connect the vertices of graph.
Step-3:
Keep adding edges until all the vertices are connected
and a Minimum Spanning Tree (MST) is obtained.
Kruskal’s Algorithm
Algorithm Kruskals(G,w)
{
1. MST=Ф
2. for each vertex vV(G)
3. Make_Set(v) //fn to make disjoint sets
4. sort the edges in E(G) by increasing weight w
5. for each sorted edge (u,v) E(G)
{
6. if ( Find_Set(u) Find_Set(v) ) //find the representative
element of a set
{
7. MST=MST U {(u,v)}
8. Union(u,v) //combine two disjoint sets
}
}
9. Return MST
}
Step 2-3
Kruskal’s Algorithm { {A}, {B}, {C},
{D} }
Step 4
Edge Cost
AD 7
CD 8
AC 9
BD 10
B BC 13
10 AB 64
A
7 Step 5-9
Edge Find_set(u) find_set(v) Select Union
8 D
C edge
AD A D
Y {A,D} { {B}, {C} }
CD C D
Y {A,C,D} { {B} }
AC C C
N
BD B C
Kruskal’s Algorithm – Time Analysis
(suppose we are implementing disjoint set with union by rank
and and path compression heuristics)
Algorithm Kruskals(G,w)
{
MST=Ф ---------------1
for each vertex vV(G) ------------ V
Make_Set(v) ----------------- V O(log V)
Analysis-
Union operation
• Union operation uses Find_set() to determine the roots of
the trees contain the vertex. If the roots are distinct, the
trees are combined by attaching the root of one to the root
of the other.
• If we are using this method the height of the trees can grow
as O(V).
The tree that we are making or The tree that we are making or
growing always remains growing usually remains
connected. disconnected.
2 14 7
16 3
2
4 18 7
2
22
5 4
14
16
1
7 24
5 7
10
25
6 5
12
2
6
5 22 28
5 1 2
4
Cost =99
Prim’s Algorithm
Practice problem
1 2
1
1
1 6
4
0 3
7
6
1
25 2
5 4
22
Return A
}
Time analysis
Algorithm Jobsequencing(S)
{
N= length(S)
A=1
Assign slot[d1-1, d1] to 1
job A B C D E
Number 1 2 3 4 5
Profit Pi 30 25 10 5 3
Deadline Di 2 4 1 4 3
select Yes Yes Yes Yes No
slot 1-2 3-4 0-1 2-3
Eg: 2 Given
job A B C D E
Profit Pi 100 19 27 25 15
Deadline Di 2 1 2 1 3
Arrange the jobs in decreasing order of profit & number the jobs
job A C D B E
Job Number 1 2 3 4 5
Profit Pi 100 27 25 19 15
Deadline Di 2 2 1 1 3
Arrange the jobs in decreasing order of profit & number the jobs
job C D A B
Job No. 1 2 3 4
Profit Pi 40 30 20 10
Deadline 1 1 4 1
Di