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

Minimum Spanning Tree

A spanning tree is a subset of a connected graph that connects all vertices without cycles, containing n-1 edges for n vertices. A minimum spanning tree (MST) is a spanning tree with the least total edge weight, which can be found using algorithms like Prim's and Kruskal's. Dijkstra's algorithm, while similar to Prim's, focuses on finding the shortest path tree from a source vertex to all other vertices in the graph.

Uploaded by

5qjq4t2w9d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Minimum Spanning Tree

A spanning tree is a subset of a connected graph that connects all vertices without cycles, containing n-1 edges for n vertices. A minimum spanning tree (MST) is a spanning tree with the least total edge weight, which can be found using algorithms like Prim's and Kruskal's. Dijkstra's algorithm, while similar to Prim's, focuses on finding the shortest path tree from a source vertex to all other vertices in the graph.

Uploaded by

5qjq4t2w9d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

What is a Spanning Tree?

A Spanning tree is a subset to a connected graph G, where all the edges are connected, i.e, we
can traverse to any edge from a particular edge with or without intermediates. Also, a
spanning tree must not have any cycle in it. Thus we can say that if there are n vertices in a
connected graph then the no. of edges that a spanning tree may have is n-1.

What is Minimum Spanning Tree?


Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a
tree and connects all the vertices together. A single graph can have many different spanning
trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted,
connected, undirected graph is a spanning tree with a weight less than or equal to the weight
of every other spanning tree. The weight of a spanning tree is the sum of weights given to each
edge of the spanning tree.

Prim’s Algorithm
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a
graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such
that the sum of the weights of the edges can be minimized.

Prim's algorithm starts with the single node and explores all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in the
graph got selected.

o First, we have to initialize an MST with the randomly chosen vertex.


o Now, we have to find all the edges that connect the tree in the above step with the new
vertices. From the edges found, select the minimum edge and add it to the tree.
o Repeat step 2 until the minimum spanning tree is formed.

The set mstSet is initially empty and keys assigned to vertices are {0, INF, INF, INF, INF, INF,
INF, INF} where INF indicates infinite. Now pick the vertex with the minimum key value. The
vertex 0 is picked, include it in mstSet. So mstSet becomes {0}. After including to mstSet,
update key values of adjacent vertices. Adjacent vertices of 0 are 1 and 7. The key values of 1
and 7 are updated as 4 and 8. Following subgraph shows vertices and their key values, only
the vertices with finite key values are shown. The vertices included in MST are shown in green
color.
Pick the vertex with minimum key value and not already
included in MST (not in mstSET). The vertex 1 is picked
and added to mstSet. So mstSet now becomes {0, 1}.
Update the key values of adjacent vertices of 1. The key
value of vertex 2 becomes 8.

Pick the vertex with minimum key value and not already
included in MST (not in mstSET). We can either pick vertex
7 or vertex 2, let vertex 7 is picked. So mstSet now
becomes {0, 1, 7}. Update the key values of adjacent
vertices of 7. The key value of vertex 6 and 8 becomes
finite (1 and 7 respectively).

Pick the vertex with minimum key value and not already
included in MST (not in mstSET). Vertex 6 is picked. So
mstSet now becomes {0, 1, 7, 6}. Update the key values of
adjacent vertices of 6. The key value of vertex 5 and 8 are
updated.

We repeat the above steps until mstSet includes all vertices


of given graph. Finally, we get the following graph.
Kruskal’s Algorithm

Below are the steps for finding MST using Kruskal’s algorithm
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.

The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be
having (9 – 1) = 8 edges.
After sorting:
Weight Src Dest
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Now pick all edges one by one from the sorted list of edges
1. Pick edge 7-6: No cycle is formed, include it.
2. Pick edge 8-2: No cycle is formed, include it.

3. Pick edge 6-5: No cycle is formed, include it.

4. Pick edge 0-1: No cycle is formed, include it.

5. Pick edge 2-5: No cycle is formed, include it.


6. Pick edge 8-6: Since including this edge results in the cycle, discard it.
7. Pick edge 2-3: No cycle is formed, include it.

8. Pick edge 7-8: Since including this edge results in the cycle, discard it.
9. Pick edge 0-7: No cycle is formed, include it.

10. Pick edge 1-2: Since including this edge results in the cycle, discard it.
11. Pick edge 3-4: No cycle is formed, include it.

Since the number of edges included equals (V – 1), the algorithm stops here.
Dijkstra’s shortest path algorithm
Dijkstra’s algorithm is very similar to Prim’s algorithm for minimum spanning tree. Like
Prim’s MST, we generate a SPT (shortest path tree) with a given source as a root. We maintain
two sets, one set contains vertices included in the shortest-path tree, other set includes
vertices not yet included in the shortest-path tree. At every step of the algorithm, we find a
vertex that is in the other set (set of not yet included) and has a minimum distance from the
source.
Algorithm
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in the
shortest-path tree, i.e., whose minimum distance from the source is calculated and finalized.
Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as
INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
a) Pick a vertex u which is not there in sptSet and has a minimum distance value.
b) Include u to sptSet.
c) Update distance value of all adjacent vertices of u. To update the distance values,
iterate through all adjacent vertices. For every adjacent vertex v, if the sum of distance
value of u (from source) and weight of edge u-v, is less than the distance value of v,
then update the distance value of v.

The set sptSet is initially empty and distances assigned to vertices are {0, INF, INF, INF, INF, INF,
INF, INF} where INF indicates infinite. Now pick the vertex with a minimum distance value. The
vertex 0 is picked, include it in sptSet. So sptSet becomes {0}. After including 0 to sptSet, update
distance values of its adjacent vertices. Adjacent vertices of 0 are 1 and 7. The distance values of
1 and 7 are updated as 4 and 8. The following subgraph shows vertices and their distance
values, only the vertices with finite distance values are shown. The vertices included in SPT are
shown in green colour.

Pick the vertex with minimum distance value and not


already included in SPT (not in sptSET). The vertex 1 is
picked and added to sptSet. So sptSet now becomes
{0, 1}. Update the distance values of adjacent vertices
of 1. The distance value of vertex 2 becomes 12.
Pick the vertex with minimum distance value and not
already included in SPT (not in sptSET). Vertex 7 is
picked. So sptSet now becomes {0, 1, 7}. Update the
distance values of adjacent vertices of 7. The distance
value of vertex 6 and 8 becomes finite (15 and 9
respectively).

Pick the vertex with minimum distance value and not


already included in SPT (not in sptSET). Vertex 6 is
picked. So sptSet now becomes {0, 1, 7, 6}. Update the
distance values of adjacent vertices of 6. The distance
value of vertex 5 and 8 are updated.

We repeat the above steps until sptSet includes all


vertices of the given graph. Finally, we get the following
Shortest Path Tree (SPT).

You might also like