Open In App

Comparison between Shortest Path Algorithms:

Last Updated : 13 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Finding the shortest way is becoming more important in a world where time and distance matter. There are multiple shorted path algorithms exists. Therefore, in this article, we will compare the shortest path algorithms on the basis of their complexity and their performance, which will help us to use the correct algorithm according to our requirements.

The shortest path algorithms are the ones that focuses on calculating the minimum travelling cost from source node to destination node of a graph in optimal time and space complexities.

In this article we’re focusing on the differences between shortest path algorithms that are:

  1. Depth-First Search (DFS)
  2. Breadth-First Search (BFS)
  3. Multi-Source BFS
  4. Dijkstra’s algorithm
  5. Bellman-Ford algorithm
  6. Floyd-Warshall algorithm
  7. A* search algorithm
  8. Johnson’s algorithm

Complexity Differences Between the Shortest Path Algorithms:

Here V and E, denote the number of vertices and the number of Edges in the graph respectively.

Algorithm

Time Complexity

Space Complexity

DFS

O(V + E)

O(V)

BFS

O(V+E)

O(V)

MultiSource BFS

O(V+E)

O(V)

Dijkstra’s algorithm

O(E*log(V))

O(V)

Bellman-Ford algorithm

O(V*E)

O(V)

Floyd-Warshall algorithm

O(V^3)

O(V^2)

A* search algorithm

O(E*log(V))

O(V)

Johnson’s algorithm

O(V^2 * log V + V*E)

O(V^2)

Differences Between the Shortest Path Algorithms based on their Working:

1. Depth First Search as a Shortest Path Algorithm:

  • DFS is a graph traversal technique that starts at the root node and explores as far as possible along each branch before backtracking.
  • Unlike other algorithms like Dijkstra’s or A* search, DFS does not guarantee finding the shortest path. It might find a path, but it could be longer than other potential routes.
  • While it may not be ideal for finding the shortest path, DFS has applications in other areas such as topological sorting, cycle detection, and solving puzzles like mazes.
  • DFS can get stuck in infinite loops if not carefully implemented or if the graph is cyclic. To avoid this, cycle detection mechanisms are often needed.
  • DFS is commonly used when the goal is not to find the shortest path, but to explore or traverse a graph or tree in a systematic way.
  • Has very limited use as compared to other shortest path algorithms.

2. BFS and Multi-Source BFS as a Shortest Path Algorithm:

  • Uses Queue data Structure.
  • In unweighted graphs, BFS and Multi-Source BFS can find the shortest path from a source node to all other reachable nodes.
  • When used on an unweighted graph, Number of edges between source node and destination nodes act the distance between them.

3. Dijkstra’s Algorithm:

  • It works on Non-Negative Weighted graphs.
  • It follows Greedy Approach
  • It is a single source shortest path algorithm
  • Uses BFS to solve.
  • Lesser overheads than Bellman-Ford

4. Bellman-Ford Algorithm:

  • It works for all types of graphs given that negative cycles does not exist in that graph.
  • It follows DP approach.
  • It is a single source shortest path algorithm.
  • Bellman Ford’s Algorithm have more overheads than Dijkstra’s Algorithm.

5. Floyd-Warshall Algorithm:

  • It works for all types of graphs given that negative cycles does not exist in that graph.
  • It follows DP approach.
  • It is an all pair shortest path algorithm
  • This algorithm takes most time and space among all other algorithms.

6. A* search Algorithm:

  • Unlike other algorithms, this one is considered to have “brains”.
  • Many web-based maps use this algorithm to find the shortest path very efficiently (approximation).
  • A* is well-suited for finding the shortest path from a starting node to a goal node in a weighted graph or grid. It is especially efficient when you have a good heuristic that estimates the cost from the current node to the goal (admissible and consistent heuristic).
  • A* is optimal, meaning it guarantees finding the shortest path if the heuristic is admissible (never overestimates the true cost).

7. Johnson’s Algorithm:

  • It is an all pair shortest path algorithm which performs better than Floyd-Warshall algorithm.
  • It works for all types of graphs given that negative cycles does not exist in that graph.
  • It is Efficient for Sparse graphs.

Related Articles:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads