370 likes | 520 Views
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. Paths in digraphs (1). Directed graphs used to represent Routes of airlines
E N D
CS1022Computer 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 • M3M.M.M records all paths of length 3 • Mk records all paths of length k • Finally, the reachability matrix: M* M or M2 or ... orMn • 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 b a d c CS1022
Reachability matrix (4) • So we have • The 3 T entries in M2 indeed correspond to paths of length 2 in G, namely • abc • abd • 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 Gbe 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 Wnis 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 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) orW(k,j)) • Row i becomes the logical or of the current row i with current row k CS1022
Warshall’s algorithm (4) To calculate Wk from Wk – 1proceed as follows • Consider column k in Wk – 1 • For each “F” row in this column, copy that row toWk • For each “T” row in this column, form the logical or of that row with row k, and write the resulting row in Wk CS1022
Warshall’s algorithm (5) • Calculate reachability matrix of digraph 4 3 2 5 1 CS1022
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 4 3 2 5 1 CS1022
Warshall’s algorithm (7) • We now use step 3 – row 3 in W1 is • Logical or of row 3 with row 1 of W0 4 3 2 5 1 CS1022
Warshall’s algorithm (8) • We use step 3 again – row 5 in W1 is • Logical or of row 5 with row 1 of W0 4 3 2 5 1 CS1022
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 4 3 2 5 1 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 4 3 2 5 1 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 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 • 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 1 5 2 4 F A C B 3 1 2 D E 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) • Our digraph is represented as 1 5 2 4 F A C B 3 1 2 D E 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) 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 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 1 5 2 4 F A C B 3 1 2 D E CS1022
Dijkstra’s algorithm (4) Step 1 (Cont’d) • 2nd row: smallest value of d[v] for unmarked vertices occurs for C and D 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 1 5 2 4 F A C B 3 1 2 D E CS1022
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 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 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 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) CS1022
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