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:
- Minimum Product Spanning Tree
- Kruskal's Minimum Spanning Tree using STL in C++
- 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
- Product of minimum edge weight between all pairs of a Tree
- Find minimum weight cycle in an undirected graph
- Number of Paths of Weight W in a K-ary tree
- Count the nodes in the given tree whose weight is even
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Count the nodes in the given tree whose sum of digits of weight is odd
- Count the nodes in the given tree whose weight is a power of two
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.