Digraphs
Digraphs
ORD
JFK
SFO
DFW
LAX
MIA
Digraph Properties D
C
A graph G=(V,E) such that B
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
E D C
A
C F
E D
A B
C F
A B
© 2015 Goodrich and Tamassia Directed Graphs 7
Strong Connectivity
Each vertex can reach all other
vertices
a
g
c
d
e
b
f
d
e
b
{f,d,e,b}
f
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 16
Floyd-Warshall, Iteration 1 v7
BOS
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 17
Floyd-Warshall, Iteration 2 v7
BOS
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 18
Floyd-Warshall, Iteration 3 v7
BOS
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 19
Floyd-Warshall, Iteration 4 v7
BOS
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 20
Floyd-Warshall, Iteration 5 v7
BOS
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 21
Floyd-Warshall, Iteration 6 v7
BOS
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 22
Floyd-Warshall, Conclusion v 7
BOS
ORD v4
JFK
v2 v6
SFO
DFW
LAX
v3
v1
MIA
v5
© 2015 Goodrich and Tamassia Directed Graphs 23
DAGs and Topological
Ordering
A directed acyclic graph (DAG) D E
is a digraph that has no
directed cycles B
A topological ordering of a
digraph is a numbering C
v1 , …, vn A DAG G
of the vertices such that for
every edge (vi , vj), we have i < j v4 v5
Example: in a task scheduling D E
digraph, a topological ordering v2
a task sequence that satisfies
B
the precedence constraints v3
Theorem
v1 C
A digraph admits a topological Topological
ordering if and only if it is a A ordering of
DAG
© 2015 Goodrich and Tamassia Directed Graphs G 24
Topological Sorting
Number vertices, so that (u,v) in E implies
u < v wake up 1 A typical student day
2 3
eat
study computer sci.
4 5
nap more c.s.
7
play
8
write c.s. program 6
9 work out
bake cookies
10
sleep 11
dream about
© 2015 Goodrich and Tamassia Directed Graphs
graphs 25
Algorithm for Topological
Sorting
Note: This algorithm is different than
the one in the book
Algorithm TopologicalSort(G)
HG // 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
nn-1
Remove v from H
9
© 2015 Goodrich and Tamassia Directed Graphs 29
Topological Sorting
Example
9
© 2015 Goodrich and Tamassia Directed Graphs 30
Topological Sorting
Example
7
8
9
© 2015 Goodrich and Tamassia Directed Graphs 31
Topological Sorting
Example
7
8
9
© 2015 Goodrich and Tamassia Directed Graphs 32
Topological Sorting
Example
6 5
7
8
9
© 2015 Goodrich and Tamassia Directed Graphs 33
Topological Sorting
Example
4
6 5
7
8
9
© 2015 Goodrich and Tamassia Directed Graphs 34
Topological Sorting
Example
3
4
6 5
7
8
9
© 2015 Goodrich and Tamassia Directed Graphs 35
Topological Sorting
Example
2
3
4
6 5
7
8
9
© 2015 Goodrich and Tamassia Directed Graphs 36
Topological Sorting
Example
2
1
3
4
6 5
7
8
9
© 2015 Goodrich and Tamassia Directed Graphs 37