Directed Graphs
3/29/14 21:36
Presentation for use with the textbook Data Structures and
Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
and M. H. Goldwasser, Wiley, 2014
Directed Graphs
BOS
ORD
JFK
SFO
LAX
DFW
MIA
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
1
Digraphs
q
A digraph is a graph
whose edges are all
directed
n
q
n
n
D
Short for “directed graph”
Applications
n
E
C
one-way streets
flights
task scheduling
© 2014 Goodrich, Tamassia, Goldwasser
B
A
Directed Graphs
2
1
Directed Graphs
3/29/14 21:36
E
Digraph Properties
D
C
q
n
n
q
q
B
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
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
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
3
Digraph Application
q
Scheduling: edge (a,b) means task a must be
completed before b can be started
cs21
cs22
cs46
cs51
cs53
cs52
cs161
cs131
cs141
cs121
The good life
cs151
© 2014 Goodrich, Tamassia, Goldwasser
cs171
Directed Graphs
4
2
Directed Graphs
3/29/14 21:36
Directed DFS
q
q
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
n
n
n
n
q
E
D
discovery edges
back edges
forward edges
cross edges
C
B
A directed DFS starting at a
vertex s determines the vertices
reachable from s
© 2014 Goodrich, Tamassia, Goldwasser
A
Directed Graphs
5
Reachability
q
DFS tree rooted at v: vertices reachable
from v via directed paths
E
E
D
C
A
C
F
A
E
B
D
C
A
© 2014 Goodrich, Tamassia, Goldwasser
D
Directed Graphs
F
B
6
3
Directed Graphs
3/29/14 21:36
Strong Connectivity
q
Each vertex can reach all other vertices
a
g
c
d
e
b
f
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
7
Strong Connectivity
Algorithm
q
q
Pick a vertex v in G
Perform a DFS from v in G
n
q
q
n
q
d
e
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’
n
a
G:
a
G’:
g
c
d
e
b
f
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
8
4
Directed Graphs
3/29/14 21:36
Strongly Connected
Components
q
q
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).
a
c
d
{ a , c , g }
b
{ f , d , e , b }
e
f
© 2014 Goodrich, Tamassia, Goldwasser
g
Directed Graphs
9
Transitive Closure
q
q
Given a digraph G, the
transitive closure of G is the
digraph G* such that
n G* has the same vertices
as G
n 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
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
D
E
B
C
G
A
D
E
B
C
A
G*
10
5
Directed Graphs
3/29/14 21:36
Computing the
Transitive Closure
q
We can perform
DFS starting at
each vertex
n
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
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
11
Floyd-Warshall
Transitive Closure
q
q
Idea #1: Number the vertices 1, 2, …, n.
Idea #2: Consider paths that use only
vertices numbered 1, 2, …, k, as
intermediate vertices:
i
Uses only vertices numbered 1,…,k
(add this edge if it’s not already in)
j
Uses only vertices
numbered 1,…,k-1
k
© 2014 Goodrich, Tamassia, Goldwasser
Uses only vertices
numbered 1,…,k-1
Directed Graphs
12
6
Directed Graphs
3/29/14 21:36
Floyd-Warshall’s Algorithm
q
q
q
q
q
Algorithm FloydWarshall(G)
Input digraph G
Output transitive closure G* of G
n G0=G
i←1
n Gk has directed edge (vi, vj)
for all v ∈ G.vertices()
denote v as vi
if G has a directed path
i←i+1
from vi to vj with
G0 ← G
intermediate vertices in
for k ← 1 to n do
{v1 , …, vk}
Gk ← Gk - 1
We have that Gn = G*
for i ← 1 to n (i ≠ k) do
In phase k, digraph Gk is
for j ← 1 to n (j ≠ i, k) do
computed from Gk - 1
if Gk - 1.areAdjacent(vi, vk) ∧
Gk - 1.areAdjacent(vk, vj)
Running time: O(n3),
if ¬Gk.areAdjacent(vi, vj)
assuming areAdjacent is O(1)
Gk.insertDirectedEdge(vi, vj , k)
(e.g., adjacency matrix)
return Gn
Number vertices v1 , …, vn
Compute digraphs G0, …, Gn
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
13
Java Implementation
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
14
7
Directed Graphs
3/29/14 21:36
Floyd-Warshall Example
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
15
Floyd-Warshall, Iteration 1
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
16
8
Directed Graphs
3/29/14 21:36
Floyd-Warshall, Iteration 2
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
17
Floyd-Warshall, Iteration 3
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
18
9
Directed Graphs
3/29/14 21:36
Floyd-Warshall, Iteration 4
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
19
Floyd-Warshall, Iteration 5
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
20
10
Directed Graphs
3/29/14 21:36
Floyd-Warshall, Iteration 6
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
21
Floyd-Warshall, Conclusion
v7
BOS
ORD
v
4
JFK
v
2
v
6
SFO
LAX
v
1
DFW
v
3
MIA
v
5
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
22
11
Directed Graphs
3/29/14 21:36
DAGs and Topological Ordering
A directed acyclic graph (DAG) is a
digraph that has no directed cycles
q 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
q Example: in a task scheduling
digraph, a topological ordering a
task sequence that satisfies the
precedence constraints
Theorem
A digraph admits a topological
ordering if and only if it is a DAG
D
q
© 2014 Goodrich, Tamassia, Goldwasser
E
B
C
A
v2
v1
DAG G
D
B
C
A
v4
E
v5
v3
Topological
ordering of G
Directed Graphs
23
Topological Sorting
q
Number vertices, so that (u,v) in E implies u < v
wake up
1
A typical student day
2
study computer sci.
7
play
nap
3
eat
4
5
more c.s.
8
write c.s. program
6
work out
9
bake cookies
10
sleep
11
dream about graphs
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
24
12
Directed Graphs
3/29/14 21:36
Algorithm for Topological Sorting
q
Note: This algorithm is different than the
one in the book
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
q
Running time: O(n + m)
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
25
Implementation with DFS
q
q
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)
© 2014 Goodrich, Tamassia, Goldwasser
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
26
13
Directed Graphs
3/29/14 21:36
Topological Sorting Example
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
27
Topological Sorting Example
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
28
14
Directed Graphs
3/29/14 21:36
Topological Sorting Example
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
29
Topological Sorting Example
7
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
30
15
Directed Graphs
3/29/14 21:36
Topological Sorting Example
6
7
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
31
Topological Sorting Example
6
5
7
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
32
16
Directed Graphs
3/29/14 21:36
Topological Sorting Example
4
6
5
7
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
33
Topological Sorting Example
3
4
6
5
7
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
34
17
Directed Graphs
3/29/14 21:36
Topological Sorting Example
2
3
4
6
5
7
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
35
Topological Sorting Example
2
1
3
4
6
5
7
8
9
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
36
18
Directed Graphs
3/29/14 21:36
Java Implementation
© 2014 Goodrich, Tamassia, Goldwasser
Directed Graphs
37
19
© Copyright 2026 Paperzz