views:

155

answers:

3

Hello. I'm writing an algorithm for finding the second min cost spanning tree. my idea was as follows:

  1. Use kruskals to find lowest MST.
  2. Delete the lowest cost edge of the MST.
  3. Run kruskals again on the entire graph.
  4. return the new MST.

My question is: Will this work? Is there a better way perhaps to do this?

+2  A: 

Consider this case:

------100----
|           |
A--1--B--3--C
      |     |
      |     3
      |     |
      2-----D

The MST consists of A-B-D-C (cost 6). The second min cost is A-B-C-D (cost 7). If you delete the lowest cost edge, you will get A-C-B-D (cost 105) instead.

So your idea will not work. I have no better idea though...

Anders Abel
+2  A: 

You can do this -- try removing the edges of the MST, one at a time from the graph, and run the MST, taking the min from it. So this is similar to yours, except for iterative:

  1. Use Kruskals to find MST.
  2. For each edge in MST:
    1. Remove edge from graph
    2. Calculate MST' on MST
    3. Keep track of smallest MST
    4. Add edge back to graph
  3. Return the smallest MST.
Larry
Thank you. I have drawn out some examples and this idea appears to work for all my examples. :)
Evil
This works - but it is an extensive search that will take O(N^2*logN) compared to the O(NlogN) complexity of Kruskal.
Anders Abel
It's just a way of saying that the MST and the MST_2 is different by exactly one edge -- there are better algorithms than this, but it is a way to think about it. There's also O(VE) algorithm -- Kruskal is actually O(V log E), making the above O(V^2 log E).
Larry
Kruskal's is actually O(E log E) because you are sorting the edges - ignoring disjoint sets, which is basically constant if done right. This algorithm is therefore O(VE log E) since you have O(V) edges in the MST. So the worst case is O(V^3 log V). (log E = log V^2 = 2log V = O(log V)).
IVlad
You're right. Generally for graph problems, I leave in the E's, since log E and log V is a constant difference, but it might point to different algorithms. I've been getting downvotes for giving too much away in my answer and I got downvotes for not giving away answer, but I do know this is not optimal!
Larry
An additional note, if the edges are already sorted, you don't need to sort again for the subsequent kruskal's, hence the complexity. You just need a linear (ish) union find all the edges in MST minus an edge. Then you can linearly walk the sorted thing anyhow.
Larry
I'm just saying :). Personally I'm against downvotes for giving too much away, I only downvote if the solution is wrong or not what the asker is looking for.
IVlad
@your additional note: true, but you still don't get O(V^2 log E). Initial Kruskal is O(E log E + E). Then you have O(V) recalculations without sorting, so O(VE) and O(V^3) worst case (O(E log E + VE) to be exact).
IVlad
Oh I agree, now I don't know what I was thinking, I think I was pointing out the O(VE). =)
Larry
+1  A: 

You can do it in O(V2). First compute the MST using Prim's algorithm (can be done in O(V2)).

Compute max[u, v] = the cost of the maximum cost edge on the (unique) path from u to v in the MST. Can be done in O(V2).

Find an edge (u, v) that's NOT part of the MST that minimizes abs(max[u, v] - weight(u, v)). Can be done in O(E) == O(V2).

Return MST' = MST - {the edge that has max[u, v] weight} + {(u, v)}, which will give you the second best MST.

Here's a link to pseudocode and more detailed explanations.

IVlad