Digraphs
Digraphs
A digraph is a graph
Directed Graphs BOS
whose edges are all
directed
E
ORD
Short for “directed graph” D
JFK
SFO
Applications C
one-way streets
LAX
DFW
flights B
MIA
task scheduling
A
© 2010 Goodrich, Tamassia Directed Graphs 1 © 2010 Goodrich, Tamassia Directed Graphs 2
© 2010 Goodrich, Tamassia Directed Graphs 3 © 2010 Goodrich, Tamassia Directed Graphs 4
Directed DFS Reachability
We can specialize the traversal
algorithms (DFS and BFS) to
E
DFS tree rooted at v: vertices reachable
digraphs by traversing edges
only along their direction from v via directed paths
D E D
In the directed DFS algorithm,
we have four types of edges
E D C
discovery edges C
back edges
A
forward edges B C F
cross edges
E D
Strong Connectivity
Strong Connectivity Algorithm
Each vertex can reach all other vertices Pick a vertex v in G
a
Perform a DFS from v in G G: c
g
© 2010 Goodrich, Tamassia Directed Graphs 7 © 2010 Goodrich, Tamassia Directed Graphs 8
Strongly Connected
Components Transitive Closure
Maximal subgraphs such that each vertex can reach Given a digraph G, the D E
all other vertices in the subgraph transitive closure of G is the
digraph G* such that B
Can also be done in O(n+m) time using DFS, but is G
G* has the same vertices C
more complicated (similar to biconnectivity).
as G
A
a if G has a directed path
g {a,c,g} from u to v (u ≠ v), G*
c
has a directed edge from D E
u to v
d
e The transitive closure B
{f,d,e,b} provides reachability
b information about a digraph C
f
A G*
© 2010 Goodrich, Tamassia Directed Graphs 9 © 2010 Goodrich, Tamassia Directed Graphs 10
ORD v4 ORD v4
JFK JFK
v2 v6 v2 v6
SFO SFO
DFW DFW
LAX LAX
v3 v3
v1 v1
MIA MIA
v5 v5
© 2010 Goodrich, Tamassia Directed Graphs 15 © 2010 Goodrich, Tamassia Directed Graphs 16
Floyd-Warshall, Iteration 3 v7 Floyd-Warshall, Iteration 4 v7
BOS BOS
ORD v4 ORD v4
JFK JFK
v2 v6 v2 v6
SFO SFO
DFW DFW
LAX LAX
v3 v3
v1 v1
MIA MIA
v5 v5
© 2010 Goodrich, Tamassia Directed Graphs 17 © 2010 Goodrich, Tamassia Directed Graphs 18
ORD v4 ORD v4
JFK JFK
v2 v6 v2 v6
SFO SFO
DFW DFW
LAX LAX
v3 v3
v1 v1
MIA MIA
v5 v5
© 2010 Goodrich, Tamassia Directed Graphs 19 © 2010 Goodrich, Tamassia Directed Graphs 20
Floyd-Warshall, Conclusion v7
DAGs and Topological Ordering
BOS
A directed acyclic graph (DAG) is a D E
v4 digraph that has no directed cycles
ORD
A topological ordering of a digraph B
JFK is a numbering
v2 v1 , …, vn
C
v6
of the vertices such that for every A DAG G
SFO edge (vi , vj), we have i < j
Example: in a task scheduling v4 v5
digraph, a topological ordering a D E
DFW task sequence that satisfies the v2
precedence constraints
LAX
v3 B
Theorem v3
v1
MIA A digraph admits a topological v1 C
ordering if and only if it is a DAG Topological
v5 A ordering of G
© 2010 Goodrich, Tamassia Directed Graphs 21 © 2010 Goodrich, Tamassia Directed Graphs 22
© 2010 Goodrich, Tamassia Directed Graphs 25 © 2010 Goodrich, Tamassia Directed Graphs 26
9 9
© 2010 Goodrich, Tamassia Directed Graphs 27 © 2010 Goodrich, Tamassia Directed Graphs 28
Topological Sorting Example Topological Sorting Example
7 7
8 8
9 9
© 2010 Goodrich, Tamassia Directed Graphs 29 © 2010 Goodrich, Tamassia Directed Graphs 30
4
6 5 6 5
7 7
8 8
9 9
© 2010 Goodrich, Tamassia Directed Graphs 31 © 2010 Goodrich, Tamassia Directed Graphs 32
Topological Sorting Example Topological Sorting Example
2
3 3
4 4
6 5 6 5
7 7
8 8
9 9
© 2010 Goodrich, Tamassia Directed Graphs 33 © 2010 Goodrich, Tamassia Directed Graphs 34
3
4
6 5
7
8