What is the fastest algorithm for finding all shortest paths in a sparse graph?

In an unweighted, undirected graph with V vertices and E edges such that 2V>E, what is the fastest way to find all shortest paths in a graph? Can it be done in faster than Floyd-Warshall which is O(V3) but very fast per iteration?

How about if the graph is weighted?

Answer

Since this is an unweighted graph, you could run a Breadth First Search (BFS) from every vertex v in the graph. Each run of BFS gives you the shortest distances (and paths) from the starting vertex to every other vertex. Time complexity for one BFS is O(V+E)=O(V) since E=O(V) in your sparse graph. Running it V times gives you a O(V2) time complexity.

For a weighted directed graph, the Johnson’s algorithm as suggested by Yuval is the fastest for sparse graphs. It takes O(V2logV+VE) which in your case turns out to be O(V2logV). For a weighted undirected graph, you could either run Dijkstra’s algorithm from each node, or replace each undirected edge with two opposite directed edges and run the Johnson’s algorithm. Both these will give the same aysmptotic times as Johnson’s algorithm above for your sparse case. Also note that the BFS approach I mention above works for both directed and undirected graphs.

Attribution
Source : Link , Question Author : Jakob Weisblat , Answer Author : Juho

Leave a Comment