Graph algorithms
Floyd-Warshall
Dijkstra's algorithm
Bellman-Ford
shortest path algorithms

Am I right about the differences between Floyd-Warshall, Dijkstra's and Bellman-Ford algorithms?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

The task of finding the shortest paths in a graph is a foundational problem in computer science, with multiple algorithms developed to address it under different conditions and constraints. Of these, the Floyd-Warshall, Dijkstra's, and Bellman-Ford algorithms are among the most commonly referenced. Each algorithm has its strengths and limitations, making them suitable for varying types of graphs and specific application scenarios.

Floyd-Warshall Algorithm

Description

The Floyd-Warshall algorithm is a dynamic programming technique used to find the shortest paths between all pairs of vertices in a weighted graph. It is particularly well-suited for dense graphs and graphs characterized by negative edge weights, though it's important to note that the algorithm cannot handle graphs with negative weight cycles.

Operation

Floyd-Warshall achieves its goal by constructing a matrix `dist[][]` where `dist[i][j]` denotes the shortest distance from vertex `i` to vertex `j`. The algorithm iteratively updates the matrix by considering each vertex as an intermediate point, checking if a path through this vertex is shorter than the previously known shortest path.

Complexity

  • Time Complexity: O(V3)O(V^3)
  • Space Complexity: O(V2)O(V^2)

Example

Consider the following weighted graph:

1 3

  • Time Complexity: O(V2)O(V^2) using an adjacency matrix, or O(ElogV)O(E \log V) with a priority queue (min-heap).
  • Space Complexity: O(V)O(V)
  • Time Complexity: O(VE)O(V \cdot E)
  • Space Complexity: O(V)O(V)
    • Use Floyd-Warshall when dealing with dense graphs or when requiring all-pairs shortest paths.
    • Opt for Dijkstra's when graph edges have non-negative weights and the graph is sparse.
    • Choose Bellman-Ford for graphs with negative weights from a single source without negative cycles.
    • Improved Fibonacci Heap implementations can make Dijkstra even faster at O(E+VlogV)O(E + V \log V).
    • Johnson's algorithm can be used in tandem with Dijkstra's to handle all-pairs shortest paths in large graphs that Dijkstra's alone can't deal with efficiently.
    • Floyd-Warshall finds use in network routing software where multiple routes are considered.
    • Dijkstra's is especially popular in geographical mapping services for its efficiency.
    • Bellman-Ford's strength in handling negative weights makes it valuable in financial networks where loss or cost is modeled dynamically.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms