I'm trying to find the second best minimum spanning three from a weighted unoriented graph. I know how to compute the MST using Kruskal algorithm, and I was thinking to find the second best minimum algorithm this way:
Steps:
Sort all the graph edges.
Compute the MST using Kruskal
Get the minimum weight edge from the graph that is not in the first MST and add it to the MST (now the MST has a cycle)
Remove the maximum weight edge in the new formed cycle
And this should be the second best MST right ?
By the way I know is a topic there pointing out an algorithm that iterates between each of the MST edges and runs Kruskal on the graph without the edge selected, I'm just asking if mine works.
MST=(a,b),(b,c),(c,d),(d,e)
with weightsw(a,b)=w(b,c)=1
andw(c,d)=w(d,e)=4
. Suppose edges(a,c)
and(c,e)
also exist with weightsw(a,c)=4
andw(c,e)=5
. Your algorithm will add(a,c)
and remove one of the edges with weight1
, but the correct way would be to add(c,e)
and remove one of the edges with weight4
. – Tony Tuttle