Sie sind auf Seite 1von 28

GRAPH - PART II

1 Shortest Path Algorithms

2 Dijkstras Algorithm

3 Bellman-Ford Algorithm

4 Summary

Lecture 10 Discrete Mathematics


The University of New South Wales Page 1
Lecture 10 Discrete Mathematics
The University of New South Wales Page 2
Introduction
• Problem when using a map application or a
navigation system is to get directions from one place
to another.
• Vertices = intersections
• Edges = roads
• Weights on the edges = the cost/ distance/ travel
time.
• One-way roads  consider edge-weighted directed
graphs.
 Problem: Find the lowest-cost way to get
from one vertex to another

Lecture 10 Discrete Mathematics


The University of New South Wales Page 3
Example: Navigation System

Vertices correspond to
intersections and edges
correspond to roads.

Weights on the edges that


model the cost, perhaps
distance or travel time.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 4
Example: Printed Circuit Board (PCB)

Autoroute Mode: Find the shortest


path for connections to minimize
resistances.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 5
Example: Game
Base under attack: Troops must
find the shortest path back home.

Game map:
Look like a
maze.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 6
Example: Uber Taxi

Fare Estimate: calculate the minimum fee for the trip.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 7
Properties of Shortest Path
• Paths are directed. A shortest path must respect the
direction of its edges.
• The weights are not necessarily distances. But the
weights might represent time or cost or an entirely different
variable and do not need to be proportional to a distance at
all.
• Not all vertices need be reachable. If t is not reachable
from s, there is no path at all, and therefore there is no
shortest path from s to t.
• For simplicity, our example graphs are strongly connected
(every vertex is reachable from every other vertex).

Lecture 10 Discrete Mathematics


The University of New South Wales Page 8
Properties of Shortest Path (con't)
• Negative weights introduce complications. For the moment, we
assume that edge weights are positive (or zero).
• Shortest paths are normally simple. Algorithms ignore zero-weight
edges that form cycles, so that the shortest paths they find have no
cycles.
• Shortest paths are not necessarily unique. There may be multiple
paths of the lowest weight from one vertex to another; we are content to
find any one of them.
• Parallel edges and self-loops may be present. Only the lowest-weight
among a set of parallel edges will play a role, and no shortest path
contains a self-loop (except possibly one of zero weight, which we
ignore). In the text, we implicitly assume that parallel edges are not
present for convenience in using the notation v->w to refer
unambiguously to the edge from v to w.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 9
Lecture 10 Discrete Mathematics
The University of New South Wales Page 10
Dijkstras Algorithm

• This algorithm can’t work with negative edge


weights.
• Dijkstras algorithm runs in O(|n|+|m|log|m|) where n
vertices and m edges.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 11
Dijkstras Algorithm: Example

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

Lecture 10 Discrete Mathematics


The University of New South Wales Page 12
Dijkstras Algorithm: Example

Step 1 Check all paths from A to


adjiacent vertices: F, G, B, C
0 2
A F
L(A) + w(A,G) = 1
3 1 4 5 L(A) + w(A,B) = 3
7 3 L(A) + w(A,F) = 2
L(A) + w(A,C) = 7
B G E
2 9
Set L(G) = 1
6 8 6

C D
3

Solved node Solving node Next vertice is G, add G to set S


Unsolved node S = {A,G}

Lecture 10 Discrete Mathematics


The University of New South Wales Page 13
Dijkstras Algorithm: Example

Step 2 Check all paths from A, G: F, B, E, D,


C.
0 2
A F
L(A) + w(A,B) = 3
3 1 4 5 L(A) + w(A,F) = 2
L(A) + w(A,C) = 7
7 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}

Lecture 10 Discrete Mathematics


The University of New South Wales Page 14
Dijkstras Algorithm: Example

Step 3 Check all paths from A, G, F to

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}

Lecture 10 Discrete Mathematics


The University of New South Wales Page 15
Dijkstras Algorithm: Example

Step 4 Check all paths from A, G, F, B to

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}

Lecture 10 Discrete Mathematics


The University of New South Wales Page 16
Dijkstras Algorithm: Example

Step 5 Check all paths from A, G, F, B, D to

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}

Lecture 10 Discrete Mathematics


The University of New South Wales Page 17
Dijkstras Algorithm: Example

Step 6 Check all paths from 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}

Lecture 10 Discrete Mathematics


The University of New South Wales Page 18
Dijkstras Algorithm: Example

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.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 19
Lecture 10 Discrete Mathematics
The University of New South Wales Page 20
Bellman-Ford Algorithm

• This algorithm can work with negative edge weights.


• Can detect a negative cycle.
• Bellman-Ford runs in O(|n|-|m|) where n vertices and
m edges.

Lecture 10 Discrete Mathematics


The University of New South Wales Page 21
Bellman-Ford Algorithm: Example

Find path from Z to all other vertices?


5
U V
-2
6
-3

8
Z 7
-4
2
7

X Y
9

Solved node Solving node


Unsolved node

Lecture 10 Discrete Mathematics


The University of New South Wales Page 22
Bellman-Ford Algorithm: Example

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

Lecture 10 Discrete Mathematics


The University of New South Wales Page 23
Bellman-Ford Algorithm: Example

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 ,- ,-

6 2 0 6,Z 7,Z 4,X 2,U


-3 3 0 2,V 7,Z 4,X 2,U
0
8
Z 7
Recalculate U,X,V,Y by (L,P) in step 3:
-4
L3(U) = min{L2(Z)+w(Z,U),L2(V)+w(V,U)}
2 = min{0+6,4-2} = 2; P3(U) = 2,V
7
L3(V) = min{L2(U)+w(U,V),L2(X)+w(X,V)}
X Y = min{6+5,7-3} = 4, P3(V) = 4,X
9
7,Z 2,U L3(Y) = min{L2(U)+w(U,Y), L2(X)+w(X,Y),
Solved node Solving node = min{6-4,7+9} = 2; P3(Y) = 2,U
L3(X) = min{L2(U)+w(U,X), L2(Z)+w(Z,X),
Unsolved node
= min{6+8,0+7} = 7; P3(X) = 7,Z
Lecture 10 Discrete Mathematics
The University of New South Wales Page 26
Bellman-Ford Algorithm: Example

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

1 Shortest Path Algorithms

2 Dijkstras Algorithm

3 Bellman-Ford Algorithm

Lecture 10 Discrete Mathematics


The University of New South Wales Page 28

Das könnte Ihnen auch gefallen