Open In App

Spanning Tree

In this article, we are going to cover one of the most commonly asked DSA topic which is the Spanning Tree with its definition, properties, and applications. Moreover, we will explore the Minimum Spanning Tree and various algorithms used to construct it. These algorithms have various pros and cons over each other depending on the use case of the problem.

What is a Spanning Tree?

A spanning tree is a subset of Graph G, such that all the vertices are connected using minimum possible number of edges. Hence, a spanning tree does not have cycles and a graph may have more than one spanning tree.



Properties of a Spanning Tree:

Real World Applications of A Spanning Tree:

Minimum Spanning Tree(MST):

The weight of a spanning tree is determined by the sum of weight of all the edge involved in it.

A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among all the possible spanning trees.



Properties of Minimum Spanning Tree:

Minimum Spanning Tree of a Graph may not be Unique:

Like a spanning tree, there can also be many possible MSTs for a graph as shown in the below image:

Algorithms to Find Minimum Spanning Tree of a Graph:

There are several algorithms to find the minimum spanning tree from a given graph, some of them are listed below:

  1. Krushkal’s MST Algorithm
  2. Prim’s MST Algorithm
  3. Boruvka’s Algorithm
  4. Reverse-Delete Algorithm

Let us discuss these algorithm one by one.

1. Krushkal’s Minimum Spanning Tree:

Kruskal’s Minimum Spanning Tree (MST) algorithm is to connect all the vertices of a graph with the minimum total edge weight while avoiding cycles. This algorithm employs a greedy approach, meaning it makes locally optimal choices at each step to achieve a globally optimal solution.

Algorithm:

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.

2. Prim’s Minimum Spanning Tree:

Minimum Spanning Tree (MST) is to build the MST incrementally by starting with a single vertex and gradually extending the tree by adding the closest neighbouring vertex at each step.

Algorithm:

To efficiently select the minimum weight edge for each iteration, this algorithm uses priority_queue to store the vertices sorted by their minimum edge weight currently. It also simultaneously keeps track of the MST using an array or other data structure suitable considering the data type it is storing.

This algorithm can be used in various scenarios such as image segmentation based on color, texture, or other features. For Routing, as in finding the shortest path between two points for a delivery truck to follow.

3. Boruvka’s Algorithm:

This is also a graph traversal algorithm used to find the minimum spanning tree of a connected, undirected graph. This is one of the oldest algorithms. The algorithm works by iteratively building the minimum spanning tree, starting with each vertex in the graph as its own tree. In each iteration, the algorithm finds the cheapest edge that connects a tree to another tree, and adds that edge to the minimum spanning tree. This is almost similar to the Prim’s algorithm for finding the minimum spanning tree.

Algorithm:

The algorithm can be implemented using a data structure such as a priority queue to efficiently find the cheapest edge between trees. Boruvka’s algorithm is a simple and easy-to-implement algorithm for finding minimum spanning trees, but it may not be as efficient as other algorithms for large graphs with many edges.

4. Reverse-Delete Algorithm:

Reverse Delete algorithm is closely related to Kruskal’s algorithm. In Kruskal’s algorithm what we do is : Sort edges by increasing order of their weights. After sorting, we one by one pick edges in increasing order. We include current picked edge if by including this in spanning tree not form any cycle until there are V-1 edges in spanning tree, where V = number of vertices.

In Reverse Delete algorithm, we sort all edges in decreasing order of their weights. After sorting, we one by one pick edges in decreasing order. We include current picked edge if excluding current edge causes disconnection in current graph. The main idea is delete edge if its deletion does not lead to disconnection of graph.

Algorithm:

  1. Sort all edges of graph in non-increasing order of edge weights.
  2. Initialize MST as original graph and remove extra edges using step 3.
  3. Pick highest weight edge from remaining edges and check if deleting the edge disconnects the graph or not.
  4. If disconnects, then we don’t delete the edge.
  5. Else we delete the edge and continue.

Real World Application of A Minimum Spanning Tree:


Article Tags :