Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which algorithm can I use to find the next to shortest path in a graph?

Tags:

I want to find the next shortest path between 2 vertices in a graph and the path has a positive cost.The next shortest path is allowed to share edges of the shortest path .Which algorithm can I use?

like image 695
Manish Basdeo Avatar asked Feb 11 '11 17:02

Manish Basdeo


People also ask

Which algorithm will you use to determine the shortest path between the nodes?

Dijkstra's algorithm can be used to determine the shortest path from one node in a graph to every other node within the same graph data structure, provided that the nodes are reachable from the starting node. Dijkstra's algorithm can be used to find the shortest path.

Which algorithm can be used to find the shortest path between every pair?

The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to find all pair shortest path problem from a given weighted graph. As a result of this algorithm, it will generate a matrix, which will represent the minimum distance from any node to all other nodes in the graph.

Which algorithm will you use to determine the shortest path between the nodes in a graph when there are no negative cost edges?

If there are no negative weight cycles, then we can solve in O(E + VLogV) time using Dijkstra's algorithm. Since the graph is unweighted, we can solve this problem in O(V + E) time.


2 Answers

Use the K-shortest path algorithm, where k=2 for you, some sample reference:

Finding the k shortest paths. D. Eppstein. 35th IEEE Symp. Foundations of Comp. Sci., Santa Fe, 1994, pp. 154-165. Tech. Rep. 94-26, ICS, UCI, 1994. SIAM J. Computing 28(2):652-673, 1998.

http://www.ics.uci.edu/~eppstein/pubs/Epp-TR-94-26.pdf

like image 152
Wei Avatar answered Oct 21 '22 10:10

Wei


I doubt this is optimal in terms of running time but:

  1. Use Dijkstra's algorithm on graph G to get path P
  2. For all edges E in path P:
  3. -- If G - E is not connected, continue for next edge (go to 2)
  4. -- Use Dijkstra's algorithm on G - E to find path X_i
  5. -- If length of current X_i is shorter than all other X_i, save it
  6. The X_i at the end of the for loop is the second shortest path

The second-shortest path can't go through all edges in P, but it could go through all but one of them, potentially. I assume by "second-shortest" that you don't use edges more than once, otherwise the second-shortest path could contain P.

like image 28
tkerwin Avatar answered Oct 21 '22 09:10

tkerwin