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.
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:
- Space Complexity:
Example
Consider the following weighted graph:
1 3
- Time Complexity: using an adjacency matrix, or with a priority queue (min-heap).
- Space Complexity:
- Time Complexity:
- Space Complexity:
- 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 .
- 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
- Amortized analysis of stdvector insertion
- Amortized complexity in layman's terms?
- Amortized time of dynamic array
- An algorithm for converting a base-10 number to a base-N number
- Amazon S3 What are considered PUT/COPY/POST/LIST request?
- An algorithm to find a pair of sums from a list of numbers?
- an algorithm for fitting a rectangle inside a polygon
- An algorithm for inflating/deflating offsetting, buffering polygons

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 courseTrack 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.