0% found this document useful (0 votes)
47 views49 pages

OR Section 5

The document discusses using Dijkstra's algorithm to find the shortest path between nodes in a directed graph. It provides an example of finding the shortest path from node A to G. It initializes the algorithm, calculates the distances and previous nodes at each step, and visits nodes in order of shortest distance until the shortest path from A to G is determined.

Uploaded by

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

OR Section 5

The document discusses using Dijkstra's algorithm to find the shortest path between nodes in a directed graph. It provides an example of finding the shortest path from node A to G. It initializes the algorithm, calculates the distances and previous nodes at each step, and visits nodes in order of shortest distance until the shortest path from A to G is determined.

Uploaded by

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

Introduccion To Operation Research

Section 5
Shortest path using Dijkstra’s Algorithm

 Integral , non-negative data


 There is a directed path from source node s to
all other nodes.
 Objective: find the shortest path from node X
to each other node.
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-

.have a start and end points


The Goal is to Obtain Shortest Path at shortest
. time
Shortest path using Dijkstra’s Algorithm

: 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

.First , we build 2 basic tables


the first contains the current visited and-1
.unvisited node

the second contains the current vertex , the-2


.shortest distance and pervious vertex
.CONT

.Start with First Node A (the source node), calc


.all directed and indirected connected nodes
Unvisited Visited
A
.CONT

:We visit the node A: The second Table


Previous Vertex Shortest Distance Vertex
from Node (A) B,C,D are
directly
A 0 A connected
A 2 B nodes .
But E,F,G
A 4 C
are not directly
A 10 D connected
A ∞ E So they have an
infinity
A ∞ F
distance from
A ∞ G node A.
.CONT

Previous Vertex Shortest Distance Vertex


from Node (A) After Visiting
Node A ,
A 0 A (done) We find the
A 2 B least value of
other nodes .
A 4 C
A 10 D
A ∞ E
A ∞ F
A ∞ G
.CONT

Previous Vertex Shortest Distance Vertex


from Node (A) Node B with
distance =2
A 0 A (done) Is the least
A 2 B value , then we
will visit node
A 4 C
B.
A 10 D
A ∞ E
A ∞ F
A ∞ G
Discover the neighbors connected to Node B.
. D& E

: Begin with node D


.B is coming from A with dst = 2
. B is going to D with dst = 11
.Then 2 + 11 = 13
From previous table
D=10
.)keep current state( 10>13
Discover the neighbors connected to Node B.
. D& E

: 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

Previous Vertex Shortest Distance Vertex After Visiting


from Node (A) Node B ,
We find the
A 0 A (done) least value of
A 2 B(done) other nodes .
A 4 C
A 10 D Unvisite Visited
d
A 7 E
A
A ∞ F
B
A ∞ G
C
.CONT

Previous Vertex Shortest Distance Vertex


from Node (A) Node C with
distance 4
A 0 A (done) Is the least
value , then we
A 2 B(done) visit node c.
A 4 C
A 10 D
A ∞ E
A ∞ F
A ∞ G
Discover the neighbors connected to Node C.
. D& F

: Begin with node D


.C is coming from A with dst = 4
. C is going to D with dst = 3 Previous
Vertex
Shortest
Distance
Vertex

.Then 4 + 3 = 7 from Node


(A)
From previous table A 0 A (done)

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)

From previous table A 0 A (done)


.)Replace infinity with 5(∞ >5 A 2 B(done)
Unvisite Visited A 4 C
d
A C 7 D
B A 7 E
C C 5 F
A ∞ G
.CONT

Previous Shortest Vertex After Visiting


Vertex Distance from
Node (A) Node C ,
We find the least
value of other
nodes .
A 0 A (done)
A 2 B(done)
A 4 C(done)
C 7 D
A 7 E
C 5 F
A ∞ G
.CONT

Previous Shortest Vertex Node F with


Vertex Distance from
Node (A) distance 5
Is the least
value , then we
visit node F.
A 0 A (done)
A 2 B(done)
Unvisite Visited
A 4 C(done) d
C 7 D A
A 7 E B
C 5 F C
F
A ∞ G
Discover the neighbors connected to Node F: Node G
.

: 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

.Nodes G , D and E are not visited yet


: Choose the least distance
D=7
E=7
G=14
. choose any (7) of the two nodes
.Lets choose D
.CONT

.Node D can visit E and F nodes

Unvisite Visited
d
A
B
C
F
D
.CONT

: Begin with node E


.D is coming from C with dst = 7
. D is going to E with dst = 8
.Then 7 + 8 = 15
From previous table

.)Keep current state( 7>15


.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)

From previous table A 0 A (done


.)Keep current state( 7>14 A 2 B(done
Unvisite Visited
d A 4 C(done
A C 7 D
B
B 7 E
C
F C 5 F
D E 14 G
:Now we have E & G

.Node E is only visiting node g


Unvisite Visited
d

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)

From previous table A 0 A (done


.)Replace 14 with 13( 14<13 A 2 B(done
A 4 C(done
C 7 D
B 7 E
C 5 F
E 13 G
:Final Step

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

Consider the network shown in the above figure find


? the shortest distance between node D to node C
:Solution

.First , we build 2 basic tables


the first contains the current visited and-1
.unvisited node

the second contains the current vertex , the-2


shortest distance and pervious vertex
.CONT

.Start with First Node D (the source node), calc


.all directed and undirected connected nodes

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

B,E,A are directly connected nodes .


But C
are not directly connected
So they have an infinity distance from node D
.CONT
Previous Shortest Vertex
vertex distance from
node D
D 0 D (done)
D 1 B
D 11 E Un viseted visited
D 2 A D
D ∞ C B

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 B with distance 1


Is the least value , then we visit node B.
Discover the neighbors connected to Node B.
. C& E

: Begin with node c


.B is coming from A with dst = 1
. B is going to C with dst =15
.Then 1 + 15 = 16
From previous table
∞ =C
.) Replace infinity with 16( 16> ∞
.CONT

: 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

We find the least value of other nodes .


.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
D B 16 ∞ C

Node A with distance 2


Is the least value , then we visit node A.
Discover the neighbors connected to Node A.
B

: 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

Node E with distance 3


Is the least value , then we visit node E
Discover the neighbors connected to Node E.
C

: Begin with node C


.E is coming from D with dst = 3
. E is going to C with dst =5
.Then 3 + 5 = 8
From previous table
C= 8
.) Replace 16 with 8( 8> 16
.CONT
Previous vertex Shortest distance Vert
from node D ex

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

After visiting all node


the shortest distance from node D to node C = 8
the path = D,B,E,C
: Example 3

Q: Consider the network shown in the above figure; Find


the shortest distance between node D and node C.
:Solution

Vertex Shortest Distance from node D Previous Vertex

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  
   
   

Vert Shortest Distance from node D Previous Vertex


ex
D 0 D
D to A & A to D =3+3=6<0 ---NO
A 3 D
D to A to F = 3+1=4<6 ---- yes
D to A to B=3+5=8<∞ -----yes E 7 D

F 4 A

B 8 A

C ∞ D
.CONT
Visited Unvisited
D E
A  
F  
   

Verte Shortest Distance from node D Previous Vertex


x
D to F to D =3+1+6=10<0 ---NO D 0 D
D to F to B = 3+1+5=9<8 ---- NO
A 3 D
D to F to E=3+1+3=6<7 -----YES
E 6 F

F 4 A

B 8 A

C ∞ D
.CONT
Visited Unvisited
D B
A  
F  
E  

Vert Shortest Distance from node D Previous Vertex


ex
D 0 D

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

You might also like