DSA-Unit-4 Prims Algorithm - MST
DSA-Unit-4 Prims Algorithm - MST
Unit 4
Prepared By
N. U. Khan
Computer Science And Engineering Department
IMS Engineering College, Ghaziabad
N U Khan 1
Reference Books
• 1.Horowitz and Sahani, “Fundamentals of Data
Structures”, Galgotia Publications Pvt Ltd Delhi
India.
• 2. Lipschutz, “Data Structures” Schaum’s Outline
Series, Tata McGraw-hill Education (India) Pvt.
Ltd.
• 3. Thareja, “Data Structure Using C” Oxford
Higher Education.
• 4. AK Sharma, “Data Structure Using C”, Pearson
Education India.
N U Khan 2
Topics covered in Unit-1
• Graphs: Terminology used with Graph, Data
Structure for Graph Representations:
Adjacency Matrices, Adjacency List, Adjacency.
Graph Traversal: Depth First Search and
Breadth First Search, Connected Component,
Spanning Trees, Minimum Cost Spanning Trees:
Prims and Kruskal algorithm. Transitive Closure
and Shortest Path algorithm: Warshal
Algorithm and Dijikstra Algorithm.
N U Khan 3
Graph
• A graph is a set of nodes ( vertices) connected by edges
• A graph ‘G’ consists of two sets ‘V’ and ‘E’
1. ‘V’ is a finite non empty set of vertices
2. ‘E’ is a set of pairs of vertices, these pairs are called edges.
e3 v2
e4
v1 e2
e1 e5
v3
v5
e6
e7 v4
We can write any graph G=(V, E)
1. V(G) represent set of vertices of graph ‘G’ [ V(G) = { v1, v2, v3, v4, v5 } ]
2. E(G) represent set of edges of graph ‘G’ [ E(G) = { e1, e2, e3, e4, e5, e6, e7 } ]
4
N U Khan
Adjacency List representation of weighted Graph
For memory representation an array of linked lists is used. The size of the
array is equal to the number of vertices.
Let the array is A[]. An entry A[i] represents the linked list of all those vertices
which are adjacent to the ith vertex.
A[1] v1 V2 7 V4 3 N
A[2] v2 V1 7 V4 5 V3 2 N v1
7
v2
V3 3 5
A[3] V2 2 V4 6 N 2
v4 v3
A[4] v4 V1 3 V2 5 V3 6 N 6
N U Khan 5
Spanning Tree
• Spanning tree is a acyclic subgraph of ‘G’, containing all the nodes of ‘G’
• i.e. it is derived from graph ‘G’ and including all vertices, there should not
be any cycle
v1 v2
v4 v3
G
T1 T2 T3
T1 ⊆ G without any cycle T2 ⊆ G without any cycle T3 ⊆ G without any cycle
and cover all vertices and cover all vertices and cover all vertices
N U Khan 6
Types of Spanning Tree
• Spanning tree can be derived from graph ‘G’ either by calling DFS or BFS
1. Depth First Spanning Tree
2. Breadth First Spanning Tree
v2 v3
v4 v5 v6 v7
v8
G T
N U Khan 7
Types of Spanning Tree
• Breadth First Spanning Tree:
• The spanning tree resulting from BFS is known as a breadth first
spanning tree
v1
v2 v3
v4 v5 v6 v7
v8
G T
N U Khan 8
Minimum Spanning Tree (MST)
• Given a connected weighted graph ‘G’, create a spanning tree ‘T’ for
graph ‘G’ such that the sum of the weights of the tree edges in ‘T’ is
as small as possible, such a tree is called a minimum spanning tree.
v1 6 v2
2 5 4
3 G
v4 5 v3
T1 T3
T1 ⊆ G without any cycle T3 ⊆ G without any cycle
Cost = 6 + 5 + 3 Cost = 2 + 4 + 3
= 14 =9
T2
T2 ⊆ G without any cycle
Cost = 2 + 6 + 3
= 11
N U Khan 9
MST
There are two techniques for creating a minimum
spanning tree for a weighted graph
1. Kruskal’s Algorithm
2. Prim’s Algorithm
N U Khan 10
Prim’s Algorithm
Let the given graph is G = (V, E) and we want to construct Minimum Spanning
Tree T = (S, A) from ‘G’
Step 1: Choose any starting vertex ‘a’ from a given graph G = ( V, E )
set S={ ‘a’ : a ϵ V } where S is set of vertices of MST ‘T’
[ Take ‘a’ as the root of spanning tree ‘T’ ]
and set A = φ where A is set of edges of MST ‘T’
Step 2: Find the lightest edge from ‘E’ such that one end point is in ‘S’ and
the other is in ‘V – S’.
Add this edge to ‘A’ i.e.
A = A ꓴ { cheapest edge ‘ab’ ϵ E : where a ϵ S and b ϵ V – S }
and add its other end point ‘b’ to ‘S’ i.e.
S = S ꓴ { ‘b’ : where b ϵ V – S }
Step 3: If V – S = φ, then stop and output will be Minimum Spanning
Tree T = (S, A)
Otherwise go to Step 2
N U Khan 11
How does Prim’s Algorithm work ?
Q: Determine the MST for following graph G=(V,E) using Prim’s Algorithm, starting
node is ‘a’
10
b e
4 8 7 6
a 9 d 5 g
2 9
8 2
c f
1
Solution:
a 9 d 5 g
2 9
8 2
c f
1
V = {a, b, c, d, e, f, g}
E = {ab, ac, bc, bd, be, ed, ef, eg, fd, fg, fc, cd }
Min
edge
Set of MST Set of MST
V–S ‘xy’ : x ϵ Graph
vertices ‘S’ edges ‘A’
S and
yϵV–S
10
V–S= b e
4 8 7 6
{a, b, c, d, e, f, g} A = φ U { ab } ‘ab’
S=φU{a} a 9 d 5 g
– { a}
A = { ab } 2 9
S={a} ={b, c, d, e, f, g} 8 2
c f
1
N U Khan 13
Min
edge
Set of MST Set of MST
V–S ‘xy’ : x ϵ Graph
vertices ‘S’ edges ‘A’
S and
yϵV–S
10
V–S= b e
4 8 7 6
{a, b, c, d, e, f, g} A = { ab } U { ac } ‘ac’
S={a}U{b} a 9 d 5 g
– { a, b }
A = { ab, ac } ‘bd’ 2 9
S = { a, b } ={c, d, e, f, g} 8 2
c f
1
10
V–S= b e
S = { a, b} U { c } 7
{a, b, c, d, e, f, g} A = { ab, ac } U {cf} 4 8 6
S = { a, b, c } – { a, b, c } ‘cf’ a d 5 g
A = { ab, ac, cf } 9
2 9
={d, e, f, g} 8 2
c f
1
N U Khan 14
Min
edge
Set of MST Set of MST
V–S ‘xy’ : x ϵ Graph
vertices ‘S’ edges ‘A’
S and
yϵV–S
10
V–S= b e
4 8 7 6
{a, b, c, d, e, f, g} A={ab, ac, cf}U{cd} ‘cd’
S={a, b, c} U { f } a 9 d 5 g
– { a, b, c, f }
A={ab, ac, cf, cd } ‘fg’ 2 9
S = { a, b, c, f } ={d, e, g} 8 2
c f
1
10
V–S= b e
S={a, b, c, f} U{ d } A={ab, ac, cf, cd} U{fg}
{a, b, c, d, e, f, g} 4 8 7 6
– { a, b, c, f, d } ={ab, ac, cf, cd, fg} ‘fg’ a d 5 g
S={ a, b, c, f, d } 9
2 9
={e, g} 8 2
c f
1
N U Khan 15
Min
edge
Set of MST Set of MST
V–S ‘xy’ : x ϵ Graph
vertices ‘S’ edges ‘A’
S and
yϵV–S
10
V–S= b e
A= 4 8 7 6
{a, b, c, d, e, f, g} {ab,ac,cf,cd,fg} U{fe}
S={a, b, c, f, d}U{g} a 9 d 5 g
– {a, b, c, f, d, g } ‘fe’
{ab,ac,cf,cd,fg,fe } 2 9
S={a, b, c, f, d, g } ={ e } 8 2
c f
1
10
V–S= b e
S={a,b,c,f,d,g} U{e} 4 8 7
{a, b, c, d, e, f, g} – 6
={a, b, c, f, d, g, e} {a, b, c, f, d, g, e} a 9 d 5 g
2 9
={ } 8 2
=φ c f
1
N U Khan 16