directed graphs. given vertices u and v of a digraph g , we say that u reaches v (and v is reachable...

35
Directed Graphs Calgary A C 112 Winnipeg T oronto O ttawa V ancouver W J35 JG 131 JG 130 A C 201 W J75 JG 120 A C 200 A flight netw ork is a directed graph,or a digraph.H ow can w e find out that w e can go from T oronto to V ancouver?

Post on 18-Dec-2015

216 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Directed Graphs. 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

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?

Page 2: Directed Graphs. 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

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

Page 3: Directed Graphs. 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

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

Page 4: Directed Graphs. 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

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

Page 5: Directed Graphs. 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

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

Page 6: Directed Graphs. 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

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

traversed according to their respective directions. Example:

Page 7: Directed Graphs. 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

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

Example:

Page 8: Directed Graphs. 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

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

Page 9: Directed Graphs. 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

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.

Page 10: Directed Graphs. 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

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.

Page 11: Directed Graphs. 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

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)

Page 12: Directed Graphs. 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

Example of DFS

Page 13: Directed Graphs. 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
Page 14: Directed Graphs. 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
Page 15: Directed Graphs. 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
Page 16: Directed Graphs. 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
Page 17: Directed Graphs. 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

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 .

Page 18: Directed Graphs. 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

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

Page 19: Directed Graphs. 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

Example of back edges

Page 20: Directed Graphs. 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

Example of forward edges

Page 21: Directed Graphs. 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

Example of cross edges

Page 22: Directed Graphs. 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

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.

Page 23: Directed Graphs. 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

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.

Page 24: Directed Graphs. 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

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.

Page 25: Directed Graphs. 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

Example

s0

Page 26: Directed Graphs. 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

s0

1

Page 27: Directed Graphs. 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

s0

12

Note: We have four cross edges (red).

Page 28: Directed Graphs. 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

s0

1

3

2

Note: We have more cross edges.

Page 29: Directed Graphs. 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

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

.

Page 30: Directed Graphs. 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

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

Page 31: Directed Graphs. 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

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

Page 32: Directed Graphs. 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

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

Page 33: Directed Graphs. 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

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

Page 34: Directed Graphs. 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

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

Page 35: Directed Graphs. 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

Data Structure Exercises 22.1