Given a connected undirected weighted graph with N nodes and M edges. The task is to perform given queries and find the weight of the minimum spanning tree. Queries are of three types:
- query(1) -> Find the weight of the minimum spanning tree.
- query(2, x, y) -> Change the weight of the edge between the nodes x and y to 0.
- query(3, x, y) -> Restore the weight of the edge between the nodes x and y to its original weight.
query(2, 1, 2),
query(3, 1, 2),
query(2, 3, 4),
Approach: Let’s first compute MST of the initial graph before performing any queries and let T be this MST. The crucial observation is that at any point while handling the queries, the weight of the MST of the current graph can be computed by running Kruskal’s algorithm on edges with zero weight at this point and edges of T. So, keep edges with weight zero in a data structure and after query of type 2 and type 3 compute weight of minimum spanning tree.
Below is the implementation of the above approach:
- Kruskal's Minimum Spanning Tree using STL in C++
- Minimum Product Spanning Tree
- Boruvka's algorithm for Minimum Spanning Tree
- Applications of Minimum Spanning Tree Problem
- Minimum spanning tree cost of given Graphs
- Reverse Delete Algorithm for Minimum Spanning Tree
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Find minimum weight cycle in an undirected graph
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Count the nodes in the given tree whose weight is even
- Number of Paths of Weight W in a K-ary tree
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Count the nodes in the given tree whose sum of digits of weight is odd
- Count the nodes of the given tree whose weight has X as a factor
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.