Given an undirected weighted complete graph of N vertices. There are exactly M edges having weight 1 and rest all the possible edges have weight 0. The array arr gives the set of edges having weight 1. The task is to calculate the total weight of the minimum spanning tree of this graph.
- Store the given graph in the map for all the edges of weight 1.
- Use set to store the vertices which are not included in any of the 0-weight Connected Components.
- For each vertex currently stored in the set, do a DFS Traversal and increase the count of Components by 1 and remove all the visited vertices during DFS Traversal from the set.
- During the DFS Traversal, include the 0-weight vertices in a vector and 1-weight vertices in another set. Run a DFS Traversal for all the vertices included in the vector.
- Then, the total weight of the minimum spanning tree is given the count of components – 1.
Below is the implementation of the above approach:
Time Complexity :O(N*log N + M) where N is the number of vertices and M is the number of edges.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find minimum weight cycle in an undirected graph
- Program to find total number of edges in a Complete Graph
- Check if there is a cycle with odd weight sum in an undirected graph
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- k'th heaviest adjacent node in a graph where each vertex has weight
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Proof that Dominant Set of a Graph is NP-Complete
- Proof that Independent Set in Graph theory is NP Complete
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Find the weight of the minimum spanning tree
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Find the maximum value permutation of a graph
- Find a Mother Vertex in a Graph
- Find k-cores of an undirected graph
- Find the Degree of a Particular vertex in a Graph
- Find all cliques of size K in an undirected graph
- Find dependencies of each Vertex in a Directed Graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.