Download presentation

Presentation is loading. Please wait.

Published byLaney Weast Modified over 2 years ago

1
CS1022 Computer Programming & Principles Lecture 8.2 Digraphs (2)

2
Plan of lecture Paths in digraphs Reachability matrix Warshall’s algorithm Shortest path Weight matrix Dijkstra’s algorithm 2 CS1022

3
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! 3 CS1022

4
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 4 CS1022

5
Reachability matrix (1) The logical Boolean product of M with itself is M 2 – A T entry indicates a path of length 2 M 3 M.M.M records all paths of length 3 M k records all paths of length k Finally, the reachability matrix: M * M or M 2 or... or M n – Records the existence of paths of some length between vertices 5 CS1022

6
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 6 CS1022

7
Reachability matrix (3) Calculate the reachability matrix of digraph 7 CS1022 a c b d

8
Reachability matrix (4) So we have The 3 T entries in M 2 indeed correspond to paths of length 2 in G, namely – a b c – a b d – b d c 8 CS1022

9
Reachability matrix (5) Further calculation gives Therefore, 9 CS1022 For example, T in top-right corner of M * arises from M 2 and corresponds to path a b d

10
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 10 CS1022

11
Warshall’s algorithm (1) Let G be a digraph with vertices v 1, v 2, , v n Warshall’s algorithm generates sequence W 0, W 1, W 2, , W n (where W 0 = M) For k 1, entries in matrix W k are W k (i, j) = T if, and only if, there is a path (of any length) from v i to v j Intermediary vertices in path are in v 1, v 2, , v k Matrix W 0 is the original adjacency matrix M Matrix W n is the reachability matrix M * 11 CS1022

12
Warshall’s algorithm (2) Clever use of nested for-loops – very elegant Each pass of outer loop generates matrix W k This is done by updating entries in matrix W k– 1 12 CS1022 begin W := M; for k 1 to n do for i 1 to n do for j 1 to n do W(i, j) := W(i, j) or (W(i, k) and W(k, j)) end

13
Warshall’s algorithm (3) To find row i of W k 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 13 CS1022 W := M; for k 1 to n do for i 1 to n do for j 1 to n do W(i, j) := W(i, j) or (W(i, k) and W(k, j))

14
Warshall’s algorithm (4) To calculate W k from W k – 1 proceed as follows 1.Consider column k in W k – 1 2.For each “F” row in this column, copy that row to W k 3.For each “T” row in this column, form the logical or of that row with row k, and write the resulting row in W k 14 CS1022 W := M; for k 1 to n do for i 1 to n do for j 1 to n do W(i, j) := W(i, j) or (W(i, k) and W(k, j))

15
Warshall’s algorithm (5) Calculate reachability matrix of digraph 15 CS1022 2 1 3 5 4 W 0 FTFFF FFTFF TFFTF FFFFF TFTFF

16
Warshall’s algorithm (6) We now calculate W 1 : – Using step 1 we consider column 1 of W 0 – Using step 2 we copy rows 1, 2 and 4 directly to W 1 16 CS1022 2 1 3 5 4 W1 W1 FTFFF FFTFF FFFFF

17
Warshall’s algorithm (7) We now use step 3 – row 3 in W 1 is – Logical or of row 3 with row 1 of W 0 17 CS1022 W1 W1 FTFFF FFTFF FFFFF W 0 FTFFF FFTFF TFFTF FFFFF TFTFF W1 W1 FTFFF FFTFF T FFFFF W1 W1 FTFFF FFTFF TT FFFFF W1 W1 FTFFF FFTFF TTF FFFFF W1 W1 FTFFF FFTFF TTFTF FFFFF W1 W1 FTFFF FFTFF TTFTF FFFFF 2 1 3 5 4

18
Warshall’s algorithm (8) We use step 3 again – row 5 in W 1 is – Logical or of row 5 with row 1 of W 0 18 CS1022 W 0 FTFFF FFTFF TFFTF FFFFF TFTFF W1 W1 FTFFF FFTFF TTFTF FFFFF 2 1 3 5 4 FTFFF FFTFF TTFTF FFFFF T FTFFF FFTFF TTFTF FFFFF TT FTFFF FFTFF TTFTF FFFFF TTT FTFFF FFTFF TTFTF FFFFF TTTFF FTFFF FFTFF TTFTF FFFFF TTTFF

19
Warshall’s algorithm (9) We now compute W 2 from W 1 – Copy rows 2 and 4 to W 2 – Row 1 in W 2 is logical or of rows 1 and 2 of W 1 – Row 3 in W 2 is logical or of rows 3 and 2 of W 1 – Row 5 in W 2 is logical or of rows 5 and 2 of W 1 19 CS1022 W1 W1 FTFFF FFTFF TTFTF FFFFF TTTFF W2 W2 FFTFF FFFFF 2 1 3 5 4 FTFFF FFTFF TTFTF FFFFF TTTFF FTTFF FFTFF FFFFF FTFFF FFTFF TTFTF FFFFF TTTFF FTTFF FFTFF TTTTF FFFFF FTFFF FFTFF TTFTF FFFFF TTTFF FTTFF FFTFF TTTTF FFFFF TTTFF

20
Warshall’s algorithm (10) Notice entry (3, 3) – Indicates a cycle from vertex 3 back to itself – Going via vertices 1 and/or 2 20 CS1022 W2 W2 FTTFF FFTFF TTTTF FFFFF TTTFF 2 1 3 5 4

21
Warshall’s algorithm (11) W 3 is computed similarly: – Since no arcs lead out of vertex 4, W 4 is the same as W 3 – For a similar reason, W 5 is the same as W 4 – So W 3 is reachability matrix 21 CS1022 W3 W3 TTTTF TTTTF TTTTF FFFFF TTTTF

22
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) 22 CS1022

23
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 23 CS1022 B D C E F A 1 2 3 4 2 1 5

24
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 24 CS1022 Edsger W. Dijkstra

25
Weight matrix (1) A compact way to represent weighted digraphs Matrix w, whose entries w(u, v) are given by 25 CS1022

26
Weight matrix (2) Our digraph is represented as 26 CS1022 B D C E F A 1 2 3 4 2 1 5 w w ABCDEF A B C D E F ABCDEF A0 B0 C0 D0 E0 F0 ABCDEF A0 B 0 C 0 D 0 E 0 F 0 ABCDEF A02 3 B 0 C 0 D 0 E 0 F 0 ABCDEF A02 3 B 01 4 C 0 D 0 E 0 F 0 ABCDEF A02 3 B 01 4 C 0 5 D 02 E 01 F 0

27
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 27 CS1022

28
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 28 CS1022 ABCDEF A02 3 B 01 4 C 0 5 D 02 E 01 F 0 Vertex to be marked Distance to vertexUnmarked vertices StepABCDEF 0A02 3 B, C, D, E, F

29
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 29 CS1022 B D C E F A 1 2 3 4 2 1 5

30
Dijkstra’s algorithm (4) Step 1 (Cont’d) – 2 nd row: smallest value of d[v] for unmarked vertices occurs for C and D 30 CS1022 ABCDEF A02 3 B 01 4 C 0 5 D 02 E 01 F 0 Vertex to be marked Distance to vertexUnmarked vertices StepABCDEF 0A02 3 B, C, D, E, F 1B02336 C, D, E, F

31
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 31 CS1022 B D C E F A 1 2 3 4 2 1 5

32
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 32 CS1022 ABCDEF A02 3 B 01 4 C 0 5 D 02 E 01 F 0 Vertex to be marked Distance to vertexUnmarked vertices StepABCDEF 0A02 3 B, C, D, E, F 1B02336 C, D, E, F 2D02335 C, E, F

33
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 33 CS1022 Vertex to be marked Distance to vertex Unmarked verticesStep ABCDEF 0A02 3 B, C, D, E, F 1B02336 C, D, E, F 2D02335 C, E, F 3C023358E, F

34
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 34 CS1022 Vertex to be marked Distance to vertex Unmarked verticesStep ABCDEF 0A02 3 B, C, D, E, F 1B02336 C, D, E, F 2D02335 C, E, F 3C023358E, F 4E023356F 5F023356

35
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 35 CS1022

36
Dijkstra’s algorithm (10) 36 CS1022 for each v V do begin d[v] := w(A, v); PATHTO(v) := A; end Mark vertex A; while unmarked vertices remain do begin u := unmarked vertex whose distance from A is minimal; Mark vertex u; for each unmarked vertex v with uv E do begin d’ := d[u] + w(u, v); if d’ < d[v] then begin d[v] := d’; PATHTO(v) := PATHTO(u), v ; end

37
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 37 CS1022

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google