Beruflich Dokumente
Kultur Dokumente
2 Dijkstras Algorithm
3 Bellman-Ford Algorithm
4 Summary
Vertices correspond to
intersections and edges
correspond to roads.
Game map:
Look like a
maze.
Step 0
Find path from A to all other vertices?
2 Denote:
A F
• w(A,B) = weighting coefficient in the
path from A to B. So, from the graph,
3 1 4 5 we have w(A,B) = 3.
7 3 • L(Y) = Label of Y. Minimum cost of Y
or the length of the shortest path from
B G E orignating vertice to Y. L(Y) is a sum of
2 9
all weighting coefficient and the cost of
all vertices on the shortest path.
6 8 6
• S: A set of all vertices that the
shortest path go through.
C D
3
Set L(A) = 0
Solved node Solving node
S = {A}
Unsolved node
C D
3
B G E L(G) + w(G,F) = 5
2 9 L(G) + w(G,E) = 10
1
L(G) + w(G,D) = 9
6 8 6 L(G) + w(G,B) = 3
C D
3
Next vertice is F, set L(F)=2, add F
Solved node Solving node to set S:
Unsolved node S = {A,G,F}
2 adjiacent vertices: B, E, C, D.
0 2
A F
L(A) + w(A,B) = 3
3 1 4 5 L(A) + w(A,C) = 7
7 3 L(G) + w(G,E) = 10
L(G) + w(G,D) = 9
B G E
2 9 L(G) + w(G,B) = 3
1
L(F) + w(F,E) = 7
6 8 6 L(F) + w(F,D) = 5
C D
3
Next vertice is B, set L(B) = 3, add B
Solved node Solving node to set S:
Unsolved node S = {A,G,F,B}
2 adjiacent vertices: E, C, D.
0 2
A F
L(A) + w(A,C) = 7
3 1 4 5
L(G) + w(G,E) = 10
7 3 L(G) + w(G,D) = 9
B G E L(F) + w(F,E) = 7
2 9 L(F) + w(F,D) = 5
3 1
L(B) + w(B,C) = 9
6 8 6
C D
3
Next vertice is D, set L(D) = 5, add D
Solved node Solving node to set S:
Unsolved node S = {A,G,F,B,D}
2 adjiacent vertices: E, C.
0 2
A F
L(A) + w(A,C) = 7
3 1 4 5
L(G) + w(G,E) = 10
7 3
L(F) + w(F,E) = 7
B G E
2 9 L(B) + w(B,C) = 9
3 1
6 8 6 L(D) + w(D,C) = 8
L(D) + w(D,E) = 11
C D
3 5
Next vertice is C, set L(C) = 7, add C
Solved node Solving node to set S:
Unsolved node S = {A,G,F,B,D,C}
2 to adjiacent vertices: E.
0 2
A F
3 1 4 5
L(G) + w(G,E) = 10
7 3
L(F) + w(F,E) = 7
B G E
2 9 L(D) + w(D,E) = 11
3 1
6 8 6
C D
3 5
7
Next vertice is E, set L(E) = 7, add E
Solved node Solving node to set S:
Unsolved node S = {A,G,F,B,D,C,E}
n A G F B D C E Shortest Path
0 0 A
1 0 (1,A) (2,A) (3,A) (7,A) A,G
2 0 (1,A) (2,A) (3,A) (9,G) (7,A) (10,G) A,G,F
3 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B
4 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D
5 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C
6 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E
0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E
Denote:
• (L,P): where L is label of the vertice, and P is preceding vertice in the path.
8
Z 7
-4
2
7
X Y
9
Step 0
5
U V n Z U X V Y
-2 0 0 ,- ,- ,- ,-
6
-3
0
8
Z 7
-4
2
7
X Y
9
Solved node Solving node
Unsolved node
Step 1
6,Z 5
U V n Z U X V Y
-2 0 0 ,- ,- ,- ,-
6 1 0 6,Z 7,Z ,- ,-
-3
0
8
Z 7
-4
2
7
From Z, can go to U, and X. Then assign
X Y labels to U, X by adding cost of Z
9 (in previous step – step 0) to w(Z,U) and
7,Z
w(Z,X). We denote
Solved node Solving node
L1(U) = 6; P1(U) = Z
Unsolved node L1(X) = 7; P1(X) = Z
Lecture 10 Discrete Mathematics
The University of New South Wales Page 24
Bellman-Ford Algorithm: Example
Step 2
n Z U X V Y
6,Z 5 4,X
0 0 ,- ,- ,- ,-
U V
-2 1 0 6,Z 7,Z ,- ,-
6 2 0 6,Z 7,Z 4,X 2,U
-3
0
8
Z 7
Recalculate U,X,V,Y by (L,P) in step 2:
-4
L2(U) = min{L1(Z)+w(Z,U),L1(V)+w(V,U)}
7
2 = min{0+6, } = 6; P2(U) = Z
L2(V) = min{L1(U)+w(U,V),L1(X)+w(X,V)}
X Y = min{6+5,7-3} = 4, P2(V) = X
9
7,Z 2,U L2(Y) = min{L1(U)+w(U,Y), L1(X)+w(X,Y),
Solved node Solving node = min{6-4,7+9} = 2; P2(Y) = U
L2(X) = min{L1(U)+w(U,X), L1(Z)+w(Z,X),
Unsolved node
= min{6+8,0+7} = 7; P2(X) = Z
Lecture 10 Discrete Mathematics
The University of New South Wales Page 25
Bellman-Ford Algorithm: Example
Step 3 n Z U X V Y
2,V 4,X
5 0 0 ,- ,- ,- ,-
U V
-2 1 0 6,Z 7,Z ,- ,-
Step 4 n Z U X V Y
2,V 5 4,X
0 0 ,- ,- ,- ,-
U V
-2 1 0 6,Z 7,Z ,- ,-
6 2 0 6,Z 7,Z 4,X 2,U
-3
3 0 2,V 7,Z 4,X 2,U
0
8 4 0 2,V 7,Z 4,X -2,U
Z 7
Recalculate U,X,V,Y by (L,P) in step 3:
-4
L4(U) = min{L3(Z)+w(Z,U),L3(V)+w(V,U)}
2 = min{0+6,4-2} = 2; P4(U) = V
7
L4(V) = min{L3(U)+w(U,V),L3(X)+w(X,V)}
X Y = min{2+5,7-3} = 4, P4(V) = X
9
7,Z 2,U L4(Y) = min{L3(U)+w(U,Y), L3(X)+w(X,Y),
Solved node Solving node = min{2-4,7+9} = -2; P4(Y) = U
L4(X) = min{L3(U)+w(U,X), L3(Z)+w(Z,X),
Unsolved node
= min{2+8,0+7} = 7; P4(X) = Z
Lecture 10 Discrete Mathematics
The University of New South Wales Page 27
Summary
2 Dijkstras Algorithm
3 Bellman-Ford Algorithm