Directed Graphs Digraphs Digraph Properties Digraph Application

Directed Graphs
4/5/2017 2:13 PM
Presentation for use with the textbook, Algorithm Design and
Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
Digraphs

Directed Graphs
BOS
ORD
A digraph is a graph
whose edges are all
directed

JFK

SFO
DFW

MIA
© 2015 Goodrich and Tamassia

Directed Graphs
1
D
Short for “directed graph”
Applications

LAX
E
C
one-way streets
flights
task scheduling
© 2015 Goodrich and Tamassia
B
A
Directed Graphs
2
E
Digraph Properties
Digraph Application
D
C

A graph G=(V,E) such that
Each edge goes in one direction:
A
Edge (a,b) goes from a to b, but not b to a





B
If G is simple, m < n(n - 1)
If we keep in-edges and out-edges in separate
adjacency lists, we can perform listing of
incoming edges and outgoing edges in time
proportional to their size
Scheduling: edge (a,b) means task a must be
completed before b can be started
cs21
cs22
cs46
cs51
cs53
cs52
cs131
cs141
cs121
cs161
The good life
cs151
© 2015 Goodrich and Tamassia
Directed Graphs
3
Directed DFS






4
E
D
C
discovery edges
back edges
forward edges
cross edges
B
A directed DFS starting at a
vertex s determines the vertices
reachable from s
© 2015 Goodrich and Tamassia
Directed Graphs
The Directed DFS Algorithm
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

© 2015 Goodrich and Tamassia
cs171
Directed Graphs
A
5
© 2015 Goodrich and Tamassia
Directed Graphs
6
1
Directed Graphs
4/5/2017 2:13 PM
Strong Connectivity
Reachability


DFS tree rooted at v: vertices reachable
from v via directed paths
E
E
F
A
d
E
D
B
e
C
F
A

Directed Graphs
7
© 2015 Goodrich and Tamassia




a
G:

d
e
G’:
g
c
9
© 2015 Goodrich and Tamassia
Directed Graphs
e
b
© 2015 Goodrich and Tamassia
D
E
B
C
G
We can perform
DFS starting at
each vertex

A
D

{f,d,e,b}
10
If there's a way to get
from A to B and from
B to C, then there's a
way to get from A to C.
O(n(n+m))
Alternatively ... Use
dynamic programming:
The Floyd-Warshall
Algorithm
E
B
C
A
{a,c,g}
Directed Graphs
Computing the
Transitive Closure
Transitive Closure

g
f
b
Directed Graphs
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 from
u to v
The transitive closure
provides reachability
information about a digraph
d
e
f

a
c
a
d
© 2015 Goodrich and Tamassia
8
Maximal subgraphs such that each vertex can reach
all other vertices in the subgraph
Can also be done in O(n+m) time using DFS, but is
more complicated (similar to biconnectivity).
b
f
If there’s a w not visited, print “no”
Else, print “yes”
Running time: O(n+m)
g
c
If there’s a w not visited, print “no”
Let G’ be G with edges reversed
Perform a DFS from v in G’

Directed Graphs
Strongly Connected
Components
Pick a vertex v in G
Perform a DFS from v in G


b
f
B
Strong Connectivity
Algorithm

g
c
C
C
© 2015 Goodrich and Tamassia
a
D
D
A
Each vertex can reach all other vertices
G*
11
© 2015 Goodrich and Tamassia
Directed Graphs
12
2
Directed Graphs
4/5/2017 2:13 PM
Floyd-Warshall
Transitive Closure
Floyd-Warshall’s Algorithm:
High-Level View
Idea #1: Number the vertices 1, 2, …, n.
Idea #2: Consider paths that use only
vertices numbered 1, 2, …, k, as
intermediate vertices:




Number vertices v1 , …, vn
Compute digraphs G0, …, Gn


Uses only vertices numbered 1,…,k
(add this edge if it’s not already in)
i

j
Uses only vertices
numbered 1,…,k-1
k
© 2015 Goodrich and Tamassia

We have that Gn = G*
In phase k, digraph Gk is computed from Gk 1
Uses only vertices
numbered 1,…,k-1
Directed Graphs
G0=G
Gk has directed edge (vi, vj) if G has a directed
path from vi to vj with intermediate vertices in
{v1 , …, vk}
Running time: O(n3), assuming areAdjacent is
O(1) (e.g., adjacency
matrix)
© 2015 Goodrich and Tamassia
Directed Graphs
14

13
The Floyd-Warshall Algorithm
Floyd-Warshall Example
v7
BOS
ORD
v4
This
image
cannot
currently
be dis
JFK
v2
This
image
cannot
currently
be displ
v6
This
image
cannot
currently
be dis…
SFO
LAX
v1
This
image
cannot
currently
be displ

DFW
v3
This
image
cannot
currently
be displ…
MIA
The running time is clearly O(n3).
© 2015 Goodrich and Tamassia
v5
This
image
cannot
currently
be displ…
Directed Graphs
15
Floyd-Warshall, Iteration 1
v7
© 2015 Goodrich and Tamassia
Directed Graphs
16
Floyd-Warshall, Iteration 2
BOS
ORD
v4
This
image
cannot
currently
be dis
ORD
JFK
v2
This
image
cannot
currently
be displ
SFO
v1
JFK
v6
This
image
cannot
currently
be dis…
SFO
DFW
LAX
v3
This
image
cannot
currently
be displ…
v1
This
image
cannot
currently
be displ
MIA
DFW
v3
This
image
cannot
currently
be displ…
MIA
v5
v5
This
image
cannot
currently
be displ…
© 2015 Goodrich and Tamassia
This
image
cannot
currently
be dis
This
image
cannot
currently
be displ
This
image
cannot
currently
be dis…
LAX
v4
v2
v6
This
image
cannot
currently
be displ
v7
BOS
Directed Graphs
This
image
cannot
currently
be displ…
17
© 2015 Goodrich and Tamassia
Directed Graphs
18
3
Directed Graphs
4/5/2017 2:13 PM
Floyd-Warshall, Iteration 3
v7
Floyd-Warshall, Iteration 4
BOS
ORD
v4
This
image
cannot
currently
be dis
JFK
This
image
cannot
currently
be displ
SFO
v1
JFK
This
image
cannot
currently
be displ
This
image
cannot
currently
be dis…
LAX
This
image
cannot
currently
be dis
v2
v6
This
image
cannot
currently
be displ
v4
ORD
v2
v6
This
image
cannot
currently
be dis…
SFO
DFW
LAX
v3
This
image
cannot
currently
be displ…
v1
This
image
cannot
currently
be displ
MIA
DFW
v3
This
image
cannot
currently
be displ…
MIA
v5
v5
This
image
cannot
currently
be displ…
© 2015 Goodrich and Tamassia
This
image
cannot
currently
be displ…
Directed Graphs
19
Floyd-Warshall, Iteration 5
v7
© 2015 Goodrich and Tamassia
Directed Graphs
20
Floyd-Warshall, Iteration 6
BOS
ORD
This
image
cannot
currently
be dis
JFK
JFK
This
image
cannot
currently
be displ
This
image
cannot
currently
be dis…
SFO
v1
This
image
cannot
currently
be dis
v2
v6
LAX
v4
ORD
This
image
cannot
currently
be displ
v6
This
image
cannot
currently
be dis…
SFO
DFW
LAX
v3
This
image
cannot
currently
be displ…
v1
This
image
cannot
currently
be displ
MIA
DFW
v3
This
image
cannot
currently
be displ…
MIA
v5
v5
This
image
cannot
currently
be displ…
© 2015 Goodrich and Tamassia
This
image
cannot
currently
be displ…
Directed Graphs
21
Floyd-Warshall, Conclusion
v7
BOS
© 2015 Goodrich and Tamassia
Directed Graphs
22
DAGs and Topological Ordering
A directed acyclic graph (DAG) is a
digraph that has no directed cycles

A topological ordering of a digraph
is a numbering
v1 , …, vn
of the vertices such that for every
edge (vi , vj), we have i < j

Example: in a task scheduling
digraph, a topological ordering a
task sequence that satisfies the
v2
precedence constraints
Theorem
A digraph admits a topological
v1
ordering if and only if it is a DAG
D

ORD
v4
This
image
cannot
currently
be dis
JFK
v2
This
image
cannot
currently
be displ
v6
This
image
cannot
currently
be dis…
SFO
LAX
v1
This
image
cannot
currently
be displ
DFW
v3
This
image
cannot
currently
be displ…
MIA
v5
This
image
cannot
currently
be displ…
© 2015 Goodrich and Tamassia
v7
BOS
v4
v2
This
image
cannot
currently
be displ
v7
BOS
Directed Graphs
23
© 2015 Goodrich and Tamassia
Directed Graphs
E
B
C
DAG G
A
D
B
C
A
v4
E
v5
v3
Topological
ordering of G
24
4
Directed Graphs
4/5/2017 2:13 PM
Topological Sorting

Algorithm for Topological Sorting
Number vertices, so that (u,v) in E implies u < v
wake up
1
2
study computer sci.
7
play

A typical student day
nap
3
eat
Algorithm TopologicalSort(G)
HG
// Temporary copy of G
n  G.numVertices()
while H is not empty do
Let v be a vertex with no outgoing edges
Label v  n
nn-1
Remove v from H
5
4
more c.s.
8
write c.s. program
6
work out
9
bake cookies
10
sleep
11

dream about graphs
© 2015 Goodrich and Tamassia
Directed Graphs
25
Implementation with DFS


Simulate the algorithm by
using depth-first search
O(n+m) time.
Algorithm topologicalDFS(G)
Input dag G
Output topological ordering of G
n  G.numVertices()
for all u  G.vertices()
setLabel(u, UNEXPLORED)
for all v  G.vertices()
if getLabel(v) = UNEXPLORED
topologicalDFS(G, v)
© 2015 Goodrich and Tamassia
Note: This algorithm is different than the
one in the book
Running time: O(n + m)
© 2015 Goodrich and Tamassia
Directed Graphs
26
Topological Sorting Example
Algorithm topologicalDFS(G, v)
Input graph G and a start vertex v of G
Output labeling of the vertices of G
in the connected component of v
setLabel(v, VISITED)
for all e  G.outEdges(v)
{ outgoing edges }
w  opposite(v,e)
if getLabel(w) = UNEXPLORED
{ e is a discovery edge }
topologicalDFS(G, w)
else
{ e is a forward or cross edge }
Label v with topological number n
nn-1
Directed Graphs
27
Topological Sorting Example
© 2015 Goodrich and Tamassia
Directed Graphs
28
Topological Sorting Example
8
9
© 2015 Goodrich and Tamassia
Directed Graphs
9
29
© 2015 Goodrich and Tamassia
Directed Graphs
30
5
Directed Graphs
4/5/2017 2:13 PM
Topological Sorting Example
Topological Sorting Example
6
7
7
8
8
9
© 2015 Goodrich and Tamassia
9
Directed Graphs
31
Topological Sorting Example
© 2015 Goodrich and Tamassia
Directed Graphs
32
Topological Sorting Example
4
6
6
5
7
5
7
8
8
9
© 2015 Goodrich and Tamassia
9
Directed Graphs
33
Topological Sorting Example
© 2015 Goodrich and Tamassia
Directed Graphs
34
Topological Sorting Example
2
3
3
4
6
4
6
5
7
7
8
8
9
© 2015 Goodrich and Tamassia
5
Directed Graphs
9
35
© 2015 Goodrich and Tamassia
Directed Graphs
36
6
Directed Graphs
4/5/2017 2:13 PM
Topological Sorting Example
2
1
3
4
6
5
7
8
9
© 2015 Goodrich and Tamassia
Directed Graphs
37
7