I've come across to this problem on UVa. It basically asks for second shortest path. I've looked it up on the internet, but I couldn't find any practical implementation of it. Are there any good tutorial on this topic?
Note: I'm asking about both SSP and APSP. Also, is second shortest path simpler than more general kth shortest path algorithms in terms of complexity?
UPD: Thank you really much for your help, I've solved the problem! But the thing is nobody has mentioned any algorithm for All-Pair Second Shortest Path problem yet. Can someone who is knowledgeable about this problem explain it?
if there is another shortest path will it be the second shortest path?
No, its distance should be higher for this problem.
Ok u do a dijkstra after that for every edge if its incident vertices are u,v and the start and end are a and b u check this if(dis[a][u] + weight[u][v] + dis[v][b] != shortest && same thing < second_shortest) second_shortest = that thing uneed a dijkstra for a and a dijkstra for b
Is this solution correct? I have implemented it for 3255 roadblocks POJ , but at two test cases answers are different, Are you sure?
For those who gave me negative , please write correctness proof of this , I couldn't figure out .
http://en.wikipedia.org/wiki/Yen's_algorithm
How about this?
Thank you! The thing is these implementations are more kind of a general and real life implementations. Is there any shorter implementation in competitive programming paradigm? Also, what about for APSP?
Can you post the statement because I can't open UVa now, please? Is the graph directed? Can the path contain cycles?
PS: Am I the only one who cannot open UVa?
it's a common problem on UVA ... just clear your cache or open in private (incognito) mode
I did.
Hello again! I just got accepted, let me explain my idea not only for the second but for the K-th shortest path in a graph:
We are going to use a modified Dijkstra's algorithm. We will store vectors for each node containing the distances(instead of an array dist[i] for each node i).
Assume that we are using the standard Dijkstra's algorithm implemented with a priority queue. We will use this structure for the queue:
At each step we take the element on the top of the queue. We will push the current distance in the vector in two cases:
1) If the vector with the distances is empty
or
2) If the vector has one element inside and the current distance is greater than the first:
Then we go through curr.vertex's children. If the current children has already have two elements in its vector, then we skip it. Otherwise, we find the current distance to reach it from curr.vertex and push it in the queue.
My code is here: http://ideone.com/QpWFnR
I got it! Thank you really much! It seems like we can't use this idea to Floyd-Warshall, can we?
UPD: Is this algorithm's complexity O(k*(V+E)*logV) using binary heap?
I don't know if Floyd-Warshall can be used since its idea of finding the shortest path differs from the Dijkstra's idea.
I think that one run of the modified Dijkstra's algorithm has complexity O(K*(V*logV + E)).
this is similar problem http://poj.org/problem?id=3255 http://ideone.com/0FtdBa this is my code with dijkstra
Thank you very much, I've been looking for this for 21 months!
Thanks Bro....Really helped
Thanks a lot
All-pair shortest path can be done running N times Dijkstra's algorithm. Then all-pair second shortest paths can be done running N times the modified Dijkstra's algorithms. The complexity is O(2*(V*logV + E)) = O(V*logV + E) per run which is the same as the normal Dijkstra.
Is that what are you asking?
Thank you again for your fast response.
I think O(V*k*(V*logV + E)) is correct for fibonacci heap. Its complexity becomes O(V*k*(V+E)*logV) = O(k*V^3*logV) when E = V^2 and using binary heap. It also doesn't work on a graph with negative weights. What I'm asking for is something like Floyd-Warshall which can work on a graph with negative edges weights, negative cycles and also something with a complexity of O(k*V^3) or something similar.