09 GTraverse
09 GTraverse
B G Breadth-FirstSearch
Search
Breadth-First
Starting node
F C E A D
Depth-FirstSearch
Depth-First Search
B G
6 3 4 5 7
7 6
Finding Connected Components
Input: a symmetric digraph G, with n nodes and 2m edges(interpreted
as an undirected graph), implemented as a array adjVertices[1,…n] of
adjacency lists.
Output: an array cc[1..n] of component number for each node vi
void connectedComponents(Intlist[ ] adjVertices, int n,
int[ ] cc) // This is a wrapper procedure
int[ ] color=new int[n+1];
Depth-first search
int v;
<Initialize color array to white for all vertices>
for (v=1; vn; v++)
if (color[v]==white)
ccDFS(adjVertices, color, v, v, cc);
return
ccDFS: the procedure
void ccDFS(IntList[ ] adjVertices, int[ ] color, int v, int ccNum, int [ ] cc)//v as
the code of current connected component
int w;
IntList remAdj; The elements of
The elements of
remAdjare
remAdj are
color[v]=gray; neighborsof
ofvv
cc[v]=ccNum; neighbors
remAdj=adjVertices[v];
Processingthe
Processing thenext
nextneighbor,
neighbor,
while (remAdjnil)
w=first(remAdj); ififexisting,
existing,another
anotherdepth-first
depth-first
if (color[w]==white) searchtotobe
search beincurred
incurred
ccDFS(adjVertices, color, w, ccNum, cc);
remAdj=rest(remAdj);
color[v]=black;
return
v finished
Analysis of CC Algorithm
connectedComponents, the wrapper
Linear in n (color array initialization+for loop on adjVertices )
ccDFS, the depth-first searcher
In one execution of ccDFS on v, the number of instructions(rest(remAdj))
executed is proportional to the size of adjVertices[v].
Note: (size of adjVertices[v]) is 2m, and the adjacency lists are traveresed
only once.
So, the complexity is in (m+n)
Extra space requirements:
color array
activation frame stack for recursion
Depth-First Search Trees
DFSforest={(DFS
DFS forest={(DFStree1),
tree1),(DFS
(DFStree2)}
tree2)}
Root of tree 1
B.E
A D
T.E
C.
T. E
E
B. E
B G
D.
E
E
C.
E
T. E
T.E
B.
T.E:tree
T.E: treeedge
edge
T. E
B.E:back
B.E: backedge
edge F C E
C.E C.E
D.E:descendant
D.E: descendant Root of tree 2
edge
edge
C.E:cross
crossedge
edge AAfinished
finishedvertex
vertexisisnever
never
C.E:
revisited,
revisited,such
suchas
asCC
Visits On a Vertex
Classification for the visits on a vertex
First visit(exploring): status: whitegray
(Possibly) multi-visits by backtracking to: status keeps
gray
Last visit(no more branch-finished): status: grayblack
Different operations can be done on the vertex or
(selected) incident edges during the different visits on
a specific vertex
Depth-First Search: Generalized
Input: Array adjVertices for graph G
Output: Return value depends on application.
int dfsSweep(IntList[] adjVertices,int n, …)
int ans;
<Allocate color array and initialize to white>
For each vertex v of G, in some order
if (color[v]==white)
int vAns=dfs(adjVertices, color, v, …);
<Process vAns>
// Continue loop
return ans;
Depth-First Search: Generalized
int dfs(IntList[] adjVertices, int[] color, int v, …)
int w;
IntList remAdj; IfIfpartial
partialsearch
searchisisused
usedfor
foraa
int ans; application,tests
application, testsfor
fortermination
termination
color[v]=gray; maybe
may beinserted
insertedhere.
here.
<Preorder processing of vertex v>
remAdj=adjVertices[v]; Specialized for
while (remAdjnil) connected
w=first(remAdj); components:
if (color[w]==white) •parameter added
<Exploratory processing for tree edge vw> •preorder processing
int wAns=dfs(adjVertices, color, w, …); inserted – cc[v]=ccNum
< Backtrack processing for tree edge vw , using wAns>
else
<Checking for nontree edge vw>
remAdj=rest(remAdj);
<Postorder processing of vertex v, including final computation of ans>
color[v]=black;
return ans;
Breadth-First Search: the Skeleton
Input: Array adjVertices for graph G
Output: Return value depends on application.
void bfsSweep(IntList[] adjVertices,int n, …)
int ans;
<Allocate color array and initialize to white>
For each vertex v of G, in some order
if (color[v]==white)
void bfs(adjVertices, color, v, …);
// Continue loop
return;
Breadth-First Search: the Skeleton
C.
2/7 T. E
E
B. E
12/13
B G
D.
C.
E
T. E
T.E
C.
summarized in the
T. E
F C E next frame
8/9 C.E C.E
3/4 11/14
Time
1 2 3 4 5 6 7 8 9 10 11 12 13
14 A E
B F G
C D
Properties about Active Intervals(1)
If w is a descendant of v in the DFS forest, then
active(w)active(v), and the inclusion is proper if wv.
Proof:
Define a partial order <: w<v iff. w is a proper descendants of v
active(w)active(x).
Let w be any proper descendant of v in the DFS tree, there
xi P2
w
Proof of White Path Theorem
Proof
All the vertices in the path are descendants of v.