Directed Graphs Digraphs Digraph Properties Digraph Application

Digraphs

Directed Graphs
BOS
ORD
A digraph is a graph
whose edges are all
directed

JFK

SFO
DFW

MIA
© 2010 Goodrich, Tamassia

Directed Graphs
1
D
Short for “directed graph”
Applications

LAX
E
C
one-way streets
flights
task scheduling
B
A
Directed Graphs
© 2010 Goodrich, Tamassia
2
E
Digraph Properties
Digraph Application
D
C



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
Scheduling: edge (a,b) means task a must be
completed before b can be started
ics21
ics22
ics23
ics51
ics53
ics52
ics161
ics131
ics141
ics121
The good life
ics151
© 2010 Goodrich, Tamassia
Directed Graphs
3
© 2010 Goodrich, Tamassia
ics171
Directed Graphs
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





Reachability

E
DFS tree rooted at v: vertices reachable
from v via directed paths
E
D
E
C
discovery edges
back edges
forward edges
cross edges
B
A directed DFS starting at a
vertex s determines the vertices
reachable from s
D
C
C
A
D
A
F
A
E
D
B
C
A
Directed Graphs
© 2010 Goodrich, Tamassia
5
B
Directed Graphs
6
Strong Connectivity
Algorithm
Strong Connectivity

© 2010 Goodrich, Tamassia
F
Each vertex can reach all other vertices


a
Pick a vertex v in G
Perform a DFS from v in G


g
c


d

e
d
b
a
G’:
f
© 2010 Goodrich, Tamassia
Directed Graphs
g
c
d
b
e
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’

a
G:
e
b
f
7
© 2010 Goodrich, Tamassia
Directed Graphs
8
Strongly Connected
Components


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
g
c
d
© 2010 Goodrich, Tamassia

{a,c,g}

b
{f,d,e,b}
Directed Graphs
Computing the
Transitive Closure
We can perform
DFS starting at
each vertex

e
f

Transitive Closure
9
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.


Alternatively ... Use
dynamic programming:
The Floyd-Warshall
Algorithm
Directed Graphs
© 2010 Goodrich, Tamassia
E
B
C
G
A
D
E
B
C
A
G*
Directed Graphs
10
Floyd-Warshall
Transitive Closure
O(n(n+m))
© 2010 Goodrich, Tamassia
D
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
11
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
© 2010 Goodrich, Tamassia
Uses only vertices
numbered 1,…,k-1
Directed Graphs
12
Floyd-Warshall’s Algorithm





Floyd-Warshall Example
Algorithm FloydWarshall(G)
Number vertices v1 , …, vn
Input digraph G
Compute digraphs G0, …, Gn
Output transitive closure G* of G
 G0=G
i1
for all v  G.vertices()
 Gk has directed edge (vi, vj)
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
Directed Graphs
© 2010 Goodrich, Tamassia
13
Floyd-Warshall, Iteration 1
v7
v7
BOS
ORD
v4
JFK
v2
v6
SFO
DFW
LAX
v1
v3
MIA
v5
Directed Graphs
© 2010 Goodrich, Tamassia
14
Floyd-Warshall, Iteration 2
BOS
ORD
v4
ORD
JFK
v2
SFO
JFK
v6
SFO
DFW
v1
DFW
LAX
v3
v1
v3
MIA
MIA
v5
© 2010 Goodrich, Tamassia
v4
v2
v6
LAX
v7
BOS
Directed Graphs
v5
15
© 2010 Goodrich, Tamassia
Directed Graphs
16
Floyd-Warshall, Iteration 3
v7
Floyd-Warshall, Iteration 4
BOS
ORD
BOS
v4
ORD
JFK
v2
SFO
JFK
v6
SFO
DFW
v1
v4
v2
v6
LAX
DFW
LAX
v3
v1
v3
MIA
MIA
v5
v5
Directed Graphs
© 2010 Goodrich, Tamassia
17
Floyd-Warshall, Iteration 5
v7
Directed Graphs
© 2010 Goodrich, Tamassia
18
Floyd-Warshall, Iteration 6
BOS
ORD
ORD
JFK
SFO
JFK
v6
SFO
DFW
DFW
LAX
v3
v1
v3
MIA
MIA
v5
© 2010 Goodrich, Tamassia
v4
v2
v6
v1
v7
BOS
v4
v2
LAX
v7
Directed Graphs
v5
19
© 2010 Goodrich, Tamassia
Directed Graphs
20
Floyd-Warshall, Conclusion
v7
DAGs and Topological Ordering
BOS
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

v4
ORD
JFK
v2
v6
SFO
DFW
LAX
v3
v1
MIA
v5
Directed Graphs
© 2010 Goodrich, Tamassia
21
Topological Sorting

wake up
7
play

A typical student day
2
study computer sci.
nap
3
eat
more c.s.
8
write c.s. program
6
work out
9
bake cookies
10
sleep
11

dream about graphs
© 2010 Goodrich, Tamassia
Directed Graphs
C
DAG G
A
D
v4
B
C
A
E
v5
v3
Topological
ordering of G
Directed Graphs
22
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
5
4
B
Algorithm for Topological Sorting
Number vertices, so that (u,v) in E implies u < v
1
© 2010 Goodrich, Tamassia
E
23
Running time: O(n + m)
© 2010 Goodrich, Tamassia
Directed Graphs
24
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)
© 2010 Goodrich, Tamassia
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
25
Topological Sorting Example
Directed Graphs
© 2010 Goodrich, Tamassia
26
Topological Sorting Example
8
9
© 2010 Goodrich, Tamassia
Directed Graphs
9
27
© 2010 Goodrich, Tamassia
Directed Graphs
28
Topological Sorting Example
Topological Sorting Example
6
7
7
8
8
9
9
Directed Graphs
© 2010 Goodrich, Tamassia
29
Topological Sorting Example
Directed Graphs
© 2010 Goodrich, Tamassia
30
Topological Sorting Example
4
6
6
5
7
7
8
8
9
© 2010 Goodrich, Tamassia
5
Directed Graphs
9
31
© 2010 Goodrich, Tamassia
Directed Graphs
32
Topological Sorting Example
Topological Sorting Example
2
3
3
4
6
4
6
5
7
7
8
8
9
9
Directed Graphs
© 2010 Goodrich, Tamassia
33
Topological Sorting Example
2
1
3
4
6
5
7
8
9
© 2010 Goodrich, Tamassia
5
Directed Graphs
35
© 2010 Goodrich, Tamassia
Directed Graphs
34