3. Graph Search (traversal)
How do we search a graph?How do we search a graph?
– At a particular vertices, where shall we go next?At a particular vertices, where shall we go next?
Two common framework:Two common framework:
the depth-first search (DFS)the depth-first search (DFS)
the breadth-first search (BFS)the breadth-first search (BFS)
We are use DFSWe are use DFS
In DFS:In DFS:
– In DFS, go as far as possible along a single pathIn DFS, go as far as possible along a single path
until reach a dead end (a vertex with no edge outuntil reach a dead end (a vertex with no edge out
or no neighbor unexplored) then backtrackor no neighbor unexplored) then backtrack
4. Depth-First Search (DFS)
The basic idea behind this algorithm is that it traversesThe basic idea behind this algorithm is that it traverses
the graph using recursionthe graph using recursion
– Go as far as possible until you reach a deadendGo as far as possible until you reach a deadend
– Backtrack to the previous path and try the next branchBacktrack to the previous path and try the next branch
– The graph below, started at node a, would be visitedThe graph below, started at node a, would be visited
in the following order: a, b, c, g, h, i, e, d, f, jin the following order: a, b, c, g, h, i, e, d, f, j
da b
h i
c
g
e
j
f
5. DFS: Color Scheme
Vertices initially colored whiteVertices initially colored white
Then colored gray when discoveredThen colored gray when discovered
Then black when finishedThen black when finished
6. DFS: Time Stamps
Discover time d[u]: when u is firstDiscover time d[u]: when u is first
discovereddiscovered
Finish time f[u]: when backtrack fromFinish time f[u]: when backtrack from
uu
d[u] < f[u]d[u] < f[u]
20. DFS Example
1 |12 8 |11 13|
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
21. DFS Example
1 |12 8 |11 13|
14|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
22. DFS Example
1 |12 8 |11 13|
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
23. DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
24. DFS: Algorithm
DFS(G)
for each vertex u in V,
color[u]=white; π[u]=NIL
time=0;
for each vertex u in V
if (color[u]=white)
DFS-VISIT(u)
25. DFS-VISIT(u)
color[u]=gray;
time = time + 1;
d[u] = time;
for each v in Adj(u) do
if (color[v] = white)
π[v] = u;
DFS-VISIT(v);
color[u] = black;
time = time + 1; f[u]= time;
DFS: Algorithm (Cont.)
source
vertex
26. DFS: Kinds of edges
DFS introduces an important distinctionDFS introduces an important distinction
among edges in the original graph:among edges in the original graph:
– Tree edgeTree edge: encounter new (white) vertex: encounter new (white) vertex
– Back edgeBack edge: from descendent to ancestor: from descendent to ancestor
– Forward edgeForward edge: from ancestor to descendent: from ancestor to descendent
– Cross edgeCross edge: between a tree or subtrees: between a tree or subtrees
Note: tree & back edges are important;Note: tree & back edges are important;
most algorithms don’t distinguish forwardmost algorithms don’t distinguish forward
& cross& cross
27. DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex d f
Tree edges Back edges Forward edges Cross edges
28. DFS: Complexity Analysis
DFS_VISIT is called exactly once for each vertex
And DFS_VISIT scans all the edges which causes cost of
O(E)
Initialization complexity is O(V)
Overall complexity is O(V + E)