0% found this document useful (0 votes)
17 views4 pages

Dijkstra and Warshall

Dijkstra's algorithm is a method for finding the shortest path from a single source vertex to all other vertices in a graph with non-negative edge weights, applicable to both directed and undirected graphs. The Floyd-Warshall algorithm, on the other hand, finds the shortest paths between all pairs of vertices in a weighted graph and can handle negative weights, provided there are no negative weight cycles. A comparison reveals that Dijkstra's algorithm uses a greedy approach with a time complexity of O(E + V*log(V)), while Floyd-Warshall employs dynamic programming with a time complexity of O(V^3).

Uploaded by

manojrajora.vgi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Dijkstra and Warshall

Dijkstra's algorithm is a method for finding the shortest path from a single source vertex to all other vertices in a graph with non-negative edge weights, applicable to both directed and undirected graphs. The Floyd-Warshall algorithm, on the other hand, finds the shortest paths between all pairs of vertices in a weighted graph and can handle negative weights, provided there are no negative weight cycles. A comparison reveals that Dijkstra's algorithm uses a greedy approach with a time complexity of O(E + V*log(V)), while Floyd-Warshall employs dynamic programming with a time complexity of O(V^3).

Uploaded by

manojrajora.vgi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Dijkstra’s algorithm - 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 distance between two vertices on a graph. It was conceived by
Dutch computer scientist Edsger W. Dijkstra in 1956.

Q- Can Dijkstra’s Algorithm work on both Directed and Undirected graphs?


Yes, Dijkstra’s algorithm can work on both directed graphs and undirected graphs as this
algorithm is designed to work on any type of graph as long as it meets the requirements of
having non-negative edge weights and being connected.

 In a directed graph, each edge has a direction, indicating the direction of travel
between the vertices connected by the edge. In this case, the algorithm follows the
direction of the edges when searching for the shortest path.
 In an undirected graph, the edges have no direction, and the algorithm can traverse
both forward and backward along the edges when searching for the shortest path.

Algorithm for Dijkstra’s Algorithm:


1. Mark the source node with a current distance of 0 and the rest with infinity.
2. Set the non-visited node with the smallest current distance as the current node.
3. For each neighbor, N of the current node adds the current distance of the adjacent node
with the weight of the edge connecting 0->1. If it is smaller than the current distance
of Node, set it as the new current distance of N.
4. Mark the current node 1 as visited.
5. Go to step 2 if there are any nodes are unvisited.

The algorithm will generate the shortest path from node 0 to all the other nodes in the
graph.

For this graph, we will assume that the weight of the edges represents the distance
between two nodes.

As, we can see we have the shortest path from,


Node 0 to Node 1, from
Node 0 to Node 2, from
Node 0 to Node 3, from
Node 0 to Node 4, from
Node 0 to Node 6.
Distance: Node 0 -> Node 1 -> Node 3 = 2 + 5 = 7
Distance: Node 0 -> Node 1 = 2

Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 ->
= 2 + 5 + 10 = 17 Node 6 = 2 + 5 + 10 + 2 = 19

So, the Shortest Distance from the Source


Vertex is 19 which is optimal one
Floyd-Warshall algorithm - Floyd-Warshall algorithm is a dynamic programming
algorithm used to find the shortest paths between all pairs of vertices in a weighted
graph. It works for both directed and undirected graphs and can handle negative
weights, provided there are no negative weight cycles.

The Floyd-Warshall Algorithm is a dynamic programming algorithm used to find the


shortest paths between all pairs of vertices in a given weighted graph. It
systematically updates the solution matrix to ensure that it eventually contains the
shortest paths between all pairs of vertices.
Complete Comparison of Dijkstra’s and Floyd–Warshall algorithms
Comparison Dijkstra Algorithm Floyd Warshall Algorithm

Dijkstra’s algorithm is a single-source The Floyd-Warshall algorithm is an


shortest path algorithm used to find all-pairs shortest path algorithm
the shortest paths from a single source used to find the shortest paths
vertex to all other vertices in a between all pairs of vertices in a
Introduction weighted graph. weighted graph.

Greedy approach,as it selects the It uses Dynamic programming, to


Algorithm vertex with the shortest distance compute all pair shortest paths.

Data It Typically uses a priority queue or


It uses a two-dimensional array.
Structure min-heap.

Dijkstra’s algorithm does not work The Floyd-Warshall algorithm can


Negative correctly with graphs that have handle graphs with both positive and
Edges negative edge weights. negative edge weights.

Time With a priority queue or min-heap, time The time complexity of the Floyd-
Complexity complexity is O(E + V*log(V)). Warshall algorithm is O(V^3).

Auxilary The auxiliary space complexity is


O(V) with priority queue or min-heap.
Space O(V^2) because of 2D array used.

It is efficient for finding shortest It is suitable for finding shortest


Use Case paths from a single source. paths between all pairs of vertices

You might also like