VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-implement-minimum-spanning-tree/

⇱ C Program to Implement Minimum Spanning Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Implement Minimum Spanning Tree

Last Updated : 24 May, 2024

A Minimum Spanning Tree is a subset of edges from a undirected graph that connects all vertices with minimum total weight and contains no cycle. The most common algorithms to generate Minimum Spanning Tree are Kruskal's algorithm and Prim's algorithm. In this article we explain about implement Minimum Spanning Tree with Kruskal's algorithm. You can also use Prim's algorithm based on your requirement.

What is Minimum Spanning Tree in C?

Minimum Spanning Tree in C can be represented in different ways, depending on the algorithm used. Here we use Kruskal's Algorithm to Implement Minimum Spanning Tree.

  • Begins with all edges sorted by weight and iterates through them, adding an edge to the Minimum Spanning Tree if it does not create a cycle.
  • And Requires an edge list and a Disjoint Set Union structure to track connected components in the graph.

Minimum Spanning Tree has V-1 edges where V is the number of vertices in Graph.

Example

Here we take one example to explain how Implement Minimum Spanning Tree by using the Kruskal's algorithm by calculating minimum total weights without a cycle.

👁 Producer
MST

Explanation

The graph has 4 vertices (0, 1, 2, 3) and 5 edges with the following weights

  • Edge from vertex 0 to 1 with a weight of 10.
  • Edge from vertex 0 to 2 with a weight of 6.
  • Edge from vertex 0 to 3 with a weight of 5.
  • Edge from vertex 1 to 3 with a weight of 15.
  • Edge from vertex 2 to 3 with a weight of 4.

Minimum Spanning Tree of the Above Graph

Kruskal's algorithm sorts the edges by weight, then iterates through the edges to include them in the MST if they don't create a cycle.

  • Edge from vertex 2 to 3 with a weight of 4
  • Edge from vertex 0 to 3 with a weight of 5
  • Edge from vertex 0 to 1 with a weight of 10

These edges connect all four vertices without creating cycles, forming the Minimum Spanning Tree with the minimum total weight 19.

C Program to Implement Minimum Spanning Tree


Output
Edges in the Minimum Spanning Tree:
2 -- 3 : 4
0 -- 3 : 5
0 -- 1 : 10

Time Complexity: O(nlog n)

Space Complexity: O(n)

Applications of MST

  • In Networking design mostly The Minimum Spanning Tree is used
  • For identifying natural clustering in Graph .
  • Used in approximation algorithms for problems like Travelling Salesman Problem.
  • Constructing efficient road or pipelines network with minimal construction costs.

Conclusion

Prim's Algorithms is an effective for creating or implementing Minimum Spanning Tree. Kruskal's Algorithm is typically chosen when edges are already given, and sorting them is efficient. It offers time and space complexity that is well suited for large scale graphs with applications in network designing, clustering, And other optimization problems.


Comment
Article Tags: