350 likes | 377 Views
Directed Graphs. r. G. r. G. u. v. Given vertices. and. of a digraph. , we say that. reaches. u. v. u. v. (and. is reachable from. ) if. has a directed path from. to. v. u. u. v. r. G. r. G. strongly connected. A digraph. is. if for any two vertices. u.
E N D
r G r G u v Given vertices and of a digraph , we say that reaches u v u v (and is reachable from ) if has a directed path from to . v u u v
r G r G strongly connected A digraph is if for any two vertices u and of , reaches and reaches . v u v v u Example: A strongly connected digraph
r G A directed cycle of is a cycle where all the edges are traversed according to their respective directions. Example:
r G A digraph is acyclic if it has no directed cycles. Example:
r r * G G r r r * * G G G r G r r * G G transitive closure of a digraph : A digraph such that the vertices of are the same as the vertices of , and has an edge ( u , v ), whenever has a directed path from u to v . Example: A digraph and its transitive closure
r G r G r G r G r r * G G There are unique problems regarding reachability in a digraph : Given vertices u and v, determine whether u reaches v · Find all the vertices of that are reachable from a given · vertex s Determine whether is strongly connected · Determine whether is acyclic · Compute the transitive closure of · These problems can be addressed with a traversal of the digraph.
r r G G A DFS on a digraph partitions the edges of reachable from the starting vertex into tree edges or discovery edges and nontree edges .
r G u s Testing for Strong Connectivity We use DFS two times to determine if the digraph is strongly connected. For the first DFS, we start from an arbitrary vertex s . If there is any vertex of that is not visited by this DFS, then the gr aph is not strongly connected. Example of a not - strongly - connected digraph DFS starting from s cannot reach u .
r G r G r G s If the first DFS visits each vertex of , then we reverse all the edges of (using the reverseDirection method) and perform another DFS starting at s . If every vertex of is visited by thi s second DFS, then the graph is strongly connected. Example The first DFS reaches every vertex while the second DFS does not. What is shown is the second DFS starting from the vertex s .
r G r r * * G G r r r G * G G r * G r r * G G Transitive Closure Recall that the transitive closure of a digraph is the digraph such that the vertices of are the same as the vertices of , and has an edge ( u , v ), whenever has a directed path from u to v . Because the transitive closure only have more edges, we usually start with to construct .
r G 0 v4 v7 v2 v6 v3 v5 v1 Floyed - Warshall algorithm In this algorithm, first, we label all the vertices in the graph. We call this graph .
r r G G 0 1 r ( v , v ) ( v , v ) G i j i 1 1 ( v , v ) 1 j v4 v7 v2 v6 v3 v5 v1 Next, we start with and called it . We add the directed edge to the graph if contains both the edges and .
r r G G 1 2 r ( v , v ) G i j 2 ( v , v ) ( v , v ) 2 j i 2 v4 v7 v2 v6 v3 v5 v1 Similarly, we start with and called it . We add the directed edge to the graph if contains both the edges and . In this example, we have nothing to add.
r r G G 3 2 r ( v , v ) ( v , v ) G i j i 3 3 ( v , v ) 3 j v4 v7 v2 v6 v3 v5 v1 Then we start with and called it . We add the directed edge to the graph if contains both the edges and .
v , v , v , v 4 5 6 7 r G 7 r G r G r r G * G r v , v ,..., v G 1 2 n r r G G 0 r r G G - k 1 k ¹ i , j k r ( v , v ) ( v , v ) G - i k k j k 1 ( v , v ) i j r G n We continue this for the rest of the vertices ( ) in the example). We end up with a graph . This will be the transitive closure of . Algorithm FloydWarshall(G): Input: A digraph with n vertices of Output: The transitive closure let be an arbitrary numbering of the vertices of assign to loop for from 1 to k n assign to loop for each , in 1,…, with and i j n j ¹ i if both edges and are in add edge if it does not exist return