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
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.
Related
This is a review problem and I am trying to get a feel for whether my answer is right or not.
Here is the gist of the original question:
You have an MST, T, of a weighted undirected graph then a new edge is introduced in the original graph between nodes (u and v) to create a new graph G'. Give a linear time algorithm to determine if T is an MST of G'.
My answer:
The MST T of the original graph does not contain any cycles. There should be only one path from node u to node v. We can add the new edge into our MST which can be done in O(1) time to produce our new tree T'. Then, we can run DFS on T' from u to v which completes in O(|V| + |E|) time. With our new edge added, we should get at most 2 paths between u and v. One will use the new edge and one wont. We can compare these two paths in O(1) time. If the shorter of the two uses the new edge, then we know the original MST "T" is not a MST for the new graph G'. Our whole algorithm will complete in linear time.
It's a correct algorithm, and you've proved that if it finds a lighter tree, then the old tree wasn't minimum. You still need to prove that if it doesn't find a lighter tree, then the old tree is still the lightest.
I know that it can be updated in O(|V| + |E|) by using a DFS, but is it possible to update in O(|V|)?
Edit: If this isn't possible, how exactly would an O(|V| + |E|) algorithm work?
Updating Minimum Spanning Tree(MST) of a graph is not limited to connecting the new vertex to the existing MST. It should also be validated that the edges used in the initial MST are still the cheapest possible way to connect all the vertices. In some cases, vk may be such a node that it could overhaul the entire existing MST of a graph G. The new vertex and the edges that come along with it may form a cheaper way to connect the vertices of the graph, and consequently, some(or all) edges in the initial MST may become not the cheapest.
One such case can be constructed rather easily. Consider the initial graph G=(V={1,2,3}, E={(1, 2), (2, 3), (1, 3)}) in which each edge has the weight of 100. Then introduce a vertex 4, and edges from 4 to each other vertices, each with weight 1. The initial MST of G would have any two of the elements of E, but after the addition of the new node, the MST would be formed by entirely different edges.
Perhaps a clever data structure may yield you a better time complexity than O(|E|), but the need to check the validity(i.e. whether they are still the cheapest way of connecting all the vertices) of each edge of the initial MST as well as the newly added edges implies a time complexity dependent on the size of |E|. It is a valid idea that an initial MST may provide useful information that could help extend it to cover a new vertex with lesser time complexity. However, the definition of MST relies heavily on the graph itself, and updating the graph can possibly invalidate all of the initial MST, rendering the information it indicates regarding the graph useless. As a result, although I haven't exactly provided a mathematical proof here, I do not think there is an O(|V|) algorithm to update an MST when a new node(and edges to it) is added to a graph.
I have a possible solution for the following question, but not sure if correct:
Assume we have already found a minimum spanning tree T for a weighted, undirected graph G = (V,E). We would like to be able to efficiently update T should G be altered slightly.
An edge is added to G to produce a new graph. Give an algorithm that uses T to find a minimum spanning tree for the new graph in O(|V|) time.
My algorithm:
for each vertex do
if smallest edge not in T then
replace this edge with existing edge in T
break
end if
end for
I do not have much experience in writing pseudocode, so my algorithm might be over-simplified or incorrect. Please correct me if I am wrong. Thanks!
Don't know if your algorithm is correct, but it doesn't seem O(|V|) at least, because getting the "smallest edge not in T" of a vertex cannot be done in O(1).
The most usual way to add an edge e=(u, v) into a MST T is:
Run a BFS in T from u to v to detect the edge with maximum value in that path. (O(|V|))
If that edge's weight is greater than the weight of the edge you're trying to add, remove that old edge and add the new one. Otherwise, do nothing, because the new edge would not improve the MST. (O(1)).
The rationale behind this solution is that simply adding the edge into T would create exactly one cycle, and to restore the MST property you have to remove the edge with maximum value from that cycle.
Yes, your algorithm seems to be correct. I would amend your pseudocode to clarify that "if smallest incident edge not in T then" so that your new algorithm is this:
for each vertex do
if smallest incident edge not in T then
replace this edge with existing edge in T
break
end if
end for
Proof by contradiction:
There exist two cases: the new edge is either in the MST or it isn't. For the case that it is: Suppose that the algorithm does not replace an edge in T. This must mean that all edges in T are smaller than other edges that are incident on the same vertex. This is a contradiction because that means the new edge is not in the MST of T.
The proof for the other case is a trivially similar proof by contradiction.
We know the original graph and the original MST. Now we change an edge's weight in the graph. Beside the Prim and the Kruskal, is there any way we can generate the new MST from the old one?
Here's how I would do it:
If the changed edge is in the original MST:
If its weight was reduced, then of course it must be in the new MST.
If its weight was increased, then delete it from the original MST and look for the lowest-weight edge that connects the two subtrees that remain (this could select the original edge again). This can be done efficiently in a Kruskal-like way by building up a disjoint-set data structure to hold the two subtrees, and sorting the remaining edges by weight: select the first one with endpoints in different sets. If you know a way of quickly deleting an edge from a disjoint-set data structure, and you built the original MST using Kruskal's algorithm, then you can avoid recomputing them here.
Otherwise:
If its weight was increased, then of course it will remain outside the MST.
If its weight was reduced, add it to the original MST. This will create a cycle. Scan the cycle, looking for the heaviest edge (this could select the original edge again). Delete this edge. If you will be performing many edge mutations, cycle-finding may be sped up by calculating all-pairs shortest paths using the Floyd-Warshall algorithm. You can then find all edges in the cycle by initially leaving the new edge out, and looking for the shortest path in the MST between its two endpoints (there will be exactly one such path).
Besides linear-time algorithm, proposed by j_random_hacker, you can find a sub-linear algorithm in this book: "Handbook of Data Structures and Applications" (Chapter 36) or in these papers: Dynamic graphs, Maintaining Minimum Spanning Trees in Dynamic Graphs.
You can change the problem a little while the result is same.
Get the structure of the original MST, run DFS from each vertice and
you can get the maximum weighted edge in the tree-path between each
vertice-pair. The complexity of this step is O(N ^ 2)
Instead of changing one edge's weight to w, we can assume we are
adding a new edge (u,v) into the original MST whose weight is w. The
adding edge would make a loop on the tree and we should cut one edge
on the loop to generate a new MST. It's obviously that we can only
compare the adding edge with the maximum edge in the path(a,b), The
complexity of this step is O(1)
Devise an algorithm that takes a weighted graph G and finds the
smallest change in the cost to a non-MST edge that would cause a change in the
minimum spanning tree of G.
My solution so far (need suggestions):
To make a change to the MST, we need to change the weight of a non-MST edge s.t. it is one less than the maximum edge in the path of its start vertex and end vertex in the MST.
So we can start by walking the edges of MST, and for every vertex, check if there is a non-MST edge. If there is, a bfs to reach the edge's end point (in the MST) can be done. The non-MST edge weight must be updated to one less than the maximum edge weight in the path.
This would cause the non-MST edge to be included in the MST and the previous maximum edge to be removed from MST.
Can someone tell if this solution is correct ? Thanks.
You found the idea.
However, your answer needs to be tuned to show that you want to find the minimum change and not that you want to modify each non-MST edge you come across in your walk.
If this is a school question, you will also be asked to provide a proof of corectness. in order to build it, I would suggest to rely on Kruskal's proof, and to explain why your change would have Kruskal choose the modified edge instead of that other max-weight edge from the path.
I have an idea. So basically, we can follow the idea of the Kruskal algorithm. So if we want the MST to change, then there must be one time when the Krukal algorithm doen't choose the edge in the original MST. That edge must be the edge whose cost we are going to modify. So the algorithm is pretty clear. Follow the Kruskal algorithm, every time when we want to select a new edge e, we keep searching according to the Kruskal algorithm and find another edge e' that still doen't create a cycle. Then we calculate the minimum change in cost:w(e')-w(e)-1 .(I am not sure if the cost if limited to be an interger or not). Simply select the minimum change from above.