# CS1022 Computer Programming & Principles

## Presentation on theme: "CS1022 Computer Programming & Principles"— Presentation transcript:

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

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

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

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

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

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

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 CS1022

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

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

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

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

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; 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

Warshall’s algorithm (4)
To calculate Wk from Wk – 1 proceed as follows Consider column k in Wk – 1 For each “F” row in this column, copy that row to Wk 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

Warshall’s algorithm (5)
Calculate reachability matrix of digraph W0  F T 2 1 3 5 4 CS1022

Warshall’s algorithm (6)
2 1 3 5 4 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 W1  F T CS1022

Warshall’s algorithm (7)
2 1 3 5 4 We now use step 3 – row 3 in W1 is Logical or of row 3 with row 1 of W0 W0  F T W1  F T W1  F T W1  F T W1  F T W1  F T W1  F T CS1022

Warshall’s algorithm (8)
2 1 3 5 4 We use step 3 again – row 5 in W1 is Logical or of row 5 with row 1 of W0 W0  F T F T F T F T F T F T W1  F T CS1022

Warshall’s algorithm (9)
2 1 3 5 4 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 F T F T F T W1  F T F T F T W2  F T F T CS1022

Warshall’s algorithm (10)
Notice entry (3, 3) Indicates a cycle from vertex 3 back to itself Going via vertices 1 and/or 2 W2  F T 2 1 3 5 4 CS1022

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 W3  T F CS1022

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

Shortest paths (2) Suppose the following weighted digraph B D C E F A
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 B D C E F A 1 2 3 4 5 CS1022

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

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

Weight matrix (2) A B C D E F 2  3 A B C D E F 2  3 1 4 A B C D E F
2 3 A B C D E F 2 3 1 4 A B C D E F 2 3 1 4 5 A B C D E F w  A B C D E F A B C D E F Our digraph is represented as B D C E F A 1 2 3 4 5 CS1022

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

Dijkstra’s algorithm (2)
B C D E F 2 3 1 4 5 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 Vertex to be marked Distance to vertex Unmarked vertices Step A B C D E F 2 3 B, C, D, E, F CS1022

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

Dijkstra’s algorithm (4)
B C D E F 2 3 1 4 5 Step 1 (Cont’d) 2nd row: smallest value of d[v] for unmarked vertices occurs for C and D Vertex to be marked Distance to vertex Unmarked vertices Step A B C D E F 2 3 B, C, D, E, F 1 6 C, D, E, F CS1022

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

Dijkstra’s algorithm (6)
B C D E F 2 3 1 4 5 Step 2 (Cont’d) Next row generated, in which smallest value of d[v] for unmarked vertices occurs for vertex C Vertex to be marked Distance to vertex Unmarked vertices Step A B C D E F 2 3 B, C, D, E, F 1 6 C, D, E, F 5 C, E, F CS1022

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 Vertex to be marked Distance to vertex Unmarked vertices Step A B C D E F 2 3 B, C, D, E, F 1 6 C, D, E, F 5 C, E, F 8 E, F CS1022

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 Vertex to be marked Distance to vertex Unmarked vertices Step A B C D E F 2 3 B, C, D, E, F 1 6 C, D, E, F 5 C, E, F 8 E, F 4 CS1022

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

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 u := unmarked vertex whose distance from A is minimal; Mark vertex u; for each unmarked vertex v with uv  E do d’ := d[u] + w(u, v); if d’ < d[v] then d[v] := d’; PATHTO(v) := PATHTO(u), v; CS1022

Further reading R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd (Chapter 8) Wikipedia’s entry on directed graphs Wikibooks entry on graph theory CS1022

Similar presentations