Download presentation
Presentation is loading. Please wait.
1
Directed Graphs
3
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. u v
5
A digraph G is strongly connected if for any two vertices u and v of G , u reaches v and v reaches u. Example: A strongly connected digraph
6
Adirected cycle of G is a cycle where all the edges are traversed according to their respective directions. Example:
7
A digraph G isacyclic if it has no directed cycles. Example:
8
transitive closure of a digraph G : A digraph * G such that the vertices of * G are the same as the vertices of G , and * G has an edge (u,v), whenever G has a directedpath fromu tov. Example: A digraph G and its transitive closure * G
9
There are unique problems regarding reachability in a digraph G : Given vertices u and v, determine whether u reaches v Find all the vertices of G that are reachable from a given vertex s Determine whether G is strongly connected Determine whether G is acyclic Compute the transitive closure * G of G These problems can be addressed with a traversal of the digraph.
17
A DFS on a digraph G partitions the edges of G reachable from the starting vertex intotree edges ordiscovery edges and nontree edges.
22
s u 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 G that is not visited by this DFS, then the graph is not strongly connected. Example of a not-strongly- connected digraph DFS starting froms cannot reachu.
23
s If the first DFS visits each vertex of G , then we reverse all the edges of G (using the reverseDirection method) and perform another DFS starting ats. If every vertex of G is visited by this 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 vertexs.
29
Transitive Closure Recall that the transitive closure of a digraph G is the digraph * G such that the vertices of * G are the same as the vertices of G , and * G has an edge (u,v), whenever G has a directed path fromu tov. Because the transitive closure * G only have more edges, we usually start with G to construct * G .
30
Floyed-Warshall algorithm In this algorithm, first, we label all the vertices in the graph. We call this graph 0 G . v1 v2 v3 v4 v5 v6 v7
31
Next, we start with 0 G and called it 1 G . We add the directed edge ),( ji vv to the graph if 1 G contains both the edges ),( 1 vv i and ),( 1 j vv. v1 v2 v3 v4 v5 v6 v7
32
Similarly, we start with 1 G and called it 2 G . We add the directed edge ),( ji vv to the graph if 2 G contains both the edges ),( 2 vv i and ),( 2 j vv. In this example, we have nothing to add. v1 v2 v3 v4 v5 v6 v7
33
Then we start with 2 G and called it 3 G . We add the directed edge ),( ji vv to the graph if 3 G contains both the edges ),( 3 vv i and ),( 3 j vv.
34
We continue this for the rest of the vertices ( 7654,,,vvvv ) in the example). We end up with a graph 7 G . This will be the transitive closure of G . Algorithm FloydWarshall(G): Input: A digraph G with n vertices Output: The transitive closure * G of G let n vvv,...,, 21 be an arbitrary numbering of the vertices of G assign G to 0 G loop for k from 1 to n assign 1 k G to k G loop for each i, j in 1,…, n with j i and kji , if both edges ),( ki vv and ),( jk vv are in 1 k G add edge ),( ji vv if it does not exist return n G
35
Data Structure Exercises 22.1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.