Lecture 16: Directed Graphs

Lecture 16: Directed Graphs
BOS
ORD
JFK
SFO
DFW
LAX
MIA
Courtesy of Goodrich, Tamassia and Olga Veksler
Instructor: Yuzhen Xie
O tline
Outline
ƒ Directed Graphs
ƒ Properties
ƒ Algorithms for Directed Graphs
ƒ DFS and BFS
ƒ Strong Connectivity
ƒ Transitive Closure
ƒ DAG and Topological
p g
Ordering
g
2
Digraphs
ƒ A digraph is a
graph
g
p whose
edges are all
directed
E
D
ƒ Short for “directed
graph”
C
B
ƒ Applications
l
ƒ one-way streets
ƒ flights
ƒ task scheduling
A
3
Digraph Properties
‰
Each edge goes in one direction:
Š
Š
‰
D
A graph G=(V,E) such that
„
‰
E
Edge
d (A,B)
( ) goes ffrom A to B,
Edge (B,A) goes from B to A,
C
B
If G is simple,
p m < n*(n-1).
( )
A
If we keep in-edges and out-edges in separate
adjacency lists, we can perform listing of in-edges and
out edges in time proportional to their size
out-edges
„
„
OutgoingEdges(A): (A,C), (A,D), (A,B)
IngoingEdges(A): (E,A),(B,A)
ƒ We say that vertex w is reachable from vertex v if
there is a directed path from v to w
ƒ E is reachable
ea hable from
f om A
ƒ E is not reachable from D
4
Directed DFS
‰
We can specialize the
traversal algorithms (DFS and
BFS) to digraphs by
traversing edges only along
their direction
ƒ In the directed DFS
algorithm, we have
four types of edges
„ discovery edges
„ back
b k edges
d
„ forward edges
„ cross edges
ƒ A directed DFS starting at a
vertex s visits all the vertices
reachable from s
„
Unlike undirected graph,
graph if u is
reachable from s, it does not
mean that s is reachable from u
Algorithm DFS(G, v)
Input digraph G and a start vertex v of G
Output traverses vertices in G reachable
from v
setLabel(v, VISITED)
for all e ∈ G.outgoingEdges(v)
w ← opposite(v,e)
if getLabel(w) = UNEXPLORED
DFS(G, w)
E
3
D
4
C 2
B 5
A 1
Reachability
Graph G
E
D
C
A
‰
DFS tree rooted at v:
vertices reachable from v
via
i directed
di t d paths
th
E
D
„
B
BFS tree rooted at v: vertices
reachable from v via directed
paths
th
E
D
C
C
A
F
DFS(G C)
DFS(G,C)
A
F
B
BFS (G,B)
Strong Connectivity
‰
A graph is strongly connected if we can reach all other
vertices from any fixed vertex
g
a
g
a
c
c
d
f
d
e
b
strongly connected graph
f
e
b
t
l connected
t d graph
h
nott strongly
cannot reach a from g
Inefficient Algorithm to Check for Strong
Connectivity
stronglyConnected
gy
= true
for every vertex v in G
DFS(G,v)
if some vertex w is not visited
stronglyConnected = false
G:
a
d
f
ƒ DFS(G,v) visits every vertex reachable from v
ƒ Simple,
l yet very inefficient,
ff
O[(n+m)n]
[(
) ]
8
g
c
e
b
Efficient Algorithm to Check for Strong
Connectivity
1) Pick a vertex v in G
2) Perform a DFS from v in G
ƒ
ƒ
v
g
c
If there’s a vertex w not visited, print “no” and
terminate the algorithm
else (all vertices are visited) from v we can
w
reach any other graph vertex
d
b
3) Let G’ be G with edges reversed
4) Perform a DFS from v in G’.
If th
there iis path
th from
f
v to
t w in
i G’,
G’ then
th there
th
v
is path from w to v in the original graph G
ƒ If every vertex is reachable from v in G’, then
there is a path from any vertex w to v in the
original graph G
ƒ Then G is strongly connected
path between anyy nodes b and d: w
ƒ To find p
first go from b to v, then go from v to d
Running time is O(n + m), perform DFS 2 times
e
G
ƒ
ƒ
9
g
c
d
e
b
G’
Transitive Closure
ƒ Given a digraph G, the
transitive closure of G is
the digraph G* such that
ƒ G* has the same vertices
as G
ƒ if G has a directed path
from u to v (u ≠ v), G*
has a directed edge
g from
u to v
D
E
B
C
G
A
D
ƒ The transitive closure
summarizes
i
reachability
h bili
information about a digraph
E
B
C
A
10
G*
Computing Transitive Closure
‰
We can perform DFS(G,v) for each vertex v
for all vertices v in graph G
DFS(G,v)
for all vertices w in graph G
if w is reachable from v
put edge (v,w) in G*
‰
Running time is O(n(n+m))
„
This is O(n2) for sparse graphs
Š
„
A graph
h is
i sparse if it has
h O(n)
O( ) edges
d
This is O(n3) for dense graphs
Š
A graph is dense if it has O(n2) edges
11
DAGs and Topological Ordering
‰
A directed acyclic graph
(DAG) is a digraph that has no
di ected ccycles
directed
cles
D
B
ƒ A topological ordering of a
digraph is a numbering of
vertices:
1 , 2, …, n
g ((v,, w),
),
such that for everyy edge
number(v) < number(w)
ƒ Example: in a task scheduling
digraph,
di
h a topological
t
l i l ordering
d i
of a task sequence satisfies
the precedence constraints
E
DAG G
C
A
4
D
2
B
C
1
A
5
E
3
Topological
T
l i l
ordering of G
Topological Ordering
‰
‰
Scheduling problem: edge (a,b) means task a must be completed before
b can be started
Number vertices
vertices, so that u.number
u number < v.number
v number for any edge (u
(u,v)
v)
wake up
1
A typical student day
3
2
study computer sci.
eat
5
more c.s.
4
nap
7
play
8
write c.s. pprogram
g
9
make cookies
for professors
6
work out
10
11
sleep
13
dream about graphs
Topological Ordering Theorem
Theorem: Digraph admits topological ordering if and only if it is a DAG
Proof:
Suppose graph is not a DAG,
DAG that is it has a cycle v1, v2,…, vn-1,
vn, v1. If topological ordering existed, it would imply that v1.num
< v2.num < …< vn-1.num < vn.num < v1.num
¾
If graph is a DAG,
DAG there is a vertex v with no outgoing edges
¾
¾
This vertex can be the last one in topological ordering
Removing this vertex from the graph with incoming and
outgoing edges leaves the graph acyclic
counter = n
Repeat until counter = 0
1. Find vertex v with no outgoing edges
2. Set topological label of v to counter
3. Remove v from the graph together
with
i h allll incoming
i
i and
d outgoing
i edges.
d
4. counter = counter -1
ƒ
if every vertex has an outgoing edge, then performing DFS we
will encounter a back edge which means cycle
Topological Sorting Algorithm Using DFS
‰
‰
Simulate the algorithm by using depth-first search
counter is an instance (global) variable
Algorithm topologicalDFS(G)
Input dag G
Output topological ordering of G
counter ← G.numVertices()
for all u ∈ G.vertices()
setLabel(u, UNEXPLORED)
for all v ∈ G.vertices()
if getLabel(v) = UNEXPLORED
topologicalDFS(G, v)
‰
O(n + m) time.
Algorithm topologicalDFS(G, v)
Input graph G and a start vertex v
Output labeling of the vertices of G
in the connected component of v
setLabel(v, VISITED)
for all e ∈ G.outgoingEdges(v)
w ← opposite(v,e)
if getLabel(w) = UNEXPLORED
topologicalDFS(G, w)
v.topologicalNumber = counter
counter ← counter - 1
15
Topological
p g
Sorting
g Example
p
16
Topological
p g
Sorting
g Example
p
9
17
Topological Sorting Example
8
9
18
Topological Sorting Example
7
8
9
19
Topological
p g
Sorting
g Example
p
6
7
8
9
20
Topological
p g
Sorting
g Example
p
6
5
7
8
9
21
Topological Sorting Example
4
6
5
7
8
9
22
Topological
p g
Sorting
g Example
p
3
4
6
5
7
8
9
23
Topological
p g
Sorting
g Example
p
2
3
4
6
5
7
8
9
24
Topological Sorting Example
2
1
3
4
6
5
7
8
9
25