0% found this document useful (0 votes)
46 views

Shortest Path Algorithms

Dijkstra's algorithm is used to find the shortest path between a source node (A) and all other nodes in an undirected graph. The algorithm works by first assigning all nodes an initial distance of infinity from the source node, except for the source which is 0. It then iteratively selects the unvisited node with the lowest distance, calculates new distances to neighboring nodes, and updates distances if lower. When applied to the sample graph, the shortest paths and costs from node A to all other nodes are output.

Uploaded by

Avinash Alla
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)
46 views

Shortest Path Algorithms

Dijkstra's algorithm is used to find the shortest path between a source node (A) and all other nodes in an undirected graph. The algorithm works by first assigning all nodes an initial distance of infinity from the source node, except for the source which is 0. It then iteratively selects the unvisited node with the lowest distance, calculates new distances to neighboring nodes, and updates distances if lower. When applied to the sample graph, the shortest paths and costs from node A to all other nodes are output.

Uploaded by

Avinash Alla
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/ 6

SHORTEST PATH ALGORITHMS

Introduction:

 We use these algorithms to solve shortest path problems.


 In general, Shortest path problem is a problem of finding the shortest path(s) between
vertices or nodes in the given graph.
 Shortest path between two vertices is a path that has the least cost (weight /
distance) as compared to all other paths that exists in the graph.
Applications:

Shortest path algorithms have a wide range of applications such as in-

 Google Maps
 Road Networks
 Logistics Research

Types of Shortest Path algorithms:


There are different types of shortest path algorithms

 Single-pair shortest path algorithm (A* search algorithm)


 Single-source shortest path algorithm (Dijkstra's algorithm)
 Single-destination shortest path algorithm(Varient of Dijkstra's algorithm)
 All pairs shortest path algorithm (Floyd Warshall & Johnson's algorithm)
Single-source shortest path algorithm:
 Single-source shortest path algorithm is a shortest path algorithm with which we find
out the shortest paths from a given source vertex to all the other remaining vertices of
the given graph.

 Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used
for solving single-source shortest path problems.

Dijkstra’s Algorithm:
 It is used for solving the single source shortest path problem which gives the shortest
paths from one particular source node to all the other nodes of the given graph.
 Conditions for Dijkstra’s Algorithm-
1. Dijkstra’s algorithm works only for those graphs that are connected.
2. Dijkstra’s algorithm works only for those graphs that do not contain any edges with
negative weights.
3. Dijkstra’s algorithm works for directed as well as undirected graphs.
Algorithm
1. for each vertex v Є V // vertex set
 dis(source) = 0 // distance of source is 0
 dis(v) = ∞ // distance of remaining vertexes is ∞
2. Q = |V| // vertex set
3. while Q ≠ Ф
 u = Extract-Min (Q) // selecting minimum edge
 for each vertex v Є adj[u] //
◦ if dis(u) + weight(u,v) < dis(v) then
▪ dis(v) = dis(u) + weight(u, v)

I) Problem:’ For the given undirected graph, find the shortest path and its cost by
considering A as its single source using Dijkstra’s Algorithm.

Solution:

Step 1: The source is the vertex A. Using a table on vertex and visiting nodes apply the given
cost from the above graph.
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α

Step 2: Find the minimum cost among 8,2,5 and select that vertex as visited one. Here it is C
= 2A. Thus the current cost =2.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(C,B) = Min(8 , α) = 8
(C,D) = Min(5 , 2+2) = 4
(C,E) = Min(α,2+5)= 7
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
Step3: Find the minimum cost among 8,4,7 and select that vertex as visited one. Here it is
D= 4C. Thus the current cost =4.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(D,B) = Min(8 , 4+2) = 6
(D,E) = Min(7, 4+1) = 5 (D,F) = Min(α,4+6)= 10
(D,G) = Min(α,4+3)= 7

Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α

Step 4: Find the minimum cost among 6,5,10,7 and select that vertex as visited one. Here it
is E= 5D. Thus the current cost =5.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(E,B) = Min(6 , 5+ α) = 6
(E,F) = Min(10, 5+ α) = 10 (E,H) = Min(α, α)= α
(E,G) = Min(7,5+1)= 6

Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
Step 5: Find the minimum cost among 6,10,6 and select one vertex as visited one. Here it is
B= 6D. Thus the current cost =6.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(B,F) = Min(10 , 6+ 13) = 10
(B,G) = Min(6, 6+ α) = 6
(B,H) = Min(α, α)= α

Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
Step 5: Find the minimum cost among 10,6 and select one vertex as visited one. Here it is
G= 6E. Thus the current cost =6.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(G,F) = Min(10 , 6+ 2) = 8
(G,H) = Min(α, 6+6)= 12

Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
G --- --- --- --- --- 8G 6E 12G
Step 6: Find the minimum cost among 8,12 and select one vertex as visited one. Here it is
F= 8G. Thus the current cost =8.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(F,H) = Min(12 , 8+ 3) = 11

Visited A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
G --- --- --- --- --- 8G 6E 12G

F --- --- --- --- --- 8G --- 11F

Step 7: Find the minimum cost among 11 and select one vertex as visited one. Here it is
H=11F. Thus the current cost =8.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)

Visited A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
G --- --- --- --- --- 8G 6E 12G
F --- --- --- --- --- 8G --- 11F

H --- --- --- --- --- --- --- 11F

Output:
The Shortest path using Dijkstra’s Algorithm is as follows:

Distance between the vertex Path Total Cost


AB A-C – D- B 2+2+2 = 6
AC A-C 2
AD A-C-D 2+2 = 4
AE A-C-D-E 2+2+1=5
AF A-C-D-E-G-F 2+2+1+1 +2 =8
AG A-C-D-E-G 2+2+1+1=6
AH A-C-D-E-G-F-H 2+2+1+1+2+3=11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy