0% found this document useful (0 votes)
66 views

Prim's and Kruskal's Algorithm

The document discusses Prim's and Kruskal's algorithms for finding minimum spanning trees in graphs. It defines minimum spanning trees as spanning subgraphs of weighted, undirected graphs that connect all nodes with the minimum total edge weight. Both Prim's and Kruskal's algorithms use greedy approaches to find optimal minimum spanning trees. Prim's grows the tree from a starting node, adding adjacent nodes. Kruskal's works with edges, sorting by weight and adding edges that avoid cycles until all nodes are connected.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Prim's and Kruskal's Algorithm

The document discusses Prim's and Kruskal's algorithms for finding minimum spanning trees in graphs. It defines minimum spanning trees as spanning subgraphs of weighted, undirected graphs that connect all nodes with the minimum total edge weight. Both Prim's and Kruskal's algorithms use greedy approaches to find optimal minimum spanning trees. Prim's grows the tree from a starting node, adding adjacent nodes. Kruskal's works with edges, sorting by weight and adding edges that avoid cycles until all nodes are connected.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Prim’s And Kruskal’s Algorithm

Minimum Spanning Tree


• Given an undirected and connected graph G=(V,E), a
spanning tree of the graph G is a tree that
spans G(that is, it includes every vertex of G) and is a
subgraph of G (every edge in the tree belongs to G)
• A minimum spanning tree (MST) of a weighted
graph G is a spanning tree of G .
• A Minimum Spanning Tree (MST) is a subgraph of an
undirected graph such that the subgraph spans
(includes) all nodes/Vertices, is connected, is
acyclic, and has minimum total edge weight.
• What is a Minimum Spanning Tree?
• The cost of the spanning tree is the sum of the weights of all the edges
in the tree.
• There can be many spanning trees( |v|-1 edges).
• Minimum spanning tree is the spanning tree where the cost is minimum
among all the spanning trees.
• There also can be many minimum spanning trees.
• Minimum spanning tree has direct application in the design of
networks.
• It is used in algorithms approximating the travelling salesman problem,
multi-terminal minimum cut problem and minimum-cost weighted
perfect matching.
Other practical applications are:
• Cluster Analysis
• Handwriting recognition
• Image segmentation
Minimum Spanning tree
Algorithms
i) Prim’s Algorithm
ii) Kruskal’s Algorithm
• Prim’s and Kruskal’s Algorithms work with
undirected graphs
• Both work with weighted and unweighted
graphs but are more interesting when edges
are weighted
• Both are greedy algorithms that produce
optimal solutions
Prim’s Algorithm
• Prim’s Algorithm also use Greedy approach to find the minimum
spanning tree.
• In Prim’s Algorithm we grow the spanning tree from a starting
position. we add vertex to the growing spanning tree in Prim's.
Algorithm Steps:
• Maintain two disjoint sets of vertices. One containing vertices that are
in the growing spanning tree and other that are not in the growing
spanning tree.
• Select the cheapest vertex that is connected to the growing spanning
tree and is not in the growing spanning tree and add it into the
growing spanning tree. This can be done using Priority Queues. Insert
the vertices, that are connected to growing spanning tree, into the
Priority Queue.
• Check for cycles. To do that, mark the nodes which have been already
selected and insert only those nodes in the Priority Queue that are
not marked.
• In Prim’s Algorithm, we will start with an arbitrary node (it doesn’t matter
which one) and mark it.
• In each iteration we will mark a new vertex that is adjacent to the one that
we have already marked.
• As a greedy algorithm, Prim’s algorithm will select the cheapest edge and
mark the vertex. So we will simply choose the edge with weight 1.
• In the next iteration we have three options, edges with weight 2, 3 and 4.
So, we will select the edge with weight 2 and mark the vertex.
• Now again we have three options, edges with weight 3, 4 and 5. But we
can’t choose edge with weight 3 as it is creating a cycle. So we will select
the edge with weight 4 and we end up with the minimum spanning tree of
total cost 7 ( = 1 + 2 +4).

• 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

Work with edges, rather than nodes


Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle
Kruskal’s Algorithm
Kruskal’s Algorithm builds the spanning tree by adding edges
one by one into a growing spanning tree.
Kruskal's algorithm follows greedy approach as in each iteration
it finds an edge which has least weight and add it to the
growing spanning tree.

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.

3. Only add edges which doesn't form a cycle , edges which


connect only disconnected components.
Kruskal’s Algorithm
Steps:
1. Sort all the edges in non-decreasing order of
their weight.
2. Pick the smallest edge. Check if it forms a
cycle with the spanning tree formed so far. If
cycle is not formed, include this edge. Else,
discard it.
3. Repeat step #2 until there are (V-1) edges in
the spanning tree.
Edge No. Vertex Edge The following table is constructed from the above
Pair Weight graph:
E1 (a, b) 20
In accordance with the weight of the Edge, the
table is rearranged in ascending order.
E2 (a, c) 9 Edge No. Vertex Pair Edge 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

Accepting edge (E,G) would create a cycle


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
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

• Finds shortest paths from the source to all other nodes


in the graph

• For a given source node in the graph, the algorithm finds


the shortest path between that node and every other.

• Shortest path algorithm is widely used in network


routing protocols
Example

You might also like