Professional Documents
Culture Documents
OR Section 5
OR Section 5
OR Section 5
Section 5
Shortest path using Dijkstra’s Algorithm
: Note That
.Network is a graph with weights-
.Graph does not have any Weights-
A GPS is an example of a Network which must-
: Remember That
: There is 2 types of graphs
.Directed Graph-1
.Undirected Graph-2
Directed Graph-1
5
B E
6
2 11 8
10
D G
A
7
4 3
9
C F
1
Find The short path From node (A) and node (G).
.CONT
: node E
.B is coming from A with dst = 2
. B is going to E with dst = 5
.Then 2 + 5 = 7
From previous table
E=infinity
.)Replace infinity with 7(∞ >7
.CONT
D=10 A 2 B(done)
A 4 C
.)Replace 10 with 7( 10<7 C 7 D
A 7 E
A ∞ F
A ∞ G
.CONT
: node F
.C is coming from A with dst = 4
Previous Shortest Vertex
. C is going to F with dst = 1 Vertex Distance
from Node
.Then 4 + 1 = 5 (A)
: node G
.F is coming from C with dst = 5 Previous Shortest Vertex
Vertex Distance
.Then 5 + 9 = 14 from Node
(A)
From previous table
Replace infinity with 5(∞ >14( A 0 A (done)
A 2 B(done)
Unvisite Visited
d A 4 C(done)
A C 7 D
B
A 7 E
C
F C 5 F
F 14 G
.CONT
Unvisite Visited
d
A
B
C
F
D
.CONT
: node F
.D is coming from C with dst = 7 Previous Shortest Vertex
. D is going to F with dst = 7 Vertex Distance
from Node
.Then 7 + 7 = 14 (A)
B
C
F
F D
.CONT
: node E
.E is coming from B with dst = 7 Previous Shortest Vertex
. Eis going to G with dst = 7 Vertex Distance
from Node
.Then 7 + 6 = 13 (A)
So
. The shortest distance from A to g = 13 -
.The shortest path is A,B,E,G-
. The shortest distance from A to F = 5 --
. The shortest distance from A to C = 4 --
:Example 2-2
Unvisited visited
D
The second Table : We visit the node D
Previous vertex Shortest Ver
distance from tex
node D
D 0 D
D 1 B
D 11 E
D 2 A
D ∞ C
After Visiting
Node D ,
We find the least value of other nodes.
.CONT
Previous Shortest Vertex
vertex distance from
node D
D 0 D (done)
D 1 B
D 11 E
D 2 A
D ∞ C
: node E
.B is coming from d with dst = 1
. B is going to E with dst = 2
.Then 2 + 1 = 3
From previous table
E=11
.)Replace 11 with 3(3 >11
.CONT
Previous vertex Shortest distance Vertex
from node D
D 0 D (done)
D 1 B(Done)
D B 3 11 E
D 2 A
Un visited
D B 16 ∞ C viseted
D
After Visiting B
Node B , A
: node B
.A is coming from d with dst = 2
. A is going to B with dst = 6
.Then 2 + 6 = 8
From previous table
A=2
.)Keep current state(2 >8
.CONT
Previous vertex Shortest distance Vert
from node D ex
Un viseted visited
D 0 D
(don D
e) B
D 1 B(do A
ne)
E
D B 3 11 E
D 2 A(do
ne)
D B 16 ∞ C
After Visiting
Node A ,
We find the least value of other nodes.
.CONT
Previous vertex Shortest distance Vert
from node D ex
D 0 D
(don
e)
D 1 B(do
ne)
D B 3 11 E
D 2 A(do
ne)
D B 16 ∞ C
D 0 D Un viseted visited
(don
e) D
D 1 B(do B
ne) A
D B 3 11 E
E
(don
e)
D 2 A(do
ne)
D BE 8 16 ∞ C
After Visiting
Node E ,
.
.CONT
D 0 D
A 3 D
E 7 D
F 6 D
B ∞ D
C ∞ D Visited Unvisited
D A
.CONT
Visited Unvisited
D F
A
F 4 A
B 8 A
C ∞ D
.CONT
Visited Unvisited
D E
A
F
F 4 A
B 8 A
C ∞ D
.CONT
Visited Unvisited
D B
A
F
E
A 3 D
D to A to F to E to D=3+1+2+7=13<0 ---NO
E 6 F
D to A to F to E to B= 3+1+2+1=7<8 ---- YES
D to A to F to E to C =3+1+2+4=10<∞ -----YES F 4 A
B 7 E
C 10 E
.CONT
Visited Unvisited
D
A
F
E
B
D to A to F to E to B to C= 3+1+2+1+7=14<10 ----NO
Verte Shortest Distance from node D Previous Vertex
x
D 0 D
A 3 D
E 6 F
F 4 A
B 7 E
C 10 E
Thanks
?Any Question