8 Graphs Traversals 8B 1 Directed Graphs BOS
8. Graphs & Traversals 8(B 1) - Directed Graphs BOS ORD Chapter 13 -Section 13. 4 JFK SFO LAX DFW MIA Prof. Abdullah Zawawi Talib, Ph. D. School of Computer Sciences Universiti Sains Malaysia Directed Graphs 1
Outline and Reading (§ 13. 4) Reachability (§ 13. 4. 1) n n Directed DFS Strong connectivity Directed Acyclic Graphs (DAG’s) (§ 13. 4. 4) n Topological Sorting Directed Graphs 2
Reachability A fundamental issue with directed graphs is the notion of reachability, which deals with determining where we can get to in a directed graph. Given vertices u and v of a digraph G, we say that u reaches v (and v is reachable from u) if G has a directed path from u to v. Reachable from C Directed Graphs Reachable from B 3
Strong Connectivity A digraph G is strongly connected if, for any two vertices u and v of G, u reaches v and v reaches u. Each vertex can reach all other vertices a g c d e b f Directed Graphs 4
Strong Connectivity A directed cycle of G is a cycle where all the edges are traversed according to their respective directions A digraph G is acyclic if it has no directed cycles. A directed cycle (ORD, MIA, DFW, LAX, ORD) [thick lines] Removing the dashed edges gives an acyclic digraph. Directed Graphs 5
Directed DFS By traversing edges only along their direction A directed DFS partitions the edges of G reachable from the starting vertex into: n n discovery edges or tree edges, discover a new vertex, nontree edges, reach a previously visited vertex. The discovery edges form a tree rooted at the starting vertex, called the directed DFS tree. Also, three kinds of nontree edges (see next figure(b)): n n n back edges, connect a vertex to an ancestor in the DFS tree forward edges, connect a vertex to a descendant cross edges, connect a vertex to a vertex that is neither its ancestor nor its descendant. Directed Graphs 6
Directed DFS A directed DFS starting at a vertex s determines the vertices reachable from s (a) Intermediate step, for the first time, an already visited vertex (DFW) is reached; (b) The completed DFS; Discovery edges = thick solid lines; Back edges = thick dashed lines (to the ancestor) e. g. (ORD, DFW); Forward edges = thin dashed lines (to the descendant) e. g. (DFW, ORD); If there were an edge (SFO, LAX), it would be a cross edge Directed Graphs 7
Directed DFS Algorithm Directed. DFS(G, v): Label v as active // Every vertex is initially unexplored for each outgoing edge, e, that is incident to v in G do if e is unexplored then Let w be the destination vertex for e if w is unexplored and not active then Label e as a discovery edge Directed. DFS(G, w) else if w is active then Label e as a back edge else Label e as a forward/cross edge Label v as explored Directed Graphs 8
Strong Connectivity Algorithm Perform DFS starting at an arbitrary vertex s. G: If there is any vertex of G that is not visited by this DFS, then G is not strongly connected. So, if this first DFS visits each vertex of G, then we reverse all the edges of G and perform another DFS starting at s in this “reverse” graph. G’: If every vertex of G is visited then G is strongly connected. Running time: O(n+m) [two DFS] Directed Graphs a g c d e b f 9
Strongly Connected Components Maximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity). a g c d e {a, c, g} {f, d, e, b} b f Directed Graphs 10
Digraph Application Scheduling: edge (a, b) means task a must be completed before b can be started ics 21 ics 22 ics 23 ics 51 ics 53 ics 52 ics 161 ics 131 ics 141 ics 151 Directed Graphs ics 121 ics 171 The good life 11
DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v 1 , …, vn of the vertices such that for every edge (vi , vj), we have i < j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the v 2 precedence constraints Theorem A digraph admits a topological ordering if and only if it is a DAG v 1 Directed Graphs D E B C DAG G A D B C A v 4 E v 5 v 3 Topological ordering of G 12
Topological Sorting Example (Using DFS) Directed Graphs 13
Topological Sorting Example 9 Directed Graphs 14
Topological Sorting Example 8 9 Directed Graphs 15
Topological Sorting Example 7 8 9 Directed Graphs 16
Topological Sorting Example 6 7 8 9 Directed Graphs 17
Topological Sorting Example 6 5 7 8 9 Directed Graphs 18
Topological Sorting Example 4 6 5 7 8 9 Directed Graphs 19
Topological Sorting Example 3 4 6 5 7 8 9 Directed Graphs 20
Topological Sorting Example 2 3 4 6 5 7 8 9 Directed Graphs 21
Topological Sorting Example 2 1 3 4 6 5 7 8 9 Directed Graphs 22
- Slides: 22