1 / 28

Chapter 8: Directed Graphs

Chapter 8: Directed Graphs. digraph:. A digraph is a graph G = (V,E) where V is a finite set of vertices and E is a relation on V . If a, b ∈ V and aEb then there is a directed edge of E from a to b called an arc .

Download Presentation

Chapter 8: Directed Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8: Directed Graphs Discrete Math for CS

  2. Discrete Math for CS digraph: • A digraph is a graph G = (V,E) where V is a finite set of vertices and E is a relation on V. • If a, b ∈ V and aEb then there is a directed edge of E from a to b called an arc. • If u, v ∈ V and (u,v) is an arc we write uv as the arc name. • A simple digraph has no loops and no multiple edges. • If uv is an arc then u is the antecedent of v.

  3. Discrete Math for CS Example: V = { a, b, c, d } E = { ab, cb, dc, bd, db } a b a b c d a 0 1 0 0 b 0 0 0 1 c 0 1 0 0 d 0 1 1 0 c d

  4. Discrete Math for CS More Notation: • A path of length k in a digraph is a sequence of distinct vertices v1, v2, v3, ..., vk where vi-1vi is an arc for i = 1, ... k. • A cycle is a path where v1 = vk and no other vertices are the same. • A graph without cycles is called acyclic. • Directed acyclic graphs (DAGs) are some of the most important graphs. • In task-scheduling problems a DAG is called a PERT chart.

  5. Discrete Math for CS Example: • A student needs to take 8 courses to satisfy a major. The courses and their prerequisites are given below. Draw a PERT chart showing the order in which the courses can be taken.

  6. Discrete Math for CS Answer: A H G B C F D E

  7. Discrete Math for CS Topological Sort Algorithm • A topological sort algorithm produces a consistent labeling of the edges of the above graph. • A labeling 1, 2, 3, ... , n is consisent if uv is an arc, u has label i, v has label j and i < j.

  8. Discrete Math for CS TSA: • G = (V,E) is a digraph. • Let A(v) = { all antecedents of v }. begin for v V do calculate A(v); label := 0; while unlabeled vertices v remain for which A(v) = ∅do begin label := label + 1; u := a vertex with A(u) = ∅ ; assign label to u; for each unlabeled vertex v ∈ V do a(v) := A(v) \ {u} end end end

  9. Discrete Math for CS Example: • Find a consistent labeling of the previous graph. Step 0: A(A) = {B}, A(B) = {C}, A(C) = {H}, A(D) = {C}, A(E) = {D, G}, A(F) = {E}, A(G) = {C}, A(H) = ∅. Step 1: Assign label 1 to H since A(H) = ∅. A(A) = {B}, A(B) = {C}, A(C) = ∅, A(D) = {C}, A(E) = {D, G}, A(F) = {E}, A(G) = {C}, A(H) = ∅. Step 2: Assign label 2 to C since A(C) = ∅. A(A) = {B}, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {D, G}, A(F) = {E}, A(G) =∅, A(H) = ∅. Step 3: Choose one of the possibilities> Assign label 3 to B. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {D, G}, A(F) = {E}, A(G) =∅, A(H) = ∅. Step 4: Assign label 4 to A. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {D, G}, A(F) = {E}, A(G) =∅, A(H) = ∅. Step 5: Assign label 5 to D. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = {G}, A(F) = {E}, A(G) =∅, A(H) = ∅. Step 6: Assign label 6 to G. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = ∅, A(F) = {E}, A(G) =∅, A(H) = ∅. Step 7: Assign label 7 to E. A(A) = ∅, A(B) =∅, A(C) = ∅, A(D) =∅, A(E) = ∅, A(F) = ∅, A(G) =∅, A(H) = ∅. Step 8: Assign label 8 to F. Consistent Labeling is H, C, B, A, D, G, E, F

  10. Discrete Math for CS Paths in Digraphs • Directed paths can represent things line airline routes or networked computers. • We may need to know alternative routes if a link goes down. • So we need to know if there exists a path between two vertices of a digraph. • G = (V,E) with n vertices. Let M be the adjacency matrix. If mij == 1 then there is an arc from vertex vito vertex vj. An arc is a path of length 1. • Consider M2. The boolean product of M by M yields a matrix which shows paths of length 2 in the original graph.

  11. Discrete Math for CS Example: V = { a, b, c, d } E = { ab, cb, dc, bd, db } a b a b c d a 0 1 0 0 b 0 0 0 1 c 0 1 0 0 d 0 1 1 0 c d M a b c d a 0 0 0 1 b 0 1 1 0 c 0 0 0 1 d 0 1 0 1 M2

  12. Discrete Math for CS Reachability Matrix • Calculate M* = M or M2 or M3 or ... or Mn • M* contains all paths of all lengths so shows what vertices are reachable from what vertices. • The reachability matrix of a graph is the graph of the transitive closure of its adjacency matrix.

  13. Discrete Math for CS Example: a b c d a 0 1 0 0 b 0 0 1 1 c 0 0 0 0 d 0 0 1 0 M a b a b c d a 0 0 1 1 b 0 0 1 0 c 0 0 0 0 d 0 0 0 0 c d M2 a b c d a 0 0 1 0 b 0 0 0 0 c 0 0 0 0 d 0 0 0 0 a b c d a 0 0 0 0 b 0 0 0 0 c 0 0 0 0 d 0 0 0 0 M3 M4

  14. Discrete Math for CS Example: a b a b c d a 0 1 1 1 b 0 0 1 1 c 0 0 0 0 d 0 0 1 0 c d M*

  15. Discrete Math for CS Large Matrices • This calculation is labourious for big matrices. • Warshall's Algorithm calculates M* more efficiently. • G = (V,E). with vertices v1, v2, ..., vn. Warshall's Algorithm generates matrices W0 = M, W1, W2, ..., Wn. • For k >= 1, Wk(i,j) = 1 iff there is a path of any length from vi to vjwhere the intermediary vertices in the path lie in the set {v1, ... vk}. • Wn = M*. • Warshall's Algorithm is efficient by a clever use of for-loops. • Successive passes of the outer loop calculate W1, W2, ..., Wn.

  16. Discrete Math for CS Warshall's Algorithm • G = (V,E). M is the adjacency matrix. Calculates W = M*. 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 Note: On each pass of outer loop the algorithm generates Wk. This is done by updating entries in Wk-1. To find ithrow of Wk we evaluate W(i,j) = W(i,j) or W(i,k) and W(k,j); (*)‏ for various values of j. If W(i,k) = 0 then (W(i,k) and W(k,j)) = 0 and so (*) reduces to W(i,j); ie, row i of the matrix remains unchanged. Otherwise W(i,k) = 1 and (*) reduces to W(i,j) or W(k,j). In this case row i becomes the logical or of the current row i and current row k.

  17. Discrete Math for CS Warshall's Algorithm: • So Warshall's Algorithm reduces to calculating Wkfrom Wk-1 as follows: • Consider the kthcolumn of Wk. • For each row with a 0 entry in this column, copy the row from Wk-1. • For each row with a 1 entry form the logical or of that row with row k and write the resulting row in Wk.

  18. Discrete Math for CS Example (Warshall's Algorithm): 0 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 2 3 W0 4 copy rows 1,2,4 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 5 W1 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 1 1 1 0 0 W1 W1 row 3 or row 1 row 5 or row 1

  19. Discrete Math for CS Example (Warshall's Algorithm): 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 1 1 1 0 0 W1 2 3 4 0 0 1 0 0 0 0 0 0 0 W2 1 5 0 1 1 0 0 0 0 1 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 W2

  20. Discrete Math for CS Example (Warshall's Algorithm): 0 1 1 0 0 0 0 1 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 W2 2 3 4 0 0 0 0 0 W3 1 5 Note: W4 = W3 so we are done. 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 W3

  21. Discrete Math for CS Shortest Paths • Find the shortest path between two vertices in a weighted digraph. • Typical situations – transportation networks, communications networks

  22. Discrete Math for CS Shortest Paths: Find the shortest path from A to any other vertex. B C 1 2 5 A F 4 3 1 weight matrix: 2 E D 0 if u = v ∞ if uv is not an arc d if uv is an arc of weight d w(u,v) = A B C D E F A 0 2 ∞ 3 ∞ ∞ B ∞ 0 1 ∞ 4 ∞ C ∞ ∞ 0 ∞ ∞ 5 D ∞ ∞ ∞ 0 2 ∞ E ∞ ∞ ∞ ∞ 0 1 F ∞ ∞ ∞ ∞ ∞ O

  23. Discrete Math for CS Idea: • Initially define d[v] to be the weight of an arc from A to v. • d[v] = ∞ if there is no arc. • We traverse the vertices and improve d[v] as we go. • We mark a value for d[u] once we know for sure the shortest route to u from A. • For the remaining vertices, w, we assign the min of the current value of d[w] and the distance to w via the last marked vertex, u. • The algorithm terminates once all vertices that can be marked are marked.

  24. Discrete Math for CS Step 0: Mark A and let the first row represent the initial values of d[v]. Step 1: Mark B since it is closest to A. Calculate the distances to unmarked vertices via B. If a shorter distance is found, use it.. Vertices not adjacent to the last marked have their d[v] values unchanged. Step 2: Next mark D (we could mark C too). Calculate the remaining distances Step 3: Mark C. F can now be accessed. Step 4 and 5: Mark E and F.

  25. Discrete Math for CS Dijkstra's Algorithm • G = (V,E) is a weighted digraph, A is a vertex. The algorithm finds the shortest path from A to v as well as d[v]. • w(u,v) is the weight of arc uv • PATHTO(v) lists the vertices on the shortest path to v from A. begin for each v in V do begin d[v] = w(A,v); PATHTO(v) := A; end Mark vertex A while unmarked vertices remain do begin u:= unmarked vertex closest to A Mark u; <for-loop> end end for each unmarked vertex v with uv in E do begin d' := d[u] + w(u,v); if d' < d[v] then d[v] := v'; PATHTO(v) := PATHTO(u), v end end

  26. Discrete Math for CS Exercise: • Use Dijkstra's Algorithm with the following graph: B E 4 12 3 2 6 F 11 8 D A 5 10 C

  27. Discrete Math for CS Answer

  28. Discrete Math for CS

More Related