Directed Graphs. Given vertices u and v of a digraph G , we say that u reaches v (and v is reachable...

Preview:

Citation preview

Directed Graphs

C alga ry

A C 11 2

W in nip e g

T o ron to

O ttaw a

V a nco uve r

W J3 5

J G 131

J G 130

A C 20 1

W J7 5

J G 120A C 20 0

A flig h t n e tw o rk is a d ire c ted g rap h , o r a d ig rap h . H o w can w e f in d o u t th a t w e can g o fro m T o ro n to to V a n co u v e r?

Reachability digraph: A graph whose edges are all directed reachability: Describe where we can go in a directed graph traversal in a digraph: Always go along directed paths

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

GG

We say that a vertex v reaches an edge (w, z) if v reaches the origin vertex w of the edge.

(w, z)

v

z

w

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

A directed cycle of G is a cycle where all the edges are

traversed according to their respective directions. Example:

A digraph G is acyclic if it has no directed cycles.

Example:

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 directed path from u to v.

Example: A digraph G

and its transitive closure *G

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.

Traversing a Digraph Recall that with undirected graphs, we have two algorithms to traverse a graph: The depth-first search (DFS) and the breadth-first search (BFS). Both DFS and BFS algorithms can also be applied to digraphs except that we have to traverse edges according to their respective directions.

Here is a version of DFS for digraphs. Algorithm DirectedDFS(v): mark vertex v as visited loop for each outgoing edge (v, w) of v if vertex w has not been visited call DirectedDFS(w)

Example of DFS

A DFS on a digraph G partitions the edges of G

reachable

from the starting vertex into tree edges or discovery edges and nontree edges .

discovery edges: Edges that lead us to discover a new vertex nontree edges: Edges that take us to a visited vertex depth-first search tree: The tree edges form a tree rooted at the starting vertex There are three kinds of nontree edges:

back edges, which connect a vertex to an ancestor in the DFS tree

forward edges, which connect a vertex to a descendent in the DFS tree

cross edges, which connect a vertex to a vertex that is neither its ancestor nor its descendent

Example of back edges

Example of forward edges

Example of cross edges

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 from s cannot reach u.

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 at s. 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 vertex s.

Directed Breadth-First Search As with DFS, we can extend breadth-first search (BFS) for digraphs. The algorithm still visits vertices level by level and partitions the set of edges into tree edges (discovery edges), which together form a directed breadth-first search tree rooted at the start vertex, and nontree edges. However, we have only two kinds of nontree edges: back edges and cross edges. There are no forward edges.

Example

s0

s0

1

s0

12

Note: We have four cross edges (red).

s0

1

3

2

Note: We have more cross edges.

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 from u to v. Because the transitive closure *G

only have more edges, we

usually start with G

to construct *G

.

Floyed -Warshall algorithm In this algorithm, first, we label all the vertices in the graph. We call this graph 0G

.

v1

v2

v3

v4

v5

v6

v7

Next, we start with 0G

and called it 1G

. We add the directed

edge ),( ji vv to the graph if 1G

contains both the edges ),( 1vvi and ),( 1 jvv .

v1

v2

v3

v4

v5

v6

v7

Similarly, we start with 1G

and called it 2G

. We add the directed edge ),( ji vv to the graph if 2G

contains both the

edges ),( 2vvi and ),( 2 jvv . In this example, we have nothing to add.

v1

v2

v3

v4

v5

v6

v7

Then we start with 2G

and called it 3G

. We add the directed

edge ),( ji vv to the graph if 3G

contains both the edges ),( 3vvi and ),( 3 jvv .

v1

v2

v3

v4

v5

v6

v7

We continue this for the rest of the vertices ( 7654 ,,, vvvv ) in the example). We end up with a graph 7G

. 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 nvvv ,...,, 21 be an arbitrary numbering of the vertices of G

assign G

to 0G

loop for k from 1 to n assign 1kG

to kG

loop for each i, j in 1,…,n with ji and kji , if both edges ),( ki vv and ),( jk vv are in 1kG

add edge ),( ji vv if it does not exist return nG

Data Structure Exercises 22.1

Recommended