Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.

Similar presentations


Presentation on theme: "Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and."— Presentation transcript:

1 Discrete Math for CS Chapter 8: Directed Graphs

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: ab dc V = { a, b, c, d } E = { ab, cb, dc, bd, db } a b c d a 0 1 0 0 b 0 0 0 1 c 0 1 0 0 d 0 1 1 0

4 Discrete Math for CS More Notation: A path of length k in a digraph is a sequence of distinct vertices v 1, v 2, v 3,..., v k where v i-1 v i is an arc for i = 1,... k. A cycle is a path where v 1 = v k 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 B C DE F G H

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

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 m ij == 1 then there is an arc from vertex v i to vertex v j. An arc is a path of length 1. Consider M 2. 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: ab dc V = { a, b, c, d } E = { ab, cb, dc, bd, db } a b c d a 0 1 0 0 b 0 0 0 1 c 0 1 0 0 d 0 1 1 0 a b c d a 0 0 0 1 b 0 1 1 0 c 0 0 0 1 d 0 1 0 1 M M2M2

12 Discrete Math for CS Reachability Matrix Calculate M* = M or M 2 or M 3 or... or M n 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: ab dc a b c d a 0 1 0 0 b 0 0 1 1 c 0 0 0 0 d 0 0 1 0 a b c d a 0 0 1 1 b 0 0 1 0 c 0 0 0 0 d 0 0 0 0 M M2M2 a b c d a 0 0 1 0 b 0 0 0 0 c 0 0 0 0 d 0 0 0 0 M3M3 a b c d a 0 0 0 0 b 0 0 0 0 c 0 0 0 0 d 0 0 0 0 M4M4

14 Discrete Math for CS Example: ab dc a b c d a 0 1 1 1 b 0 0 1 1 c 0 0 0 0 d 0 0 1 0 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 v 1, v 2,..., v n. Warshall's Algorithm generates matrices W 0 = M, W 1, W 2,..., W n. For k >= 1, W k (i,j) = 1 iff there is a path of any length from v i to v j where the intermediary vertices in the path lie in the set {v 1,... v k }. W n = M*. Warshall's Algorithm is efficient by a clever use of for-loops. Successive passes of the outer loop calculate W 1, W 2,..., W n.

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 W k. This is done by updating entries in W k-1. To find i th row of W k 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 W k from W k-1 as follows:  Consider the k th column of W k.  For each row with a 0 entry in this column, copy the row from W k-1.  For each row with a 1 entry form the logical or of that row with row k and write the resulting row in W k.

18 Discrete Math for CS Example (Warshall's Algorithm): 1 23 4 5 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 W0W0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 W1W1 copy rows 1,2,4 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 W1W1 row 3 or row 1 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 W1W1 row 5 or row 1

19 Discrete Math for CS Example (Warshall's Algorithm): 1 23 4 5 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 W1W1 0 0 1 0 0 0 0 0 0 0 W2W2 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 W2W2

20 Discrete Math for CS Example (Warshall's Algorithm): 1 23 4 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 W2W2 0 0 0 0 0 W3W3 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 W3W3 Note: W 4 = W 3 so we are done.

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: A BC F E D 2 1 5 4 3 2 1 Find the shortest path from A to any other vertex. weight matrix: w(u,v) = 0 if u = v ∞ if uv is not an arc d if uv is an arc of weight d 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; 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

26 Discrete Math for CS Exercise: Use Dijkstra's Algorithm with the following graph: BE F D A C 10 3 4 12 118 26 5

27 Discrete Math for CS Answer

28 Discrete Math for CS


Download ppt "Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and."

Similar presentations


Ads by Google