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 inO(1)
.The most usual way to add an edge
e=(u, v)
into a MSTT
is:T
fromu
tov
to detect the edge with maximum value in that path. (O(|V|)
)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.