Title: Spectral Analysis of Internet Topologies
17th Set of Lecture Notes
Thu Feb 5, 04.
Depth First Search in Directed
Graphs I . DAGS and Topological
Sorting
Algorithm Design Paradigm
Graph Theory , Recursion
Reading CLRS Paragraphs 22.3, 22.4
and these lecture notes.
2DFS in a directed graph does not necessarily
produce a single DFS tree
a
e
a ? b,d
b ? c,d
c ? a
g
f
b
d ? c
e ? a,f,g
f ? d,g
c
d
g ? e
In general, DFS in a directed graph produces a
DFS forest.
3a
e
DFS producing a forest with two trees.
g
f
b
a
e
c
d
g
f
b
DFS producing a single tree in the same
graph. (For a different order of visiting
vertices.)
c
d
4Characterization of DFS Edges in a Directed Graph
cross-edge
a
e
back-edge
forward edge
g
f
back-edge
b
cross-edge
c
d
5RECOGNITION DURING DFS
DEFINITION
v ? w is a back-edge iff w is an ancestor of v in
the DFS forest
finish(w)
start(v)
finish(v)
start(w)
start(v)
finish(v)
v ? w is a forward-edge iff w is a descendant of
v in the DFS forest
start(w)
finish(w)
v ? w is a cross-edge iff v and w are not in an
ancestral-descendant relationship in the DFS
forest
start(w)
finish(w)
start(v)
finish(v)
6Definition A directed graph is acyclic if and
only if it has no cycles. DAG
Directed Acyclic Graph.
cross-edge
a
e
forward edge
g
f
b
cross-edge
c
d
7FACT A directed graph is acyclic iff DFS
produces no back edges. (Note how we got a dag by
removing the two back-edges of the example
graph.)
PROOF If DFS produces a back edge Then
obviously the graph has a cycle.
Spse that the graph has a cycle. We need to
argue that DFS produces a back edge. Let e1, e2,
, ek be a cycle of the graph. Spse v was the
first vertex of the cycle visited by DFS. Then
DFS must visit all vertices of the cycle before
it finishes v. Therefore, when w will be
visited, w ?v will become a back edge.
w
v first vertex visited by DFS
cycle of graph
8Fact Every d.a.g. has an ordering of its
vertices such that all edges are directed from
left to right.
a
e
Example
sink
g
f
Heres one
c
g
sink
Heres another
c
g
c
Every dag has a sink a vertex without outgoing
edges (otherwise you could create a cycle). The
sink can always go first. Repeat.
Proof
9ALGORITHM to find a TOPOLOGICAL SORTING
Do DFS and produce an ordering of vertices
according to finish times.
The vertex with smallest finish times goes
first, the vertex with the second smallest finish
times goes second, e.t.c. So we just run DFS and
output vertices as they finish.
Example
1/8
9/14
Proof Realize that the vertex which finishes
first is a sink no tree-edges out of it, no
back-edges since dag, and no cross edges since
finishes first. Repeat.
a
e
2/7
g
f
b
11/12
10/13
3/4
5/6
c
d