Reputation: 11
Suppose that a graph G has a minimum spanning tree already computed. How can we quickly update the minimum tree if we add a new vertex and incident edges to G.
My initial solution is to choose the new added edge who has least weight and then add this edge to the tree already computed. But it seems this solution is wrong. Can someone give me some tips on this question? Thanks
Upvotes: 1
Views: 6577
Reputation: 1
There exist algorithms that can add a vertex with all its edges to a graph with a known MST in linear time, which is faster than Prim or Kruskal. Probably the most neat algorithm proposed in the paper below [1] uses just one cleverly designed DFS call. It is strange that people do not know this algorithm, although it can be explained by its date: it was proposed long before both Prim and Kruskal.
That algorithm is based on an observation that the new MST shall consist of edges that either belong to the old MST or are adjacent to the newly inserted vertex (let's denote it z as in the cited paper). Furthermore, it constructs the new MST recursively while performing a depth-first search on the old MST. So its running time is proportional to the number of edges in the old MST, plus the number of edges adjacent to z, hence O(n). The way it does it is detailed below.
Using Python as pseudocode, the algorithm can be written down as follows:
def update_mst(w, z):
visited(w) = True
t = edge(w, z)
for r in old_mst_adjacent(w):
if not visited(r):
tt = update_mst(r, z)
rw = edge(r, w)
if cost(rw) < cost(tt):
add_to_new_mst(rw)
t = min(t, tt)
else:
add_to_new_mst(tt)
t = min(t, rw)
return t
Note that [1] uses a global variable instead of returning the heaviest edge from the recursive call, but the presented way is, I think, somewhat cleaner to the modern programmers.
[1] Francis Chin and David Houck. 1978. Algorithms for Updating Minimal Spanning Trees. J. Comput. System Sci. 16 (1978), 333–344.
Upvotes: 0
Reputation: 503
Adding the minimum weight edge will give wrong results because now you have extra edges and more than one edge out of them can be a part of the new MST. See the image for example.
You can use Prim's algorithm. Just take into account the previous MST edges and the new edges while running the algorithm.
This will work because if you run Prims
on the whole new graph then also all the edges that it will add will be from old MST or new edges.
You can use any other MST
finding algorithm as well like Kruskal
considering the above said edges.
Upvotes: 1