Graphs part 2 Depth First Search (DFS) Search deeply first

Graphs part 2
Depth First Search (DFS)
Search deeply first… Explores edges out of most recently discovered vertex so
long as there are still unexplored edges leaving it… and then backtracks (like
searching in a maze). Run time as in BFS will be linear in number of vertices and
edges O(n+m)
Applications: topological sort in directed acyclic graphs (sequences of events
where one comes before another; later); strongly connected components (later).
Simple example of DFS ordering showing main idea and distinguishing from bfs:
1%
2%
3%
6%
a%
c%
e%
d%
f%
4%
7%
S%
b%
5%
Note the “forest” (not just a single tree).
One way to implement: Stack instead of queue. Last in first out. But often
implemented recursively.
Marking nodes in algorithm: Colors are the same:
White: Vertices are initially white;
Gray: First time vertex encountered, make gray
Black: Only when all its adjacent neighbors discovered, turn to black.
We also mark a node with a number “time stamp” both when it is first discovered
(turns gray) and when we have gone through all its neighbors (black). The
discovery time is u.d and the finish time is u.f (u.f > u.d).
Main algorithm with recursion: We’ll have a function DFS that initializes, and then
calls DFS-Visit, which is a recursive function and does the depth first search.
DFS(G)
// G is a graph
- initialize all vertices to white
- set all time stamps to 0
- for each vertex u in G.V
if u.color == white
DFS-Visit(G,u)
Recursion:
DFS-Visit(G,u)
1. time = time + 1
// white vertex u has just been discovered
2. u.d = time
// start time
3. u.color = gray
// we’ve discovered u
4. For each v in adjacency list of u
5.
if v.color == white
6.
v.parent = u
7.
DFS-Visit(G, v)
8. u.color = black
// we finished going through all of u’s neighbors
9. time = time + 1
10. u.f = time
// finish time
Example of DFS:
22.3 Depth-first search
u
1/
v
w
u
1/
x
y
(a)
z
x
u
1/
v
2/
w
u
1/
B
v
2/
w
u
1/
v
2/
y
(b)
z
x
3/
y
(c)
v
2/
w
u
1/
v
2/
B
4/
x
3/
y
u
1/
v
2/7
F
605
z
(e)
w
B
z
w
u
1/
v
2/
4/
x
3/
y
(d)
u
1/
v
2/7
B
4/5
x
3/
y
u
1/8
v
2/7
F
w
z
(f)
w
B
w
z
w
B
4/5
x
3/6
y
z
4/5
x
3/6
y
z
u
1/8
v
2/7
w
9/
u
1/8
v
2/7
w
9/
F
(g)
B
F
(h)
B
C
4/5
x
3/6
y
(i)
z
4/5
x
3/6
y
(j)
z
4/5
x
3/6
y
(k)
z
4/5
x
3/6
y
(l)
z
u
1/8
v
2/7
w
9/
u
1/8
v
2/7
w
9/
u
1/8
v
2/7
w
9/
u
1/8
v
2/7
w
9/12
F
4/5
x
B
C
3/6
y
(m)
F
10/
z
4/5
x
B
C
3/6
y
(n)
10/
z
B
F
4/5
x
B
C
3/6
y
(o)
10/11
z
B
F
4/5
x
B
C
3/6
y
(p)
10/11
z
B
Figure 22.4 The progress of the depth-first-search algorithm DFS on a directed graph. As edges
are explored by the algorithm, they are shown as either shaded (if they are tree edges) or dashed
Run time DFS:(otherwise).
O(n + m)Nontree
with nedges
number
vertices
and
m number
edges.
are labeled
B, C, or
F according
to whether
they Procedure
are back, cross, or
forward
edges. Timestamps
indicate
discovery
time/finishing
DFS-Visit is called
exactly
once forwithin
eachvertices
vertex
(only
if white
and thentimes.
changed).
During the DFS-Visit all adjacent nodes of each vertex are used, so overall
includes all edges.
the root of a new tree in the depth-first forest. When DFS returns, every vertex u
has been assigned a discovery time u:d and a finishing time u:f .
Some properties DFS:
In each call DFS-V ISIT .G; u/, vertex u is initially white. Line 1 increments
global
variable
time, line
2 records
theofnew
value
of of
time
discovery
1. Every vertexthe
v that
gets
discovered
between
start
u and
end
u, as
is athechild
time
u:d,
and
line
3
paints
u
gray.
Lines
4–7
examine
each
vertex
!
adjacent
of u in the DFS tree. The v will also finish before u does (see path from u to v to y to u
and recursively visit ! if it is white. As each vertex ! 2 AdjŒu" is considered in
to x).
line 4, we say that edge .u; !/ is explored by the depth-first search. Finally, after
every
leaving DFS-Visit(u)
u has been explored,
black,
increment time,
This can be seen inedge
recursion:
calledlines
first;8–10
and paint
then ucalls
DFSand recordfirst
the (last
finishing
timeout,
in u:f
visit(v), which finishes
in first
as. in a stack)
Note that the results of depth-first search may depend upon the order in which
2. There is never
a black
to a white
(because
a node
linean
5 ofedge
DFSfrom
examines
the vertices
andnode
upon the
order inifwhich
lineis4 of DFSblack, we already
visited
all
its
neighbors).
V ISIT visits the neighbors of a vertex. These different visitation orders tend not
3. White path theorem: vertex v is a descendant of vertex u if and only if when
the search discovers u, there is a path from u to v consisting entirely of white
vertices.
(example, look at panel a in example, nodes u and y, when u is discovered the
path to y is all white). Proof: induction (we won’t show).
4. Disjoint trees, contained trees, and parenthesis theorem: Consider vertices u
and v. Either:
a. [u.d u.f] and [v.d v.f] are entirely disjoint. Neither u or v is a descendant of the
other.
b. Or [u.d u.f] is contained entirely within [v.d v.f], and u is a descendant of v
(which means u was discovered after v)
c. Or vice versa; [v.d v.f] is contained entirely within [u.d u.f], and v is a
descendant
u (which means
22.3of Depth-first
searchv was discovered after u)
We represent start time of u with “(u”, and end time with “u)” …
y
3/6
z
2/9
s
1/10
B
(a)
4/5
x
F
7/8
w
C
C
t
11/16
C
12/13
v
B
14/15
u
C
s
t
z
(b)
y
v
u
w
x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
(s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)
s
C
t
B
5. Classification of edges: edge (u,v).
We did not discuss this in class. I am keeping it in the notes for completeness,
but not required.
Tree edges: v was discovered from u, during the procedure. If this is the case,
then when we were at u and checked out its adjacency neighbor v, v was white.
Back edges: connects u to ancestor v. This happens when we are in u and
checking adjacency neighbor v, but v is already gray (so v is like a grandfather).
(This also includes self-loops.)
Forward edges: connects u to a descendant v (but not part of the tree; not direct
child that was discovered). This happens during the procedure if we are in u and
check out adjacency neighbor v, and v is black.
Cross edges: everything else. Can be between trees or in the same tree. Here
also v is black.
Note: Some ambiguity since in undirected graph, edge between u and v could
also mean edge between v and u: can classify based on which is encountered
first in procedure, or which is first in list of edge types.
Note: in undirected graph, every edge either a tree edge or back edge.
Application DFS: topological sort:
A directed edge from u to v, means that v happens after u. Topological order: All
directed edges only go forward (needs to be acyclic graph). When we have tasks
that one needs to be done before another. Again linear time in vertices and
edges O(n+m), since doing DFS.
22.4 Topological sort
11/16
undershorts
socks 17/18
pants
shoes 13/14
12/15
(a)
613
6/7
shirt 1/8
belt
tie
watch 9/10
2/5
jacket 3/4
(b)
socks
undershorts
pants
shoes
watch
shirt
belt
tie
jacket
17/18
11/16
12/15
13/14
9/10
1/8
6/7
2/5
3/4
Figure 22.7 (a) Professor Bumstead topologically sorts his clothing when getting dressed. Each
directed edge .u; !/ means that garment u must be put on before garment !. The discovery and
finishing times from a depth-first search are shown next to each vertex. (b) The same graph shown
topologically sorted, with its vertices arranged from left to right in order of decreasing finishing time.
All directed edges go from left to right.
pants). A directed edge .u; !/ in the dag of Figure 22.7(a) indicates that garment u
must be donned before garment !. A topological sort of this dag therefore gives an
Pseudo code:
SortFigure
(G) 22.7(b) shows the topologically sorted dag as an
orderTopological
for getting dressed.
ordering of vertices along a horizontal line such that all directed edges go from left
1. Call DFS(G)
to compute finish times v.f for each vertex v
to right.
2. As each vertex
is finished,
insert into
the frontsorts
of aa dag:
linked list
The following
simple algorithm
topologically
3. return the linked list of vertices
T OPOLOGICAL -S ORT .G/
Application1 DFS:
Strongly
connected
components.
Next.
call DFS.G/
to compute
finishing
times !:f for each
vertex !
2 as each vertex is finished, insert it onto the front of a linked list
3 return the linked list of vertices
Figure 22.7(b) shows how the topologically sorted vertices appear in reverse order
of their finishing times.
We can perform a topological sort in time ‚.V C E/, since depth-first search
takes ‚.V C E/ time and it takes O.1/ time to insert each of the jV j vertices onto
the front of the linked list.
We prove the correctness of this algorithm using the following key lemma characterizing directed acyclic graphs.