Prim's and Kruskal's Algorithm
Prim's and Kruskal's Algorithm
• Time Complexity:
The time complexity of the Prim’s Algorithm is O((V+E)logV)
because each vertex is inserted in the priority queue only once and
insertion in priority queue take logarithmic time.
Prims Algorithm
Step 0: Choose any element r ; Set S ={r} and A= ɸ
(Take r as the root of our Spanning Tree)
Step 1: Find the lightest edge such that one end
point is in S and the other is in V-S.
Add this edge to A and its (other) endpoint to S.
Step 2: If V-S = ɸ, then STOP and Output(Minimum)
Spanning Tree (S,A) otherwise GOTO STEP1
• Minimum
Walk-Through
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2
3
F C
A 4
3
4
B D
H
2
3
G E
Done
Example
Kruskal’s Algorithm
Algorithm Steps:
1. Sort the graph edges with respect to their weights.
2.Start adding edges to the MST from the edge with the
smallest weight until the edge of the largest weight.
E4 (b, c) 1
E3 (a, d) 13
E7 (c, d) 2
E4 (b, c) 1
E8 (d, e) 3
E5 (b, e) 4
E5 (b, e) 4
E6 (b, f) 5 E6 (b, f) 5
E2 (a, c) 9
E7 (c, d) 2
E3 (a, d) 13
E8 (d, e) 3
E9 (d, f) 14
E9 (d, f) 14
E1 (a, b) 20
• As all the edges are covered in the last figure, the algorithm is
stopped and this is considered as the minimal spanning tree and
the total weight of the spanning tree is (1+2+3+5+9)=20.
Walk-Through
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
Sort the edges by increasing edge weight
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
F C edge dv edge dv
A 3 (D,E) 1 (B,E) 4
4
(D,G) 2 (B,F) 4
5
B D (E,G) 3 (B,H) 4
H 1
(C,D) 3 (A,H) 5
2
(D,F) 6
}
3 (G,H) 3
not
G E (C,F) 3 (A,B) 8 considered
(B,C) 4 (A,F) 10
Done
Total Cost = dv = 21
Example
Shortest Path Algorithms
The shortest path problem is the problem of
finding a path between two vertices (or nodes)
in a graph such that the sum of the weights of its
constituent edges is minimized.
Algorithms
-Dijkstra’s Algorithm
-Floyd/Floyd–Warshall algorithm
Dijkstra’s Shortest Path Algorithm
• An algorithm for finding the shortest paths between
nodes in a graph