Prim Algorithm
Minimum Spanning Trees
Ranjeet Kumar Rout
DR. B.R. Ambedkar NIT, Jalandhar
April 11, 2017
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 1 / 17
Overview
Introduction
Algorithm
Example
Complexity
Application
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 2 / 17
Introduction
Spanning Tree
If we have a graph G(V,E) where V is the no of vertices and E is the no of
edges in a graph then a spanning tree G’(V’, E’) of a Graph G, have
V’ = V
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 3 / 17
Introduction
Spanning Tree
If we have a graph G(V,E) where V is the no of vertices and E is the no of
edges in a graph then a spanning tree G’(V’, E’) of a Graph G, have
V’ = V
E’ = E-1
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 3 / 17
Introduction
Spanning Tree
If we have a graph G(V,E) where V is the no of vertices and E is the no of
edges in a graph then a spanning tree G’(V’, E’) of a Graph G, have
V’ = V
E’ = E-1
Acyclic
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 3 / 17
Introduction
Spanning Tree
If we have a graph G(V,E) where V is the no of vertices and E is the no of
edges in a graph then a spanning tree G’(V’, E’) of a Graph G, have
V’ = V
E’ = E-1
Acyclic
Connected
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 3 / 17
Minimum Spanning Tree:
A minimum spanning tree is a spanning tree with weight less than or equal
to the weight of every other spanning tree.
Connected
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 4 / 17
Minimum Spanning Tree:
A minimum spanning tree is a spanning tree with weight less than or equal
to the weight of every other spanning tree.
Connected
Acyclic
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 4 / 17
Minimum Spanning Tree:
A minimum spanning tree is a spanning tree with weight less than or equal
to the weight of every other spanning tree.
Connected
Acyclic
Minimum Weight
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 4 / 17
Prim’s Algorithm
Developed by Vojtech Jarnikin and Robert Prim.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 5 / 17
Prim’s Algorithm
Developed by Vojtech Jarnikin and Robert Prim.
Prim’s algorithm is a greedy algorithm that is used to find the
minimum cost spanning tree for connected, weighted, undirected
graph.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 5 / 17
Prim’s Algorithm
Developed by Vojtech Jarnikin and Robert Prim.
Prim’s algorithm is a greedy algorithm that is used to find the
minimum cost spanning tree for connected, weighted, undirected
graph.
The tree starts from an arbitrary root vertex r and grows until the
tree spans all the vertices in V.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 5 / 17
Prim’s Algorithm
Developed by Vojtech Jarnikin and Robert Prim.
Prim’s algorithm is a greedy algorithm that is used to find the
minimum cost spanning tree for connected, weighted, undirected
graph.
The tree starts from an arbitrary root vertex r and grows until the
tree spans all the vertices in V.
Each step adds to the tree A a light edge that connects A to an
isolated vertex–one on which no edge of A is incident.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 5 / 17
Prim’s Algorithm
Developed by Vojtech Jarnikin and Robert Prim.
Prim’s algorithm is a greedy algorithm that is used to find the
minimum cost spanning tree for connected, weighted, undirected
graph.
The tree starts from an arbitrary root vertex r and grows until the
tree spans all the vertices in V.
Each step adds to the tree A a light edge that connects A to an
isolated vertex–one on which no edge of A is incident.
This rule adds only edges that are safe for A; therefore, when the
algorithm terminates, the edges in A form a minimum spanning tree.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 5 / 17
Prim’s Algorithm
Developed by Vojtech Jarnikin and Robert Prim.
Prim’s algorithm is a greedy algorithm that is used to find the
minimum cost spanning tree for connected, weighted, undirected
graph.
The tree starts from an arbitrary root vertex r and grows until the
tree spans all the vertices in V.
Each step adds to the tree A a light edge that connects A to an
isolated vertex–one on which no edge of A is incident.
This rule adds only edges that are safe for A; therefore, when the
algorithm terminates, the edges in A form a minimum spanning tree.
This strategy qualies as greedy since at each step it adds to the tree
an edge that contributes the minimum amount possible to the trees
weight.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 5 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
5 Q = G.V
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
5 Q = G.V
6 while Q 6= φ
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
5 Q = G.V
6 while Q 6= φ
7 u = EXTRACT-MIN(Q)
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
5 Q = G.V
6 while Q 6= φ
7 u = EXTRACT-MIN(Q)
8 for each v ∈ G .Adj[u]
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
5 Q = G.V
6 while Q 6= φ
7 u = EXTRACT-MIN(Q)
8 for each v ∈ G .Adj[u]
9 if v ∈ Q and w (u, v ) ≤ v .key
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
5 Q = G.V
6 while Q 6= φ
7 u = EXTRACT-MIN(Q)
8 for each v ∈ G .Adj[u]
9 if v ∈ Q and w (u, v ) ≤ v .key
10 v .π = u
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Algorithm: MST-PRIM(G,w,r)
1 for each u ∈ G .V
2 u.key = ∞
3 u.π = NIL
4 r.key = 0
5 Q = G.V
6 while Q 6= φ
7 u = EXTRACT-MIN(Q)
8 for each v ∈ G .Adj[u]
9 if v ∈ Q and w (u, v ) ≤ v .key
10 v .π = u
11 v .key = w (u, v )
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 6 / 17
Example
Prim’s Algorithm
Minimum cost Spaning Tree : Apply Prim’s Algo
10 28
12
25 16
24
18
22 14
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 7 / 17
Example
Prim’s Algorithm
Minimum cost Spaning Tree : Apply Prim’s Algo
10 28 10
12
25 16
24
18
22 14
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 8 / 17
Example
Prim’s Algorithm
Minimum cost Spaning Tree : Apply Prim’s Algo
10 28 10
12
25 16 25
24
18
22 14
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 9 / 17
Example
Prim’s Algorithm
Minimum cost Spaning Tree : Apply Prim’s Algo
10 28 10
12
25 16 25
24
18
22 14 22
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 10 / 17
Example
Prim’s Algorithm
Minimum cost Spaning Tree : Apply Prim’s Algo
10 28 10
12
25 16 25
24
18
22 14 22 14
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 11 / 17
Example
Prim’s Algorithm
Minimum cost Spaning Tree : Apply Prim’s Algo
10 28 10
12
25 16 25 16
24
18
22 14 22 14
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 12 / 17
Example
Prim’s Algorithm
Minimum cost Spaning Tree : Apply Prim’s Algo
10 28 10
12 12
25 16 25 16
24
18
22 14 22 14
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 13 / 17
Complexity Analysis
The Time complexity of Prim Algorithm is O(ElogV ).
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 14 / 17
Complexity Analysis
The Time complexity of Prim Algorithm is O(ElogV ).
The running time of Prim’s algorithm depends on how we implement
the minpriority queue Q.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 14 / 17
Complexity Analysis
The Time complexity of Prim Algorithm is O(ElogV ).
The running time of Prim’s algorithm depends on how we implement
the minpriority queue Q.
The body of the while loop executes |V | times, and since each
EXTRACT-MIN operation takes O(lgV ) time, the total timefor allcalls
to EXTRACT-MIN is O(VlgV ) .
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 14 / 17
Complexity Analysis
The Time complexity of Prim Algorithm is O(ElogV ).
The running time of Prim’s algorithm depends on how we implement
the minpriority queue Q.
The body of the while loop executes |V | times, and since each
EXTRACT-MIN operation takes O(lgV ) time, the total timefor allcalls
to EXTRACT-MIN is O(VlgV ) .
The for loop in lines 8-11 executes O(E) times altogether
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 14 / 17
Complexity Analysis
The Time complexity of Prim Algorithm is O(ElogV ).
The running time of Prim’s algorithm depends on how we implement
the minpriority queue Q.
The body of the while loop executes |V | times, and since each
EXTRACT-MIN operation takes O(lgV ) time, the total timefor allcalls
to EXTRACT-MIN is O(VlgV ) .
The for loop in lines 8-11 executes O(E) times altogether
Thus, the total time for Prims algorithm is
O(VlgV + lgV ) = O(ElogV ) time.
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 14 / 17
Applications
All this path finding algorithms are used in AI (Artificial Intelligence)
Game Development
Cognitive Science
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 15 / 17
References
Introduction to Algorithm -Thomas H. Corman,Ronald L. Rivest,
Charles F. Leiserson.
http://www.geeksforgeeks.org/greedy-algorithms-set-2-kruskals-
minimum-spanning-tree-mst/
https://www.quora.com/What-are-practical-real-life-industrial-
applications-of-Dijkstra-Kruskal-and-Prims-Algortithm
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 16 / 17
The End
Thank You
Ranjeet Kumar Rout (DR. B.R. Ambedkar NIT, Jalandhar) Prim Algorithm April 11, 2017 17 / 17