0% found this document useful (0 votes)
7 views9 pages

Shortest Path algorithm

The document discusses two algorithms for solving the shortest path problem in graphs: Dijkstra's Algorithm and the Floyd-Warshall Algorithm. Dijkstra's Algorithm is designed for single-source shortest path problems with non-negative edge weights, while the Floyd-Warshall Algorithm finds shortest paths between all pairs of vertices and can handle negative edge weights. It also compares the two algorithms in terms of optimization, technique, time complexity, and handling of negative weights.

Uploaded by

soniyagupta1154
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views9 pages

Shortest Path algorithm

The document discusses two algorithms for solving the shortest path problem in graphs: Dijkstra's Algorithm and the Floyd-Warshall Algorithm. Dijkstra's Algorithm is designed for single-source shortest path problems with non-negative edge weights, while the Floyd-Warshall Algorithm finds shortest paths between all pairs of vertices and can handle negative edge weights. It also compares the two algorithms in terms of optimization, technique, time complexity, and handling of negative weights.

Uploaded by

soniyagupta1154
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Shortest Path algorithm

The shortest path problem is about finding a path between 2 vertices in a graph such that the total
sum of weight of the edges is minimum.
Dijkstra’s Algorithm:
Dijkstra’s algorithm is a 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 Dutch computer scientist Edsger W. Dijkstra in 1956.
Dijkstra’s algorithm is also called single-source shortest path algorithm. The output
obtained is called shortest path spanning tree.

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.

Examples

Step 1

Initialize the distances of all the vertices as ∞, except the source node S.
Vertex S A B C D E
Distance 0 ∞ ∞ ∞ ∞ ∞

Now that the source vertex S is visited, add it into the visited array.
Visited = {S}

Step 2

The vertex S has three adjacent vertices with various distances and the vertex with
minimum distance among them all is A. Hence, A is visited and the dist[A] is changed
from ∞ to 6.
S→A=6
S→D=8
S→E=7

Visited = {S, A}

Vertex S A B C D E
Distance 0 6 ∞ ∞ 8 7

Step 3

There are two vertices visited in the visited array, therefore, the adjacent vertices
must be checked for both the visited vertices.

Vertex S has two more adjacent vertices to be visited yet: D and E. Vertex A has one
adjacent vertex B.

Calculate the distances from S to D, E, B and select the minimum distance −


S → D = 8 and S → E = 7.
S → B = S → A + A → B = 6 + 9 = 15
Visited = {S, A, E}
Vertex S A B C D E
Distance 0 6 15 ∞ 8 7

Step 4
Calculate the distances of the adjacent vertices – S, A, E – of all the visited arrays and
select the vertex with minimum distance.
S→D=8
S → B = 15
S → C = S → E + E → C = 7 + 5 = 12
Visited = {S, A, E, D}

Vertex S A B C D E
Distance 0 6 15 12 8 7

Step 5

Recalculate the distances of unvisited vertices and if the distances minimum than
existing distance is found, replace the value in the distance array.
S → C = S → E + E → C = 7 + 5 = 12
S → C = S → D + D → C = 8 + 3 = 11

dist[C] = minimum (12, 11) = 11


S → B = S → A + A → B = 6 + 9 = 15
S → B = S → D + D → C + C → B = 8 + 3 + 12 = 23

dist[B] = minimum (15,23) = 15


Vertex S A B C D E
Distance 0 6 15 11 8 7
Visited = { S, A, E, D, C}

Step 6

The remaining unvisited vertex in the graph is B with the minimum distance 15, is
added to the output spanning tree.
Visited = {S, A, E, D, C, B}

Complexity Analysis:

 Time Complexity: O(V^2) in the worst case, where V is the number of vertices. This can
be improved to O(V^2) with some optimizations.
 Auxiliary Space: O(V), where V is the number of vertices and E is the number of edges in
the graph.

Floyd Warshall Algorithm

The Floyd-Warshall algorithm is a graph algorithm that is deployed to find the shortest path
between all the vertices present in a weighted graph. The Floyd-Warshall algorithm, named
after its creators Robert Floyd and Stephen Warshall.
This algorithm is highly efficient and can handle graphs with both positive and negative edge
weights, making it a versatile tool for solving a wide range of network and connectivity
problems.

Consider a graph, G = {V, E} where V is the set of all vertices present in the graph and E is the
set of all the edges in the graph. The graph, G, is represented in the form of an adjacency
matrix, A, that contains all the weights of every edge connecting two vertices.

Algorithm

Step 1 − Construct an adjacency matrix A with all the costs of edges present in the
graph. If there is no path between two vertices, mark the value as ∞.

Step 2 − Derive another adjacency matrix A1 from A keeping the first row and first
column of the original adjacency matrix intact in A1. And for the remaining values,
say A1[i,j], if A[i,j]>A[i,k]+A[k,j] then replace A1[i,j] with A[i,k]+A[k,j]. Otherwise, do
not change the values. Here, in this step, k = 1 (first vertex acting as pivot).

Step 3 − Repeat Step 2 for all the vertices in the graph by changing the k value for
every pivot vertex until the final matrix is achieved.

Step 4 − The final adjacency matrix obtained is the final solution with all the shortest
paths.

Pseudo-code-

Floyd-Warshall(w, n){ // w: weights, n: number of vertices


for i = 1 to n do // initialize, D (0) = [wij]
for j = 1 to n do{
d[i, j] = w[i, j];
}
for k = 1 to n do // Compute D (k) from D (k-1)
for i = 1 to n do
for j = 1 to n do
if (d[i, k] + d[k, j] < d[i, j]){
d[i, j] = d[i, k] + d[k, j];
}
return d[1..n, 1..n];
}

Example-

1. Create a matrix A0 of dimension n*n where n is the number of vertices. The row and the column

are indexed as i and j respectively. i and j are the vertices of the graph.

Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is no path

from ith vertex to jth vertex, the cell is left as infinity.


2. Fill each cell with the distance between ith and jth vertex

3. Now, create a matrix A1 using matrix A0. The elements in the first column and the first row are

left as they are. The remaining cells are filled in the following way.

Let k be the intermediate vertex in the shortest path from source to destination. In this

step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).

That is, if the direct distance from the source to the destination is greater than the path

through the vertex k, then the cell is filled with A[i][k] + A[k][j].

In this step, k is vertex 1. We calculate the distance from source vertex to destination vertex

through this vertex k.

Calculate the distance from the source vertex to destination vertex through this vertex k.

For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the distance
from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to 4) is 7. Since 4 <

7, A0[2, 4] is filled with 4.

4. Similarly, A2 is created using A1. The elements in the second column and the second row are left

as they are.

In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in step 2.

Calculate the distance from the source vertex to destination vertex through this vertex 2

5. Similarly, A3 and A4 is also created.

Calculate the distance from the source vertex to destination vertex through this vertex 3
6. Calculate the distance from the source vertex to destination vertex through this vertex 4

7. A4 gives the shortest path between each pair of vertices.

Complexity Analysis:

 Time Complexity: O(V3), where V is the number of vertices in the graph and we run
three nested loops each of size V
 Auxiliary Space: O(V2), to create a 2-D matrix in order to store the shortest distance for
each pair of nodes.

Dijkstra’s Algorithm vs Floyd-Warshall Algorithm-


Dijkstra’s algorithm and Floyd-Warshall algorithm are both used to find the shortest path in a
weighted graph, but they have some key differences. Here are the main differences between
Dijkstra’s algorithm and Floyd-Warshall algorithm:

Feature: Dijkstra’s Floyd-Warshall Algorithm


Optimization Optimized for finding the shortest path Floyd-Warshall algorithm is
between a single source node and all other optimized for finding the
nodes in a graph with non-negative edge shortest path between all
weights pairs of nodes in a graph.

Technique Dijkstra’s algorithm is a single-source Floyd-Warshall algorithm, on


shortest path algorithm that uses a greedy the other hand, is an all-pairs
approach and calculates the shortest path shortest path algorithm that
from the source node to all other nodes in uses dynamic programming to
the graph. calculate the shortest path
between all pairs of nodes in
the graph.
Time Dijkstra’s algorithm has a time complexity Floyd-Warshall algorithm, on
Complexity of O(V^2) for a dense graph and O(E log V) the other hand, is an all-pairs
for a sparse graph, where V is the number shortest path algorithm that
of vertices and E is the number of edges in uses dynamic programming to
the graph. calculate the shortest path
between all pairs of nodes in
the graph.
Negative Dijkstra’s algorithm does not work with Floyd-Warshall algorithm, on
Weights graphs that have negative edge weights, as the other hand, is an all-pairs
it assumes that all edge weights are non- shortest path algorithm that
negative. uses dynamic programming to
calculate the shortest path
between all pairs of nodes in
the graph.

You might also like