Open In App

Spanning Tree

Last Updated : 13 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

spanningtreedrawio

Properties of a Spanning Tree:

  • A Spanning tree does not exist for a disconnected graph.
  • For a connected graph having N vertices then the number of edges in the spanning tree for that graph will be N-1.
  • A Spanning tree does not have any cycle.
  • We can construct a spanning tree for a complete graph by removing E-N+1 edges, where E is the number of Edges and N is the number of vertices.
  • Cayley’s Formula: It states that the number of spanning trees in a complete graph with N vertices is N^{N-2}
    • For example: N=4, then maximum number of spanning tree possible =4^{4-2}       = 16 (shown in the above image).

Real World Applications of A Spanning Tree:

  • Several path finding algorithms, such as Dijkstra’s algorithm and A* search algorithm, internally build a spanning tree as an intermediate step.
  • Building Telecommunication Network.
  • Image Segmentation to break an image into distinguishable components.
  • Computer Network Routing Protocol

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:

  • A minimum spanning tree connects all the vertices in the graph, ensuring that there is a path between any pair of nodes.
  • An MST is acyclic, meaning it contains no cycles. This property ensures that it remains a tree and not a graph with loops.
  • An MST with V vertices (where V is the number of vertices in the original graph) will have exactly V – 1 edges, where V is the number of vertices.
  • An MST is optimal for minimizing the total edge weight, but it may not necessarily be unique.
  • The cut property states that if you take any cut (a partition of the vertices into two sets) in the original graph and consider the minimum-weight edge that crosses the cut, that edge is part of the MST.

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:

MSTdrawio

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:

  • First, it sorts all the edges of the graph by their weights,
  • Then starts the iterations of finding the spanning tree.
  • At each iteration, the algorithm adds the next lowest-weight edge one by one, such that the edges picked until now does not form a cycle.

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:

  • It starts by selecting an arbitrary vertex and then adding it to the MST.
  • Then, it repeatedly checks for the minimum edge weight that connects one vertex of MST to another vertex that is not yet in the MST.
  • This process is continued until all the vertices are included in the MST.

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:

  • Initialize a forest of trees, with each vertex in the graph as its own tree.
  • For each tree in the forest:
    • Find the cheapest edge that connects it to another tree. Add these edges to the minimum spanning tree.
    • Update the forest by merging the trees connected by the added edges.
  • Repeat the above steps until the forest contains only one tree, which is the minimum spanning tree.

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:

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads