Update minimum spanning tree with modification of edge
Asked Answered
S

2

25

A graph (positive weight edges) with a MST If some edge, e is modified to a new value, what is the best way to update the MST without completely remaking it. I think this can be done in linear time. Also, it seems that I would need a different algorithm based on whether 1) e is already a part of the MST and 2) whether the new edge, e is larger or smaller than the original

Shaduf answered 29/3, 2012 at 20:58 Comment(0)
M
56

There are 4 cases:

  1. Edge is in MST and you decreasing value of edge:
    Current MST is still MST

  2. Edge is not in MST and you decreasing value of edge:
    Add this edge to the MST. Now you've got exactly 1 cycle.
    Based on cycle property in MST you need to find and remove edge with highest value that is on that cycle. You can do it using dfs or bfs. Complexity O(n).

  3. Edge is in MST and you increasing its value of edge:
    Remove this edge from MST. Now you have 2 connected components that should be connected. You can calculate both components in O(n) (bfs or dfs). You need to find edge with smallest value that connects these components. Iterate over edges in ascending order by their value. Complexity O(n).

  4. Edge is not in MST and you increasing its value of edge:
    Current MST is still MST

Magnanimity answered 29/3, 2012 at 22:59 Comment(6)
CASE 3. IS NOT O(N). to iterate over edges in ascending order. we need to sort them. there are O(n^2) edges. even if we are taking sorted edges that we calculated during making mst, it would still have to go through these (all may in worst case) edges.Mithraism
It could be O(n). 1. Remove the edge whose weight was increased and keep track of the two nodes that were connected by this edge 2. Run bfs/dfs starting with these two nodes which are now in 2 disjoint sets. You should somehow hash the vertices visited so you can access them in O(1). I would create two hashtables, one for each disjoint set. 3. Loop through all the edges in E-E' where G=(V,E) and MST=(V,E'). If any edge contains 1 node from each hashtable, it connects the two disjoint sets. Maintain a min variable to determine which edge connected the two sets and had the lowest weight. O(E)Bobbobb
Olshansk, O(E) is O(n^2), as Ashish pointed out. As far as I can tell, removal requires O(n^2), because for each edge (assume sorted already in a list), we need to find the smallest edge which connects the two spanning trees. This can take up to O(n^2) if the only edge that connects them is also the edge with the highest value.Venterea
@Michael - It is O(n). When the variable isn't specified, it refers to the size of the input to the algorithm, which, in this case, presumably includes the graph G. So ... n is &Theta; (V+E). In passing, O is an upper-bound ... f(n) is O(g(n)) means f(n)<=some multiple * g(n) (for large n). So saying a function f(n) is O(n^2) could mean that it is f(n)=1 or f(n)=n.Brazenfaced
Intuitively, I understand cases 1 and 4, but can somebody help me by giving a formal proof of them please?Shin
@ A T - Student - Michael seems to be correct, and I would request you to revisit your statement of n=theta (V + E) . Looks like you're mixing up the concept of independent variables in a function. V and E can vary independently, that's the reasons, unless specified as dense or the likes, V and E are both included for most functions for Graph. At lest give a thought.Cawnpore
A
1

My O(n) solution is based on assumption, that before you start modifying edges, you should find MST (is not given with the graph). To do so, you can use Kruskal algorithm which works in O(n log n), and as a side effect produces sorted list of edges. Its cost is dominated by sorting, so when you modify weight of an edge, you can delete it from sorted list in O(log n), and insert it back with new value, also in O(log n), and finally call Kruskal algorithm again, which now runs in linear time because edges are sorted.

This is a linear solution as you requested, but it looks like it can be done faster.

Angeliqueangelis answered 29/3, 2012 at 21:9 Comment(1)
Unfortunetly in Kruskal algorithm you need Union-find which isn't O(1) ;/Sixgun

© 2022 - 2024 — McMap. All rights reserved.