CS1022 Computer Programming & Principles Lecture 8.2 Digraphs (2)
-
Upload
laney-weast -
Category
Documents
-
view
221 -
download
0
Transcript of CS1022 Computer Programming & Principles Lecture 8.2 Digraphs (2)
CS1022 Computer Programming &
Principles
Lecture 8.2Digraphs (2)
Plan of lecture• Paths in digraphs• Reachability matrix• Warshall’s algorithm• Shortest path• Weight matrix• Dijkstra’s algorithm
2CS1022
Paths in digraphs (1)• Directed graphs used to represent– Routes of airlines– Connections between networked computers
• What would happen if a link (vertex or arc) is lost?– If city is unreachable (due to poor weather) and a plane
needs refuelling, it may be impossible to re-route plane– If a path in a computer network is lost, users may no
longer be able to access certain file server• Problem: is there a path between two vertices of a
digraph?– Solution: try every combination of edges...– We can do better than this!
3CS1022
Paths in digraphs (2)• Let G (V, E) be a digraph with n vertices (|V| n)• Let M be its adjacency matrix– A T entry in the matrix represents an arc in G– An arc is a path of length 1
4CS1022
Reachability matrix (1)• The logical Boolean product of M with itself is M2
– A T entry indicates a path of length 2• M3 M.M.M records all paths of length 3• Mk records all paths of length k• Finally, the reachability matrix:
M* M or M2 or ... or Mn – Records the existence of paths of some length between
vertices
5CS1022
Reachability matrix (2)• Logical or of two matrices is the result of forming
the logical or of corresponding entries– This requires that both matrices have the same number
of rows and same number of columns• Reachability matrix of G (V, E) is in fact adjacency
matrix of the transitive closure E* on E
6CS1022
Reachability matrix (3)• Calculate the reachability matrix of digraph
7CS1022
a
c
b
d
FF
F
F
FTFFFFFFTTFFFFTF
M
Reachability matrix (4)• So we have
• The 3 T entries in M2 indeed correspond to paths of length 2 in G, namely– a b c – a b d– b d c
8CS1022
FF
F
F
FFFFFFFFFTFFTTFF
FTFFFFFFTTFFFFTF
FTFFFFFFTTFFFFTF
2M
Reachability matrix (5)• Further calculation gives
• Therefore,
9CS1022
FF
F
F
FFFFFFFFFFFFFTFF
3M
FFFFFFFFFFFFFFFF
4M
FTFFFFFFTTFFTTTF
*M
• For example, T in top-right corner of M* arises from M2 and corresponds to path a b d
Reachability matrix (6)• For large digraphs, calculating M* via higher and
higher powers of M is laborious and inefficient• A more efficient way is Warshall’s algorithm
10CS1022
FF
F
F
Warshall’s algorithm (1)• Let G be a digraph with vertices v1, v2, , vn
• Warshall’s algorithm generates sequenceW0, W1, W2, , Wn (where W0 = M)
• For k 1, entries in matrix Wk are Wk(i, j) = T if, and only if, there is a path (of any length) from vi to vj
• Intermediary vertices in path are in v1, v2, , vk
• Matrix W0 is the original adjacency matrix M
• Matrix Wn is the reachability matrix M*
11CS1022
FF
F
F
Warshall’s algorithm (2)• Clever use of nested for-loops – very elegant• Each pass of outer loop generates matrix Wk
• This is done by updating entries in matrix Wk– 1
12CS1022
FF
F
Fbegin
W := M; for k 1 to n do
for i 1 to n dofor j 1 to n do
W(i, j) := W(i, j) or (W(i, k) and W(k, j))end
Warshall’s algorithm (3)• To find row i of Wk we evaluate
W(i, j) := W(i, j) or (W(i, k) and W(k, j))• If W(i, k) = F then (W(i, k) and W(k, j)) = F– So expression depends on W(i, j)– Row i remains unchanged
• Otherwise, W(i, k) = T – Expression reduces to (W(i, j) or W(k, j))– Row i becomes the logical or of the current row i with
current row k
13CS1022
FF
F
F
W := M; for k 1 to n do
for i 1 to n dofor j 1 to n do
W(i, j) := W(i, j) or (W(i, k) and W(k, j))
Warshall’s algorithm (4)To calculate Wk from Wk – 1 proceed as follows
1. Consider column k in Wk – 1
2. For each “F” row in this column, copy that row to Wk
3. For each “T” row in this column, form the logical or of that row with row k, and write the resulting row in Wk
14CS1022
W := M; for k 1 to n dofor i 1 to n do
for j 1 to n doW(i, j) := W(i, j) or (W(i, k) and W(k, j))
Warshall’s algorithm (5)• Calculate reachability matrix of digraph
15CS1022
2
1
3
5
4 W0
F T F F FF F T F FT F F T FF F F F FT F T F F
Warshall’s algorithm (6)• We now calculate W1:– Using step 1 we consider column 1 of W0
– Using step 2 we copy rows 1, 2 and 4 directly to W1
16CS1022
2
1
3
5
4
W1
F T F F FF F T F F
F F F F F
Warshall’s algorithm (7)• We now use step 3 – row 3 in W1 is – Logical or of row 3 with row 1 of W0
17CS1022
W1
F T F F FF F T F F
F F F F FW0
F T F F FF F T F FT F F T FF F F F FT F T F F
W1
F T F F FF F T F FTF F F F F
W1
F T F F FF F T F FT TF F F F F
W1
F T F F FF F T F FT T FF F F F F
W1
F T F F FF F T F FT T F T FF F F F F
W1
F T F F FF F T F FT T F T FF F F F F
2
1
3
5
4
Warshall’s algorithm (8)• We use step 3 again – row 5 in W1 is – Logical or of row 5 with row 1 of W0
18CS1022
W0
F T F F FF F T F FT F F T FF F F F FT F T F F
W1
F T F F FF F T F FT T F T FF F F F F
2
1
3
5
4
F T F F FF F T F FT T F T FF F F F FT
F T F F FF F T F FT T F T FF F F F FT T
F T F F FF F T F FT T F T FF F F F FT T T
F T F F FF F T F FT T F T FF F F F FT T T F F
F T F F FF F T F FT T F T FF F F F FT T T F F
Warshall’s algorithm (9)• We now compute W2 from W1
– Copy rows 2 and 4 to W2
– Row 1 in W2 is logical or of rows 1 and 2 of W1
– Row 3 in W2 is logical or of rows 3 and 2 of W1
– Row 5 in W2 is logical or of rows 5 and 2 of W1
19CS1022
W1
F T F F FF F T F FT T F T FF F F F FT T T F F
W2 F F T F F
F F F F F
2
1
3
5
4
F T F F FF F T F FT T F T FF F F F FT T T F F
F T T F FF F T F F
F F F F F
F T F F FF F T F FT T F T FF F F F FT T T F F
F T T F FF F T F FT T T T FF F F F F
F T F F FF F T F FT T F T FF F F F FT T T F F
F T T F FF F T F FT T T T FF F F F FT T T F F
Warshall’s algorithm (10)• Notice entry (3, 3)– Indicates a cycle from vertex 3 back to itself– Going via vertices 1 and/or 2
20CS1022
W2
F T T F FF F T F FT T T T FF F F F FT T T F F
2
1
3
5
4
Warshall’s algorithm (11)• W3 is computed similarly:– Since no arcs lead out of vertex 4, W4 is the same as W3
– For a similar reason, W5 is the same as W4
– So W3 is reachability matrix
21CS1022
W3
T T T T FT T T T FT T T T FF F F F FT T T T F
Shortest paths (1)• Given a digraph with weighted arcs• Find a path between two vertices which has the
lowest sum of weights of the arcs travelled along–Weights can be costs, petrol, etc.–We make it simple and think of distances– Hence the “shortest path”, but it could be “cheapest”– You might also want to maximise sum (e.g., taxi drivers)
22CS1022
Shortest paths (2)• Suppose the following weighted digraph
– Not so many vertices and arcs–We could list all possible paths between any two vertices• We then pick the one with lowest sum of weights of arcs
– In real-life scenarios there are too many possibilities–We need more efficient ways to find shortest path
23CS1022
B
D
C
E
FA
1
2
3
4
21
5
Shortest paths (3)• Dijkstra’s algorithm• Let’s see its “effect” with previous digraph• Problem: – Find shortest path between A and other vertices– Shortest = “minimal total weight” between two vertices– Total weight is sum of individual weights of arcs in path
24CS1022 Edsger W. Dijkstra
Weight matrix (1)• A compact way to represent weighted digraphs• Matrix w, whose entries w(u, v) are given by
25CS1022
duv
uv
vu
d
vuw
weightof arc an is if arc an not is if
if
0
),(
Weight matrix (2)• Our digraph is represented as
26CS1022
duv
uv
vu
d
vuw
weightof arc an is if arc an not is if
if
0
),(
B
D
C
E
FA
1
2
3
4
21
5
w
A B C D E FABCDEF
A B C D E FA 0B 0C 0D 0E 0F 0
A B C D E FA 0 B 0 C 0 D 0 E 0F 0
A B C D E FA 0 2 3 B 0 C 0 D 0 E 0F 0
A B C D E FA 0 2 3 B 0 1 4 C 0 D 0 E 0F 0
A B C D E FA 0 2 3 B 0 1 4 C 0 5D 0 2 E 0 1F 0
Dijkstra’s algorithm (1)• For each vertex v in digraph we assign a label d[v]– d[v] represents distance from A to v– Initially d[v] is the weight of arc (A, v) if it exists– Otherwise d[v]
• We traverse vertices and improve d[v] as we go• At each step of the algorithm a vertex u is marked– This is done when we are sure we found a best route to it
• For remaining unmarked vertices v,– Label d[v] is replaced by minimum of its current value and
distance to v via last marked vertex u• Algorithm terminates when – All vertices have received their final label and– All possible vertices have been marked
27CS1022
Dijkstra’s algorithm (2)Step 0–We start at A so we mark it and use
first row of w for initial values of d[v]– Smallest value is d[B] = 2
28CS1022
A B C D E FA 0 2 3 B 0 1 4 C 0 5D 0 2 E 0 1F 0
Vertex tobe marked
Distance to vertexUnmarked
verticesStep A B C D E F
0 A 0 2 3 B, C, D, E, F
Dijkstra’s algorithm (3)Step 1–Mark B since it is the closest unmarked vertex to A– Calculate distances to unmarked vertices via B• If a shorter distance is found use this
– In our case, • A B C has weight 3• A B E has weight 6• Notice that previously d[C] and d[E] were
29CS1022
B
D
C
E
FA
1
2
3
4
21
5
Dijkstra’s algorithm (4)Step 1 (Cont’d)– 2nd row: smallest value of d[v] for
unmarked vertices occurs for C and D
30CS1022
A B C D E FA 0 2 3 B 0 1 4 C 0 5D 0 2 E 0 1F 0
Vertex tobe marked
Distance to vertexUnmarked
verticesStep A B C D E F
0 A 0 2 3 B, C, D, E, F1 B 0 2 3 3 6 C, D, E, F
Dijkstra’s algorithm (5)Step 2– Of remaining unmarked vertices D and C are closest to A– Choose one of them (say, D)– Path A D E has weight 5, so current value of d[E] can
be updated to 5
31CS1022
B
D
C
E
FA
1
2
3
4
21
5
Dijkstra’s algorithm (6)Step 2 (Cont’d)– Next row generated, in which smallest
value of d[v] for unmarked vertices occurs for vertex C
32CS1022
A B C D E FA 0 2 3 B 0 1 4 C 0 5D 0 2 E 0 1F 0
Vertex tobe marked
Distance to vertexUnmarked
verticesStep A B C D E F
0 A 0 2 3 B, C, D, E, F1 B 0 2 3 3 6 C, D, E, F2 D 0 2 3 3 5 C, E, F
Dijkstra’s algorithm (7)Step 3–We mark vertex C and recompute distances– Vertex F can be accessed for the first time via path
A B C E– So d[F] = 8, and two unmarked vertices remain
33CS1022
Vertex tobe marked
Distance to vertexUnmarked
verticesStep A B C D E F
0 A 0 2 3 B, C, D, E, F1 B 0 2 3 3 6 C, D, E, F2 D 0 2 3 3 5 C, E, F3 C 0 2 3 3 5 8 E, F
Dijkstra’s algorithm (8)Step 4 and 5–We mark vertex E next, which reduces the distance to F
from 8 to 6– Finally, mark F
34CS1022
Vertex tobe marked
Distance to vertexUnmarked
verticesStep A B C D E F
0 A 0 2 3 B, C, D, E, F1 B 0 2 3 3 6 C, D, E, F2 D 0 2 3 3 5 C, E, F3 C 0 2 3 3 5 8 E, F4 E 0 2 3 3 5 6 F5 F 0 2 3 3 5 6
Dijkstra’s algorithm (9)• Input: G = (V, E) and A V– Finds shortest path from A to all v V – For any u and v, w(u, v) is the weight of the arc uv– PATHTO(v) stores the current shortest path from A to v
35CS1022
Dijkstra’s algorithm (10)
36CS1022
for each v V dobegin
d[v] := w(A, v);PATHTO(v) := A;
endMark vertex A;while unmarked vertices remain dobegin
u := unmarked vertex whose distance from A is minimal;Mark vertex u;for each unmarked vertex v with uv E dobegin
d’ := d[u] + w(u, v);if d’ < d[v] thenbegin
d[v] := d’;PATHTO(v) := PATHTO(u), v;
endend
Further reading• R. Haggarty. “Discrete Mathematics for
Computing”. Pearson Education Ltd. 2002. (Chapter 8)
• Wikipedia’s entry on directed graphs• Wikibooks entry on graph theory
37CS1022