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

chapitre 5- Shortest path problem

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

chapitre 5- Shortest path problem

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/ 23

29

30


31

R
32

 

𝒂 𝑪
33
34

(a) (b)

Figure 1: A network with a root and a network with an absorbing (negative) circuit.
35


36

𝒂 𝑪
37
38
39

-
40

Bellman’s algorithm
Input:Let R=(X,A,l) be an acyclic network, with r as the initial
vertex.
Output: The shortest paths from the root r to all the vertices in R,
with their lengths.

S{r}; π(r) 0;  xV, x≠r, π(x)+; Arc ;


For every vertex x such that N-(x) S do
π(x)minyN-(x)[π(y)+l(yx)]=π(z)+l(zx){z is the selected vertex}
SS{x}; ArcArc {zx};
EndFor
If S = X Then
The set Arc represents the arcs of the shortest paths from r to x,
x V.
Else
The vertex r is not a root, there is no shortest path in this
case.
EndIf
41

Figure 2 : Weighted graph (network) without circuit and with negative weights.
42

Initialization : S{s}; π(s) 0; xV, x≠s, π(x)+;


Arc ;
 x = 1 : π(1) π(s)+l(sx)]=1 ; SS{1}; ArcArc{s1};
 x = 2 : π(2) min[π(s)+l(s2), π(1)+l(12]=π(1)+l(12)=2 ;
SS{2}; ArcArc{12};
 x = 4 : π(4) π(2)+l(24)=3 ; SS{4}; ArcArc{24};
 x = 3 : π(3) π(1)+l(13) =3 ; SS{3}; ArcArc{13};
 x=5: π(5)min[π(1)+l(15),π(2)+l(25),π(3)+l(35),π(4)+l(45)]=
π(1)+l(15)=-1 ; SS{5}; ArcArc{15};
 x=6:π(6)min[π(3)+l(36),π(5)+l(56),π(4)+l(46)]=π(3)+l(36)=6;
SS{6}; ArcArc{36};
 S = X End of the algorithm.
43

Figure 3 : The shortest paths from s to all other vertices of R.


44
45
46

Dijkstra's Algorithm
Input : R=(X,A,l) A network with positive edge lengths,r
as the initial vertex.
Output: The shortest paths from the root r to all
vertices in R, along with their lengths.
S{r}; π(r) 0;  xV, x≠r, π(x)+; Arc(x) ,  xV;
While S ≠ V do
For every vertex y that is a successor of x do
If π(x)+l(xy)< π(y) then
π(y)  π(x)+l(xy) ;
Arc(y){xy} ;
EndIf
EndFor
Choose a vertex zV\S such that π(z)minxV\S(π(x)) ;
47

If π(z)=+ then
Termination of the algorithm: the vertex r is not a
root, and there is no solution.
else
S S{z} ; xZ ;
EndIf
EndWhile
48

Figure 4 : Weighted graph (network) with all positive weights.


49

Initialization: S{r}; π(r) 0;  xV, x≠r, π(x)+;


Arc(x) ;
Iteration1 x=r
 Y=1 :π(r)+l(r1)=2<π(1)=+π(1)π(r)+l(r1)=0+2=2;
Arc(1){r1};
 Y=2 :π(r)+l(r2)=8<π(2)=+  π(2)π(r)+l(r2)=0+8=8;
Arc(2){r2};
Choose a vertex z : z1 ; S S{1} ; x1 ;
Iteration2 x=1
 Y=3 :π(1)+l(13)=3<π(3)=+  π(3)π(1)+l(13)=1+2=3;
Arc(3){13};
 Y=2 :π(1)+l(12)=7<π(2)=8  π(2)π(1)+l(12)=2+5=7;
Arc(2){12};
Choose a vertex z : z3 ; S S{3} ; x3 ;
50

Iteration3 x=3
 Y=2 :π(3)+l(32)=5<π(2)=7  π(2)π(3)+l(32)=3+2=5;
Arc(2){32};
 Y=4 :π(3)+l(34)=7<π(4)=+  π(4)π(3)+l(34)=3+4=7;
Arc(4){34};
 Y=5 :π(3)+l(35)=4<π(5)=+  π(5)π(3)+l(35)=3+1=4;
Arc(5){35};
Choose a vertex z : z5 ; S S{5} ; x5;
Vertex 5 has no successors.
Choose a vertex z : z2 ; S S{2} ; x2;
Iteration4 x=2
 Y=4 :π(2)+l(24)=6<π(4)=7  π(4)π(2)+l(24)=5+1=6;
Arc(4){24};
Choose a vertex z : z4 ; S S{4} ; x4;
S=V  End of the algorithm.
51

Figure 5 : The shortest paths from r to all other vertices in the network R.

You might also like