# Comparison of Dijkstra’s and Floyd–Warshall algorithms

## Dijkstra Algorithm

Dijkstra’s Algorithm is a Single-Source Shortest Path SSSP algorithm, i.e., given a source vertex it finds the shortest path from the source to all other vertices. The idea is to generate a SPT (shortest path tree) with a given source as a root and with two sets,

• one set contains vertices included in the shortest-path tree,
• other set includes vertices not yet included in the shortest-path tree.

At every step of the algorithm, find a vertex that is in the other set (set not yet included) and has a minimum distance from the source.

## Floyd Warshall Algorithm

Floyd Warshall Algorithm is All-Pair Shortest Path algorithm, which is to find shortest distance between all pair of vertices. The idea is to use a 2-D Distance Array and initialize it with direct edge weights and then iteratively considers all possible intermediate vertices to update the array. The algorithm checks if a path through an intermediate vertex offers a shorter distance between two nodes.

## Comparison of Dijkstra’s and Floyd–Warshall algorithms on the basis of algorithm:

### Dijkstra Algorithm:

Dijkstra’s Algorithm uses a Greedy approach to find the shortest path from the source to all other vertices. It starts from the source vertex and maintains a set of vertices with known minimum distances from the source. The key idea of the Greedy strategy is selecting the vertex with the currently shortest known distance from the Source and exploring it next.

### Floyd Warshall Algorithm:

Floyd Warshall Algorithm uses Dynamic Programming approach to compute shortest distance between all pair of vertices. The idea of dp approach is to maintain a 2-D Distance Array and initialize it with direct edge weights effectively solving the subproblems for vertex pairs that have direct connections. It then considers additional intermediate vertices to optimize the paths between all pairs.

## Comparison of Dijkstra’s and Floyd–Warshall algorithms on the basis of Complexity Analysis:

### Dijkstra Algorithm:

The time complexity of Dijkstra Algorithm depends on the specific implementation and data structures used. In a straightforward implementation for dense graphs where E (no. of edges) is approximately equal to V2 (no. of Vertices) time complexity is of O(V2) . However, for Sparse graphs using Priority queue or Min-heap is more optimal which gives time complexity of O(E + V*log(V)).

### Floyd Warshall Algorithm:

For the Floyd Warshall Algorithm, the time complexity is O(V^3), where V represents the number of vertices in the graph. It involves three nested loops that iterate through all possible pairs of nodes and consider all possible intermediaries, leading to a cubic time complexity.

## Complete Comparison of Dijkstra’s and Floyd–Warshall algorithms

Comparison Basis

Dijkstra Algorithm

Floyd Warshall Algorithm

Introduction

Dijkstra’s algorithm is a single-source shortest path algorithm used to find the shortest paths from a single source vertex to all other vertices in a weighted graph.

The Floyd-Warshall algorithm is an all-pairs shortest path algorithm used to find the shortest paths between all pairs of vertices in a weighted graph.

Algorithm

Greedy approach,as it selects the vertex with the shortest distance

It uses Dynamic programming, to compute all pair shortest paths.

Data Structure

It Typically uses a priority queue or min-heap.

It uses a two-dimensional array.

Negative Edges

Dijkstra’s algorithm does not work correctly with graphs that have negative edge weights.

The Floyd-Warshall algorithm can handle graphs with both positive and negative edge weights.

Time Complexity

With a priority queue or min-heap, time complexity is O(E + V*log(V)).

The time complexity of the Floyd-Warshall algorithm is O(V^3).

Auxilary Space

O(V) with priority queue or min-heap.

The auxiliary space complexity is O(V^2) because of 2D array used.

Use Case

It is efficient for finding shortest paths from a single source.

It is suitable for finding shortest paths between all pairs of vertices

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!