CS1022 Computer Programming & Principles Lecture 8.2 Digraphs (2) Plan of lecture • • • • • • Paths in digraphs Reachability matrix Warshall’s algorithm Shortest path Weight matrix Dijkstra’s algorithm CS1022 2 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! CS1022 3 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 CS1022 4 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 CS1022 5 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 CS1022 6 Reachability matrix (3) • Calculate the reachability matrix of digraph a b c d CS1022 F F M F F F F T T F F F F T F T F 7 Reachability matrix (4) • So we have F T F F 2 M F F F F F F T T F F F F T F F F F F F T T F F F F F F T F F T F F T T F T F F F F F F F • The 3 T entries in M2 indeed correspond to paths of length 2 in G, namely –abc –abd –bdc CS1022 8 Reachability matrix (5) • Further calculation gives F F T F F F F F F F F F F F F F 3 4 M M F F F F F F F F F F F F F F F F • Therefore, F T T T • For example, T in top-right F F T T corner of M* arises from * M2 and corresponds to M F F F F path a b d CS1022 F F T F 9 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 CS1022 10 Warshall’s algorithm (1) • Let G be a digraph with vertices v1, v2, , vn • Warshall’s algorithm generates sequence W0, 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* CS1022 11 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 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 CS1022 12 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 W := M; CS1022 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)) 13 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 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)) CS1022 14 Warshall’s algorithm (5) • Calculate reachability matrix of digraph 2 3 4 1 CS1022 5 F F W0 T F T T F F F F F T F F T F F T F F F F F F F 15 Warshall’s algorithm (6) 2 3 4 • We now calculate W1: – Using step 1 we consider column 1 of W0 1 – Using step 2 we copy rows 1, 2 and 4 directly to W1 5 F T F F F F F T F F W1 F F F F F CS1022 16 Warshall’s algorithm (7) 3 2 4 • We now use step 3 – row 3 in W1 is – Logical or of row 3 with row 1 of W0 F F W0 T F T CS1022 T F F F F F T F F T F F T F F F F F F F F F W1 T F 5 1 T F T F F T F F F F T F F F F F 17 Warshall’s algorithm (8) 3 2 4 • We use step 3 again – row 5 in W1 is – Logical or of row 5 with row 1 of W0 F F W0 T F T CS1022 T F F F F F T F F T F F T F F F F F F F F F W1 T F T 5 1 T F T F T F T F F T F F T F F F F F F F 18 Warshall’s algorithm (9) 3 2 4 • We now compute W2 from W1 1 – 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 F F W1 T F T CS1022 T F T F T F T F F T F F T F F F F F F F F F W2 T F T T F T F T T T T F T 5 F F T F F F F F F F 19 Warshall’s algorithm (10) • Notice entry (3, 3) – Indicates a cycle from vertex 3 back to itself – Going via vertices 1 and/or 2 F F W2 T F T CS1022 T F T F T T T T F T F F T F F F F F F F 2 3 4 1 5 20 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 T T W3 T F T CS1022 T T T F T T T T F T T T T F T F F F F F 21 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) CS1022 22 Shortest paths (2) • Suppose the following weighted digraph 1 C B 5 2 4 A F 3 D 2 1 E – 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 CS1022 23 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 CS1022 Edsger W. Dijkstra 24 Weight matrix (1) • A compact way to represent weighted digraphs • Matrix w, whose entries w(u, v) are given by 0 if u v w(u, v) if uv is not an arc d if uv is an arc of weight d CS1022 25 Weight matrix (2) • Our digraph is represented as B 1 2 C 5 4 A F 3 D 2 1 E A B w C D E F A B 0 2 0 C DE F 3 14 0 5 0 2 0 1 0 0 if u v w(u, v) if uv is not an arc d if uv is an arc of weight d CS1022 26 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 CS1022 27 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 A B C D E F A 0 B 2 0 C 1 0 D 3 0 E 4 2 0 F 5 1 0 Vertex to Distance to vertex Unmarked Step be marked A B C D E F vertices 0 A 0 2 3 B, C, D, E, F CS1022 28 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 B C 1 2 4 A F 3 1 D CS1022 5 2 E 29 Dijkstra’s algorithm (4) Step 1 (Cont’d) – 2nd row: smallest value of d[v] for unmarked vertices occurs for C and D A B C D E F A 0 B 2 0 C 1 0 D 3 0 E 4 2 0 F 5 1 0 Vertex to Distance to vertex Unmarked Step be marked A B C D E F vertices 0 A 0 2 3 B, C, D, E, F 1 B 0 2 3 3 6 C, D, E, F CS1022 30 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 B 1 2 C 4 A F 3 D CS1022 5 2 1 E 31 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 Step 0 1 2 CS1022 Vertex to be marked A B D A B C D E F Distance to vertex A B C D E F 0 2 3 0 2 3 3 6 0 2 3 3 5 A 0 B 2 0 C 1 0 D 3 0 E 4 2 0 F 5 1 0 Unmarked vertices B, C, D, E, F C, D, E, F C, E, F 32 Dijkstra’s algorithm (7) Step 3 – We mark vertex C and recompute distances – Vertex F can be accessed for the first time via path ABCE – So d[F] = 8, and two unmarked vertices remain Vertex to Distance to vertex Unmarked Step be marked A B C D E F vertices 0 A 0 2 3 B, C, D, E, F 1 B 0 2 3 3 6 C, D, E, F 2 D 0 2 3 3 5 C, E, F 3 C 0 2 3 3 5 8 E, F CS1022 33 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 Step 0 1 2 3 4 5 CS1022 Vertex to be marked A B D C E F Distance to vertex A B C D E F 0 2 3 0 2 3 3 6 0 2 3 3 5 0 2 3 3 5 8 0 2 3 3 5 6 0 2 3 3 5 6 Unmarked vertices B, C, D, E, F C, D, E, F C, E, F E, F F 34 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 CS1022 35 Dijkstra’s algorithm (10) 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 end CS1022 36 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 CS1022 37
© Copyright 2026 Paperzz