2. Let's start with Spanning Tree:
A spanning tree is defined as a tree-like subgraph of a connected, undirected graph that includes all
the vertices of the graph.it is a subset of the edges of the graph that forms a tree (acyclic) where
every node of the graph is a part of the tree.
What is minimum Spanning tree?
The minimum spanning tree has all the properties of a spanning tree with an added constraint of
having the minimum possible weights among all possible spanning trees. Like a spanning tree,
there can also be many possible MSTs for a graph.
3. Properties of a
Spanning Tree:
The spanning tree holds the below-mentioned principles:
The number of vertices (V) in the graph and the spanning tree is the same.
There is a fixed number of edges in the spanning tree which is equal to one less than the
total number of vertices ( E = V-1 ).
The spanning tree should not be disconnected, as in there should only be a single source of
component, not more than that.
The spanning tree should be acyclic, which means there would not be any cycle in the tree.
The total cost (or weight) of the spanning tree is defined as the sum of the edge weights of all
the edges of the spanning tree.
There can be many possible spanning trees for a graph.
4. There are two famous algorithms for finding the Minimum Spanning
Tree:
5. 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.
Sort the graph edges with respect to their weights.
Start adding edges to the MST from the edge with the smallest weight until the edge of the largest weight.
Only add edges which doesn't form a cycle , edges which connect only disconnected components.
This algorithm can be implemented efficiently using a DSU ( Disjoint-Set ) data structure to keep track of
the connected components of the graph. This is used in a variety of practical applications such as network
design, clustering, and data analysis.
6.
7. 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. Unlike an edge in Kruskal's, 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.
8.
9. Time Complexity:
Primes algorithm:
The time complexity of the Prim’s Algorithm is
O((V+E)log(V)). because each edge is inserted in the
priority queue only once and insertion in priority
queue take logarithmic time.
Kruskal’s algorithm:
In Kruskal’s algorithm, most time consuming
operation is sorting because the total complexity of
the Disjoint-Set operations will be O(Elog(V)).
10. Application :
1. Network design: Spanning trees can be used in
network design to find the minimum number of
connections required to connect all nodes.
Minimum spanning trees, in particular, can help
minimize the cost of the connections by selecting
the cheapest edges.
2. Image processing: Spanning trees can be used
in image processing to identify regions of similar
intensity or color, which can be useful for
segmentation and classification tasks.
3. Social network analysis: Spanning trees and
minimum spanning trees can be used in social
network analysis to identify important
connections and relationships among individuals
or groups.