DFS (V) - Recursive Version: Fully-Explored
DFS (V) - Recursive Version: Fully-Explored
Global Initialization:
for all nodes v, v.dfs# = -1 // mark v "undiscovered"
dfscounter = 0
for v = 1 to n do
if state(v) != fully-explored then
DFS(v):
DFS(v)
v.dfs# = dfscounter++ // v "discovered", number it
Mark v ``discovered”.
for each edge (v,x)
if (x.dfs# == -1) // (x previously undiscovered)
DFS(x)
else …
Mark v “fully-explored”
Kinds of edges – DFS on
Edge (u,v) directed graphs
Tree
[u [v v] u]
Forward
[u [v v] u]
Cross
[v v] [u u]
Back
134
[v [u u] v]
Topological Sort using DFS
Global Initialization:
for all nodes v, v.dfs# = -1 // mark v "undiscovered"
dfscounter = 0
current_label = n
for v = 1 to n do
if state(v) != fully-explored then
DFS(v):
DFS(v)
v.dfs# = dfscounter++ // v "discovered", number it
Mark v ``discovered”.
for each edge (v,x)
if (x.dfs# == -1) // (x previously undiscovered)
DFS(x)
else // add check for cycle if needed
Mark v “fully-explored”
f(v) = current_label // f(v) values give the topological order
current_label --;
Analysis
Running time O(n+m)
137
€
DFS(v) – Recursive version
Global Initialization:
for all nodes v, v.dfs# = -1 // mark v "undiscovered"
dfscounter = 0 // (global variable)
DFS(s); // start DFS at node s;
DFS(v)
v.dfs# = dfscounter++ // v "discovered", number it
for each edge (v,x)
if (x.dfs# = -1) // tree edge (x previously undiscovered)
DFS(x)
138
Application: Articulation Points
A node in an undirected graph is an
articulation point iff removing it
disconnects the graph
139
Identifying key proteins on the anthrax predicted network
140
Articulation point proteins Ram Samudrala/Jason McDermott
Articulation Points
1 articulation point
iff its removal
disconnects
2 the graph
10
3
11
12
7 8
13
6 9
4
141
5
Articulation Points
1
2
10
3
11
12
7 8
13
6 9
4
142
5
Simple Case: Artic. Pts in a tree
Which nodes in a rooted tree are articulation
points?
143
Simple Case: Artic. Pts in a tree
Leaves – never articulation points
Internal nodes – always articulation points
Root – articulation point if and only if two or
more children
146
Simple Case: Artic. Pts in a tree
Leaves – never articulation points
Internal nodes – always articulation points
Root – articulation point if and only if two or
more children
148
Articulation Points from DFS
Root node is an articulation point
iff it has more than one child
Leaf is never an articulation point
non-leaf, non-root u
node u is an y x
articulation point
⇔
150
LOW(v) is the lowest dfs# of any vertex that is either in
the dfs subtree rooted at v (including v itself) or connected
to a vertex in that subtree by a back edge.
A 1
2 B Vertex DFS # Low
A 1
B 2
3
C C 3
D 4
4 8 E 8
D E F 5
9 G 9
5 H 10
F G H 10
11 I 6
J J 11
6
I L 12 K 7
L 12
7 M 13
K M 13 151
Articulation Points
A 1
2 B Vertex DFS # Low
A 1 1
B 2 1
3
C C 3 1
D 4 3
4 8 E 8 1
D E F 5 3
9 G 9 9
5 H 10 1
F G H 10
11 I 6 3
J J 11 10
6
I L 12 K 7 3
L 12 10
7 M 13 13
K M 13 152
Articulation Points
A 1
2 B Vertex DFS # Low
A 1 1
B 2 1
3
C C 3 1
D 4 3
4 8 E 8 1
D E F 5 3
9 G 9 9
5 H 10 1
F G H 10
11 I 6 3
J J 11 10
6
I L 12 K 7 3
L 12 10
7 M 13 13
K M 13 153
Articulation Points:
the "LOW" function
Definition: LOW(v) is the lowest dfs# of any
vertex that is either in the dfs subtree rooted at v
(including v itself) or connected to a vertex in that
subtree by a back edge.
154
Articulation Points:
the "LOW" function
Definition: LOW(v) is the lowest dfs# of any
vertex that is either in the dfs subtree rooted at v
(including v itself) or connected to a vertex in that
subtree by a back edge.
v (non-root) articulation point iff some
child x of v has LOW(x) ≥ dfs#(v)
155
Articulation Points:
the "LOW" function
Definition: LOW(v) is the lowest dfs# of any
vertex that is either in the dfs subtree rooted at v
(including v itself) or connected to a vertex in that
subtree by a back edge.
v (nonroot) articulation point iff some child x of v
has LOW(x) ) ≥ dfs#(v)
LOW(v) =
min ( {dfs#(v)} ∪ {LOW(w) | w a child of v } ∪
{ dfs#(x) | {v,x} is a back edge from v } ) 156
DFS(v) for
Finding Articulation Points
Global initialization: v.dfs# = -1 for all v.