Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Graph
  • Practice Graph
  • MCQs on Graph
  • Graph Tutorial
  • Graph Representation
  • Graph Properties
  • Types of Graphs
  • Graph Applications
  • BFS on Graph
  • DFS on Graph
  • Graph VS Tree
  • Transpose Graph
  • Dijkstra's Algorithm
  • Minimum Spanning Tree
  • Prim’s Algorithm
  • Topological Sorting
  • Floyd Warshall Algorithm
  • Strongly Connected Components
  • Advantages & Disadvantages
Open In App
Next Article:
Applications of Dijkstra's shortest path algorithm
Next article icon

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:

  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:

  • Shortest Path Properties
  • How to find Shortest Paths from Source to all Vertices using Dijkstra’s Algorithm
  • Shortest path in an unweighted graph
  • Shortest Path in Directed Acyclic Graph
  • Shortest paths from all vertices to a destination

Next Article
Applications of Dijkstra's shortest path algorithm

V

vaibhav_gfg
Improve
Article Tags :
  • Graph
  • DSA
  • Dijkstra
  • BFS
  • DFS
  • bellman-ford
  • Floyd-Warshall
Practice Tags :
  • BFS
  • DFS
  • Graph

Similar Reads

  • Comparison between Tarjan's and Kosaraju's Algorithm
    Tarjan's Algorithm: The Tarjan's Algorithm is an efficient graph algorithm that is used to find the Strongly Connected Component(SCC) in a directed graph by using only one DFS traversal in linear time complexity. Working: Perform a DFS traversal over the nodes so that the sub-trees of the Strongly C
    15+ min read
  • 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
  • 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences