Shortest Distance Graph Algorithms - How do they differ?

The shortest distance graph algorithms can be tricky. Which algorithm should be used when, whether it can work for negative edges or cycles, which one is the most efficient for the given problem - you should be asking these questions before deciding on a Shortest Distance Algorithms.

The implementation and pseudo code for the standard Shortest Distance Algorithms are easily available on the web but how these different algorithms differ is often not highlighted. In this post, I will focus on how different graph algorithms differ from each other.

These different algorithms differ in term of -
  • Single Source Shortest Path vs All Pair Shortest Path Algorithms. Single Source Shortest Path algorithm calculates the shortest distance from a given Source vertex to all other graph vertices. All Pair Shortest Distance algorithm calculates the shortest distance between all pairs of graph vertices.
  • Weighted graph - Can the algorithm work if edges have weights?
  • Negative weight edges - Will the algorithm work if there are negative weight edges?
  • Negative weight cycles - The shortest distance problem is not defined for a graph with a negative weight cycle. Because you can keep on going around that cycle and distance with keep on getting smaller. So no algorithms will work in this case.(Update - as pointed out on Reddit, although Shortest Distance problem is not well defined if there are negative weight cycles, it is slightly incorrect to say that algorithms do not work in this case. e.g. Bellman Ford algorithm can be used to detect negative weight cycles in graphs). 
  • Space and Time Complexity
Now let's look at the standard algorithms from these aspects.

Breadth First Search

  • Single Source Shortest Path algorithm
  • Does not work for weighted graphs. All edges should have equal or unit weight.
  • Does not work for Negative weight edges 
  • Time Complexity - O(E + V)
  • Space Complexity - O(V)

Dijkstra 

  • Single Source Shortest Path algorithm
  • Works well for Weighted graphs
  • Does not work for Negative weight edges 
  • Time Complexity -  O(E log(V) +  V log(V)) using standard heaps, O(E + V log(V)) using Fibonacci Heaps. 
  • Space Complexity - O(V)
  • Single Source Shortest Distance algorithm
  • Negative Edges - Yes
  • Time Complexity - O(EV)
  • Space Complexity - O(V)

Shortest Distance for Directed Acyclic Graphs(DAG)

Apparently if the graph is of type DAG (Directed Acyclic Graph), then there is another algorithm with better time complexity. It works for all cases where Bellman Ford works plus it has better time complexity. 
  • Single Source Shortest Distance algorithm
  • Negative Edges - Yes
  • Time Complexity - O(E + V)
  • Space Complexity - O(V)

Floyd Warshall 

  • All pairs shortest path algorithm
  • Works well for Weighted graphs
  • Works well with negative weight edges
  • Time Complexity - O(V^3)
  • Space Complexity - O(V^2)

Now let's try to use these differences to solve a problem. I was asked this problem in an interview and it is a very good problem to test understanding of different graph algorithms. 

You've always been intrigued with the Six Degrees of Kevin Bacon game. Let's say if two actors have been in the same movie we call them 'friends' and if two actors have not been in the same movie, we say they are not 'friends'. Now choose any two actors at random -- we want to calculate the number of degrees of separation and the path between them. How do you go about this problem?
• Discuss your algorithm ideas. For each algorithm talk about the trade-offs.

Ok. Clearly this is a shortest distance problem. 
  • What are the weights of the edges? All edges have unit weight. So BFS can be used. 
  • Are there negative weight edges? No. All edges have weight 1. So Dijkstra can also be used. Between BFS and Dijkstra, BFS has better Time complexity O (E + V) vs O(E + V log(V) ) in Dijkstra. Space complexity is same for both O(V). Clearly between BFS and Dijkstra, BFS is better in this case. 
  • This problem is best modeled as an undirected graph and can contain cycles. So DAG algorithm will not work.
  • But what about Floyd Warshall Algorithm. So do we need an All Pair Shortest Path algorithm here? Floyd warshall has time complexity of O(V^3) and space complexity of O(V^2) which is worse than BFS. Here we need to understand the problem better. Do we need to perform the distance calculation for a single pair of actors or there can be many looks we need to perform. If we need to do a one-off lookup, then clearly BFS is a better solution. However if we need to perform many lookups over a long period, then it make better sense to just run Floyd Warshal once, calculate all the distances and store the results and later use the stored result for each lookup.
Hope this articles helps you understand different shortest distance algorithms better. Please comment if you have any questions or feedback. 


Comments

Popular posts from this blog

Prefer ThreadLocalRandom over Random

Java - One liner to Configure Logging