Comparison between Shortest Path Algorithms: Last Updated : 13 Oct, 2023 Comments Improve Suggest changes Like Article Like 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: Depth-First Search (DFS)Breadth-First Search (BFS)Multi-Source BFSDijkstra's algorithmBellman-Ford algorithmFloyd-Warshall algorithmA* search algorithmJohnson's algorithmComplexity 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 ApproachIt is a single source shortest path algorithmUses BFS to solve.Lesser overheads than Bellman-Ford4. 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 algorithmThis 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: Shortest Path PropertiesHow to find Shortest Paths from Source to all Vertices using Dijkstra’s AlgorithmShortest path in an unweighted graphShortest Path in Directed Acyclic GraphShortest paths from all vertices to a destination Comment More infoAdvertise with us Next Article Comparison between Shortest Path Algorithms: V vaibhav_gfg Follow Improve Article Tags : Graph DSA Dijkstra BFS DFS bellman-ford Floyd-Warshall +3 More Practice Tags : BFSDFSGraph Similar Reads Johnson's algorithm for All-pairs shortest paths The problem is to find the shortest paths between every pair of vertices in a given weighted directed Graph and weights may be negative. We have discussed Floyd Warshall Algorithm for this problem. The time complexity of the Floyd Warshall Algorithm is Î(V3). Using Johnson's algorithm, we can find a 15+ min read Applications of Dijkstra's shortest path algorithm Dijkstraâs algorithm is one of the most popular algorithms for solving many single-source shortest path problems having non-negative edge weight in the graphs i.e., it is to find the shortest distance between two vertices on a graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 4 min read Shortest alternate colored path Consider a directed graph of âNâ nodes where each node is labeled from â0â to âN - 1â. Each edge of the graph is either âredâ or âblueâ colored. The graph may contain self-edges or parallel edges. You are given two arrays, âredEdgesâ and âblueEdgesâ, whose each element is of the form [i, j], denotin 12 min read Dijkstra's shortest path algorithm in Python Given a graph and a source vertex in the graph, find the shortest paths from source to all vertices in the given graph. Dijkstraâs algorithm is a popular algorithm for solving many single-source shortest path problems having non-negative edge weight in the graphs i.e., it is to find the shortest dis 4 min read Difference between BFS and Dijkstra's algorithms when looking for shortest path? What is Dijkstra's Algorithm? Dijkstra's Algorithm is used for finding the shortest path between any two vertices of a graph. It uses a priority queue for finding the shortest path. For more detail about Dijkstra's Algorithm, you can refer to this article. What is BFS Algorithm? Breadth First Search 2 min read Johnsonâs algorithm for All-pairs shortest paths | Implementation Given a weighted Directed Graph where the weights may be negative, find the shortest path between every pair of vertices in the Graph using Johnson's Algorithm. The detailed explanation of Johnson's algorithm has already been discussed in the previous post. Refer Johnsonâs algorithm for All-pairs 12 min read Shortest Path to Get Food in Matrix Given a matrix grid[][], where '*' represents your location, '#' represents food cells, 'O' represents free space, 'X' represents obstacles. The task is to find the shortest path from your location to any food cell in a given matrix grid, Return the length of the shortest path to reach any food cell 10 min read Shortest Path in Directed Acyclic Graph Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths from given source to all other vertices. Recommended PracticeShortest path from 1 to nTry It! For a general weighted graph, we can calculate single source shortest distances in O(VE) time using Bellmanâ 15 min read Implementation of Johnsonâs algorithm for all-pairs shortest paths Johnson's algorithm finds the shortest paths between all pairs of vertices in a weighted directed graph. It allows some of the edge weights to be negative numbers, but no negative-weight cycles may exist. It uses the Bellman-Ford algorithm to re-weight the original graph, removing all negative weigh 14 min read Printing Paths in Dijkstra's Shortest Path Algorithm Given a graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph.We have discussed Dijkstra's Shortest Path algorithm in the below posts. Dijkstraâs shortest path for adjacency matrix representationDijkstraâs shortest path for adjacency list 15 min read Like