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

CS221 ShortestPath

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph. It works as follows: 1) Initialize the algorithm by setting the distance of the source node to itself as 0 and all other nodes to infinity. 2) Find the closest node not yet processed and add it to the set of processed nodes. 3) Update the distances of unprocessed neighbors by comparing their distance to going through the newly processed node. 4) Repeat steps 2-3 until all nodes have been processed.

Uploaded by

usmanasif674
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

CS221 ShortestPath

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph. It works as follows: 1) Initialize the algorithm by setting the distance of the source node to itself as 0 and all other nodes to infinity. 2) Find the closest node not yet processed and add it to the set of processed nodes. 3) Update the distances of unprocessed neighbors by comparing their distance to going through the newly processed node. 4) Repeat steps 2-3 until all nodes have been processed.

Uploaded by

usmanasif674
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Graphs

1
Route Calculation

Dijkstra’s shortest path algorithm


Dijkstra’s algorithm
N : set of nodes for which shortest path already found
Initialization: (Start with source node s)
N = {s}, Ds = 0, “s is distance zero from itself”
Dj=Csj for all j  s, distances of directly-connected neighbors
Step A: (Find next closest node i )
Find i  N such that
Di = min Dj for j  N
Add i to N
If N contains all the nodes, stop
Step B: (update minimum costs)
For each node j  N
Minimum distance from s
Dj = min (Dj, Di+Cij)
to j through node i in N
Go to Step A
Execution of Dijkstra’s algorithm
2 2 
1 3 1 1 3 1
6 6 
5 2 5 2
3 3
1 4 1 4 2
2
3 3
2 2
 4 5 4 5

Iteration N D2 D3 D4 D5 D6
Initial {1} 3 2 5  
1 {1,3} 3 2 4  3
2 {1,2,3} 3 2 4 7 3 
3 {1,2,3,6} 3 2 4  5 3
4 {1,2,3,4,6} 3 2 4 5  3
5 {1,2,3,4,5,6} 3 2 4 5 3
Shortest Paths in Dijkstra’s Algorithm
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5

2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
7777777777777
Step start N D(B),p(B) D(C),p(C) D(D),p(D) D(E),p(E) D(F),p(F)
0 A 2,A 5,A 1,A infinity infinity
1 AD 2,A 4,D 2,D infinity
2 ADE 2,A 3,E 4,E
3 ADEB 3,E 4,E
Dijkstra’s algorithm: example
4 ADEBC 4,E
5 ADEBCF

5
3
B C 5
2
A 2 1 F
3
1 2
D E
1
ALL PAIRS SHORTEST PATH
 So far, we have discussed the problem of finding
the shortest paths, starting from a specific node .
 How about finding the shorted path between
every possible pair of nodes in a graph?
 Which algorithm can be used?
 Dijkstra’s algorithm can be used
 Call
the Dijkstra’s algorithm by setting each node as the
source node, one by one
 Any Other solution?
 Floyd -Warshal Algorithm can be used instead..

You might also like