Minimum Spanning Tree
Minimum Spanning Tree
Algorithm
Greedy Methods
(Minimum Spanning Tree)
Lecture – 45 -
53
Overview
return A
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Generic MST Algorithm
GENERIC-MST
;
while A is not a spanning tree
find an edge that is safe for A
return A
Use the loop invariant to show that this generic algorithm
works.
• Initialization: The empty set trivially satisfies the loop
invariant.
• Maintenance: Since we add only safe edges, A remains a
subset of some MST.
• Termination: All edges added to A are in an MST, so when we
stop, A is a spanning tree that is also an MST.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Concept and Examples
• Prim’s Algorithm
• Concept and Examples
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Concept and Examples
• Prim’s Algorithm
• Concept and Examples
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Kruskal’s Algorithm is a famous greedy
algorithm.
• It is used for finding the Minimum
Spanning Tree (MST) of a given graph.
• To apply Kruskal’s algorithm, the given
graph must be weighted, connected and
undirected.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Implementation of algorithm.
• The implementation of Kruskal’s Algorithm is
explained in the following steps-
• Step-01:
• Sort all the edges from low weight to high
weight.
• Step-02:
• Take the edge with the lowest weight and use it
to connect the vertices of graph.
• If adding an edge creates a cycle, then reject
that edge and go for the next least weight edge.
• Step-03:
• Keep adding edges until all the vertices are
connected and a Minimum Spanning Tree (MST)
is obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Kruskal’s Algorithm-
8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm:
Example 1:
Solution: Read the edges 8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
ab 4
Example 1: bc 8
Solution: Read the edges cd 7
8 7 de 9
b c d ef 10
4 9
2 df 14
11 cf 4
a i 4
14 e
ci 2
7 6
8 10 ih 7
h g f ig 6
1 2 gf 2
gh 1
ah 8
bh 11
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Implementation of algorithm.
• The implementation of Kruskal’s Algorithm is
explained in the following steps-
• Step-01:
• Sort all the edges from low weight to high
weight.
• Step-02:
• Take the edge with the lowest weight and use it
to connect the vertices of graph.
• If adding an edge creates a cycle, then reject
that edge and go for the next least weight edge.
• Step-03:
• Keep adding edges until all the vertices are
connected and a Minimum Spanning Tree (MST)
is obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 1 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Implementation of algorithm.
• The implementation of Kruskal’s Algorithm is
explained in the following steps-
• Step-01:
• Sort all the edges from low weight to high
weight.
• Step-02:
• Take the edge with the lowest weight and use it
to connect the vertices of graph.
• If adding an edge creates a cycle, then reject
that edge and go for the next least weight edge.
• Step-03:
• Keep adding edges until all the vertices are
connected and a Minimum Spanning Tree (MST)
is obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8
7 X6 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
X
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8
7 X6 bc 8
X
10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8
7 X6 bc 8
X
10
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8X
7 X6 bc 8
X
10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8X
7 X6 bc 8
X
10
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
8X
7 X6 X bc 8
X
10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
a 11 X i 14 e
cd 7
4 ih 7
8X
7 X6 X bc 8
X
10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
a 11 X i X14 e
cd 7
4 ih 7
8X
7 X6 X bc 8
X
10
h g f ah 8
1 2 de 9
ef 10
bh 11
Form a df 14
cycle
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm: Edge Weight
gh 1
Example 1: ci 2
Solution: Apply Step 2 and 3 gf 2
8 7 ab 4
b c d cf 4
4 9
2 ig 6
11 cd 7
a i 4
14 e
ih 7
7 6
8 10 bc 8
h g f ah 8
1 2 de 9
ef 10
bh 11
df 14
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
MST-KRUSKAL(G, w)
1 A←Ø
2 for each vertex v V[G]
3 do MAKE-SET(v)
4 sort the edges of E into nondecreasing order by weight w
5 for each edge (u, v) E, taken in nondecreasing order by
weight
6 do if FIND-SET(u) ≠ FIND-SET(v)
7 then A ← A U {(u, v)}
8 UNION(u, v)
9 return A
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm(Analysis)
• The running time of Kruskal's algorithm for a graph
depends on the implementation of the disjoint-set
data structure.
• Initializing the set A in line 1 takes time, and the
time to sort the edges in line 4 is O(E lg E).
• The for loop of lines 5-8 performs FIND-SET and
UNION operations on the disjoint-set forest. Along
with the |V| MAKE-SET operations, these take a
total of time.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm (Analysis)
• the total running time of Kruskal's algorithm is
• Observing that
• Apply log both side
• Hence the running time of Kruskal's algorithm as
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm:
Example 2: Construct the minimum spanning tree (MST) for the
given graph using Kruskal’s Algorithm- Self
28
b c Practice
10 14 16
a e d
24 18
25 12
f g
22
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Kruskal’s Algorithm
• Concept and Examples
• Prim’s Algorithm
• Concept and Examples
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm
• Prim’s Algorithm is a famous greedy
algorithm.
• It is used for finding the Minimum
Spanning Tree (MST) of a given graph.
• To apply Prim’s algorithm, the given graph
must be weighted, connected and
undirected.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm (Implimentation)
• The implementation of Prim’s Algorithm is
explained in the following steps-
• Step-1:
• Randomly choose any vertex.
• The vertex connecting to the edge having
least weight is usually selected.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm (Implementation)
• Step-2:
• 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-2 until all the vertices are
included and Minimum Spanning Tree (MST) is
obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm (Implementation)
• Step-2:
• 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-2 until all the vertices are
included and Minimum Spanning Tree (MST) is
obtained.
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm: (Algorithm)
MST-PRIM(G, w, r)
1 for each u V[G]
2 do key[u] ← ∞
3 [u] ← NIL
4 key[r] ← 0
5 Q ← V [G]
6 while Q ≠ Ø
7 do u ← EXTRACT-MIN(Q)
8 for each v Adj[u]
9 do if v Q and w(u, v) < key[v]
10 then π[v] ← u
11 key[v] ← w(u, v)
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f
1 2
a b c d e f g h i
key
/ / / / / / / / /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9 Assume that ‘a’ is the
2 root.
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ / / / / / / / /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given
Mi n
graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 1
4 9
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Kruskal’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a / / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Constructn the minimum spanning tree (MST) for the
Mi
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b / / / / a /
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c / c / a c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1:inConstruct the minimum spanning tree (MST) for the
M
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c / c / a c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1:inConstruct the minimum spanning tree (MST) for the
M
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c / c / a c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Kruskal’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c / c i i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c / c i i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c / c i i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
Mi n key
/ a b c f c f i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f i c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f g c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
Mi n key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8 10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
cycle key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum
Mi n
spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c f c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
cycle h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
cycle h g f Q a b c d e f g h i
1 2
Mi n
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Mi n Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
8 7
b c d Use step 2 and 3
4 9 until MST form
2
a 11 i 14 e
4
7 6
X
8
X
10
h g f Q a b c d e f g h i
1 2
a b c d e f g h i
key
/ a b c d c f h c
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm:
Example 1: Construct the minimum spanning tree (MST) for the
8 7
given graph
b usingcPrim’s Algorithm-
d
4 9 Use step 2 and 3
2 until MST form
a 11 i 4
14 e
7 6
8
X
10
X
X
h g f
1 2
Since all the vertices have been included in the MST, so stop the
process.
Now, Cost of Minimum Spanning Tree
= Sum of all edge weights
= w[ab]+w[bc]+w[cd]+w[ci]+w[cf]+w[de]+w[gh]+w[fg]
=4+8+7+2+4+9+2+1+=37
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem
• Prim’s Algorithm: (Analysis)
MST-PRIM(G, w, r)
1 for each u V[G]
Ο (𝑉 )
2 do key[u] ← ∞
3 [u] ← NIL
Ο (1)
4 key[r] ← 0
by the help of Fibonacci Heap
5 Q ← V [G]
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem Ο (𝑉 lg 𝑉 )
• Prim’s Algorithm: (Analysis)
MST-PRIM(G, w, r)
6 while Q ≠ Ø Ο(lg 𝑉 )
7 do u ← EXTRACT-MIN(Q) Ο (𝑉 )
8 for each v Adj[u]
9 do if v Q and w(u, v) < ( 𝐸)
Οkey[v]
10 then π[v] ← u
11 𝐷𝑒𝑐𝑟𝑒𝑎𝑠𝑒 𝐾𝑒𝑦𝑜𝑓
key[v] ←𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖
w(u, v) 𝐻𝑒𝑎𝑝𝑤𝑖𝑙𝑙 𝑡𝑎𝑘𝑒 Ο(1)
Time Complexity of Prim’s Algorithm
=Build max Heap()+Extract Min()+Decrease Key()
={In case of Sparse graph}
=
Greedy Algorithm
• Problem 4: Minimum Spanning Tree
problem Ο (𝑉 lg 𝑉 )
• Prim’s Algorithm: (Analysis)
MST-PRIM(G, w, r)
6 while Q ≠ Ø Ο(lg 𝑉 )
7 do u ← EXTRACT-MIN(Q) Ο (𝑉 )
8 for each v Adj[u]
9 do if v Q and w(u, v) < ( 𝐸)
Οkey[v]
10 then π[v] ← u
11 𝐷𝑒𝑐𝑟𝑒𝑎𝑠𝑒 𝐾𝑒𝑦𝑜𝑓
key[v] 𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖
← w(u, v) 𝐻𝑒𝑎𝑝𝑤𝑖𝑙𝑙 𝑡𝑎𝑘𝑒 Ο(1)
a g d
24 18
25 12
f e
22