Title: Digraphs
1 Digraphs
- Reachability
- Connectivity
- Transitive Closure
- Floyd-Warshall Algorithm
v
7
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
2Digraphs
1
A typical student day
wake up
3
2
eat
study computer sci.
5
4
work
more c.s.
7
play
8
write c.s. program
6
battletris
9
make cookies for c.s. prof.
10
sleep
11
dream of cs16
3Whats a Digraph?
- a) A small burrowing animal with long sharp teeth
and a unquenchable lust for the blood of computer
science majors - b) A distressed graph
- c) A directed graph
a
Each edge goes in one direction Edge (a,b) goes
from a to b, but not b to a
Youre saying, Yo, how about an example of how
we might be enlightened by the use of
digraphs!! - Well, if you insist. . .
4Applications
Maps digraphs handle one-way streets (especially
helpful in Providence)
5Another Application
- Scheduling edge (a,b) means task a must be
completed before b can be started
cs15
cs16
cs22
cs126
cs127
cs141
cs32
cs167
cs31
old computer scientists never die, they just fall
into black holes
6DAGs
- dag (noun) dÂ-g
- 1. Di-Acyl-Glycerol - My favorite snack!
- 2.mans best friend
- 3. directed acyclic graph
Say What?!
directed graph with no directed cycles
a
b
a
b
c
c
e
d
e
d
DAG
not a DAG
7Depth-First Search
- Same algorithm as for undirected graphs
- On a connected digraph, may yield unconnected DFS
trees (i.e., a DFS forest)
a
b
c
f
e
d
a
e
b
d
f
c
8Reachability
- DFS tree rooted at v vertices reachable from v
via directed paths
a
b
c
f
e
d
e
c
f
b
a
c
a
b
d
d
9Strongly Connected Digraphs
- Each vertex can reach all other vertices
a
g
c
d
e
b
f
10Strongly Connected Components
a
a , c , g
g
c
f , d , e , b
d
e
b
f
11Transitive Closure
- Digraph G is obtained from G using the rule If
there is a directed path in G from a to b, then
add the edge (a,b) to G
G
G
12Computing the Transitive Closure
- We can perform DFS starting at each vertex
- Time O(n(nm))
- Alternatively ... Floyd-Warshall Algorithm
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.
Pink Flo
yd
13Example
v
7
BOS
v
ORD
4
v
2
v
6
SFO
DFW
v
LAX
7
BOS
v
3
v
1
MIA
v
ORD
4
v
5
JFK
v
2
v
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
14Floyd-Warshall Algorithm
- Assumes that methods areAdjacent and
insertDirectedEdge take O(1) time (e.g.,
adjacency matrix structure) -
Algorithm FloydWarshall(G) let v1 ... vn be
an arbitrary ordering of the vertices G0
G for k 1 to n do // consider all possible
routing vertices vk Gk Gk-1// these are the
only ones you need to store for each (i, j
1, ..., n) (i ! j) (i, j ! k) do // for
each pair of vertices vi and vj if
Gk-1.areAdjacent(vi,vk) and
Gk-1.areAdjacent(vk,vj) then
Gk.insertDirectedEdge(vi,vj,null) return
Gn
Digraph Gk is the subdigraph of the transitive
closure of G induced by paths with intermediate
vertices in the set v1, ..., vk Running time
O(n3)
15Example
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
16Example
v
7
BOS
v
4
ORD
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
17Topological Sorting
- For each edge (u,v), vertex u is visited before
vertex v
1
A typical student day
w
ak
e up
3
2
eat
cs16 meditation
5
4
w
ork
more cs16
7
play
8
cs16 program
6
cxhe
xtris
9
mak
e cookies
for cs16 HT
A
10
sleep
11
dream of cs16
18Topological Sorting
- Topological sorting may not be unique
A
A B C D
or
A C B D
C
B
D
- You make the call!
19Topological Sorting
- Labels are increasing along a directed path
- A digraph has a topological sorting if and only
if it is acyclic (i.e., a dag)
1
A
2
3
B
C
5
4
E
D
20Algorithm for Topological Sorting
A
method
T
opolog
icalSor
t
if
there are more v
ertices
let
v
be a source
B
C
// a v
erte
x w/o incoming edges
label and remo
v
e
v
T
opolog
icalSor
t
D
E
21Algorithm (continued)
- Simulate deletion of sources using indegree
counters
T
opSort
(V
erte
x v)
label v
f
or
each
edge(v
,w)
inde
g(w) inde
g(w)
-
1
if
inde
g(w) 0
T
opSort
(w)
Compute indeg(v) for all vertices Foreach
vertex v do if v not labeled and indeg(v)
0 then TopSort(v)
22Example
?
0
?
0
A
B
?
1
C
D
?
3
?
2
E
F
?
1
G
?
2
H
?
1
I
?
3