21

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

Peeber Burns
  • 211
  • 1
  • 2
  • 3

2 Answers2

47

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

Jarosław Gomułka
  • 4,727
  • 16
  • 24
  • 2
    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. – Ashish Negi Apr 11 '13 at 06:53
  • 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) – Olshansk Apr 26 '13 at 22:49
  • 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. – Michael Dotson Feb 26 '14 at 17:16
  • @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 Θ (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. – A T - student Jul 11 '17 at 06:32
  • Intuitively, I understand cases 1 and 4, but can somebody help me by giving a formal proof of them please? – James Lawson Jul 27 '17 at 12:14
  • @ 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. – KGhatak Jun 29 '19 at 04:13
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.

KCH
  • 2,648
  • 2
  • 19
  • 22