0% found this document useful (0 votes)
86 views29 pages

Minimal Spanning Tree

The document discusses spanning trees and minimum spanning trees. It defines a spanning tree as a subgraph that includes all vertices with a minimum number of edges and no cycles. A minimum spanning tree is a spanning tree where the sum of edge weights is minimum. The document describes Kruskal's and Prim's algorithms for finding minimum spanning trees and provides pseudocode and examples of how they work by adding edges in order of increasing weight.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views29 pages

Minimal Spanning Tree

The document discusses spanning trees and minimum spanning trees. It defines a spanning tree as a subgraph that includes all vertices with a minimum number of edges and no cycles. A minimum spanning tree is a spanning tree where the sum of edge weights is minimum. The document describes Kruskal's and Prim's algorithms for finding minimum spanning trees and provides pseudocode and examples of how they work by adding edges in order of increasing weight.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Minimal Spanning Tree

DR. SHWETA SHARMA


Spanning Tree
 A spanning tree is a sub-graph of an undirected connected graph, which
includes all the vertices of the graph with a minimum possible number of
edges

 If a vertex is missed, then it is not a spanning tree

 No Cycle

 The edges may or may not have weights assigned to them

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 2


Spanning Tree
 The total number of spanning trees with n vertices that can be created
from a complete graph is equal to n(n-2)
 If we have n = 4, the maximum number of possible spanning trees is
equal to 44-2 = 16. Thus, 16 spanning trees can be formed from a
complete graph with 4 vertices

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 3


Example

Some of the possible Spanning Trees


A Normal Graph

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 4


Minimum Spanning Tree
 A minimum spanning tree is a spanning tree in which the sum of the
weight of the edges is as minimum as possible

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 5


Example of a Minimum Spanning Tree

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 6


1 Kruskal’s Algorithm
 Kruskal's algorithm is a minimum spanning tree algorithm that takes a
graph as input and finds the subset of the edges of that graph which:

 form a tree that includes every vertex


 has the minimum sum of weights among all the trees that can be formed
from the graph

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 7


Pseudo code
KRUSKAL(G):
A=∅
For each vertex v ∈ G.V:
MAKE-SET(v)
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
if FIND-SET(u) ≠ FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)
return A

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 8


Working of Kruskal’s Algorithm
 It falls under a class of algorithms called greedy algorithms that find the
local optimum in the hopes of finding a global optimum

 We start from the edges with the lowest weight and keep adding edges
until we reach our goal

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 9


Working of Kruskal’s Algorithm
The steps for implementing Kruskal's algorithm are as follows:

 Sort all the edges from low weight to high


 Take the edge with the lowest weight and add it to the spanning tree. If
adding the edge created a cycle, then reject this edge.
 Keep adding edges until we reach all vertices or until there are (V-1) edges
in the spanning tree

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 10


Example of Kruskal’s Algorithm

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 11


Example of Kruskal’s Algorithm

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 12


Example of Kruskal’s Algorithm

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 13


Question 1

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 14


Solution 1

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 15


2 Prim’s Algorithm
 Prim's algorithm is a minimum spanning tree algorithm that takes a
graph as input and finds the subset of the edges of that graph which:

 form a tree that includes every vertex


 has the minimum sum of weights among all the trees that can be formed
from the graph

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 16


Working of Prim’s Algorithm
 It falls under a class of algorithms called greedy algorithms that find the local
optimum in the hopes of finding a global optimum

 We start from one vertex and keep adding edges with the lowest weight until
we reach our goal

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 17


Working of Prim’s Algorithm
The steps for implementing Prim's algorithm are as follows:

 Initialize the minimum spanning tree with a vertex chosen at random


 Find all the edges that connect the tree to new vertices, find the minimum
and add it to the tree
 Keep repeating step 2 until we get a minimum spanning tree

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 18


Pseudo code

T = ∅;
U = { 1 };
while (U ≠ V)
let (u, v) be the lowest cost edge such that u ∈ U and v ∈ V - U;
T = T ∪ {(u, v)}
U = U ∪ {v}

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 19


Example
Starting Vertex: C

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 20


Example

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 21


Example

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 22


Question 2
Starting Vertex: a

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 23


Solution 2

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 24


Question 3
Starting Vertex: D

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 25


Solution 3

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 26


Question 4
Starting Vertex: A

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 27


Solution 4

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 28


Thank you!

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 29

You might also like