Graph Traversal
description
Transcript of Graph Traversal
Graph TraversalGraph Traversal
Chapter 9
ProblemProblem
Given a directed or undirected graph G=(V,E), find a way to traverse all of its vertices.
SolutionSolution
Depth First Search (DFS) Breadth First Search (BFS)
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
DFSDFS
And so on ...
DFSDFS
We need also to record
– the birth time of any node: the time of the first visit to the node.
– the death time: the time after all of its children are visited and ..also dead
Algorithm: DFSAlgorithm: DFSInput: graph G=(V,E) directed or undirected
Output: preordering of the vertices in DFS spanning tree (or forest)
1. predfn 0; postdfn 0; % global variables
2. For each vertex vV3. mark v unvisited 4. End for5. For each vV % for connected components
6. if v is unvisited then dfs(v)7. End for
Procedure dfs(v)Procedure dfs(v)
1. Mark v visited
2. predfn predfn + 1 % birth time
3. For each edge (v,w) E
4. if w is marked unvisited then dfs(w)
5. End for
6. postdfn postdfn + 1 % death time
DefinitionsDefinitions
In a directed graph, (u,v) is– a tree edge, if it is in the DFS tree, i.e., v is
marked unvisited – back edge, if v is marked visited &
ancestor of u– Forward edge, if v marked visited &
descendant of u– Cross edge, if it is otherwise.
Tree edge
back edge
Forward edge
Cross edge
DefinitionsDefinitions
In an undirected graph, (u,v) is– a tree edge if it is in the DFS tree – back edges, otherwise.
ExampleExample
a
c
d
b f
e1
ExampleExample
a
c
d
b f
e1
2
ExampleExample
a
c
d
b f
e1
2
3
ExampleExample
a
c
d
b f
e1
2
3
4
ExampleExample
a
c
d
b f
e1
2
3
4
5
ExampleExample
a
c
d
b f
e1
2
3
4
5Back edge
ExampleExample
a
c
d
b f
e1
2
3
4
5, 1Back edge
ExampleExample
a
c
d
b f
e1
2
3
4
5, 1Back edge
6
ExampleExample
a
c
d
b f
e1
2
3
4
5, 1Back edge
6
Cross edge
ExampleExample
a
c
d
b f
e1
2
3
4
5, 1Back edge
6
Cross edge
Back edge
ExampleExample
a
c
d
b f
e1
2
3
4
5, 1Back edge
6, 2
Cross edge
Back edge
ExampleExample
a
c
d
b f
e1
2
3
4, 3
5, 1Back edge
6, 2
Cross edge
Back edge
ExampleExample
a
c
d
b f
e1
2
3, 4
4, 3
5, 1Back edge
6, 2
Cross edge
Back edge
ExampleExample
a
c
d
b f
e1
2, 33, 4
4, 3
5, 1Back edge
6, 2
Cross edge
Back edge
ExampleExample
a
c
d
b f
e1
2, 33, 4
4, 3
5, 1Back edge
6, 2
Cross edge
Back edge
Forward edge
ExampleExample
a
c
d
b f
e1, 6
2, 53, 4
4, 3
5, 1Back edge
6, 2
Cross edge
Back edge
Forward edge
Analysis of DFSAnalysis of DFS
Time = (m + n)
Applications: Testing acyclicity (is G a tree?) Strongly connected components
BFSBFS
BFSBFS
BFSBFS
BFSBFS
BFSBFS
BFSBFS
BFSBFS
BFSBFS
And so on ..
Algorithm: BFSAlgorithm: BFSInput: graph G=(V,E) directed or undirected
Output: ordering of the vertices in BFS spanning tree (or forest)
1. bfn 0; % global variable
2. For each vertex vV3. mark v unvisited 4. End for5. For each vV % for connected components
6. if v is unvisited then bfs(v)7. End for
Procedure bfs(v)Procedure bfs(v)
1. Q { v }
2. Mark v visited3. While Q { }4. v pop(Q)5. bfn bfn + 1 % birth time= time to give birth to all of its children
6. For each edge (v,w) E7. if w is marked unvisited then 8. push(w, Q)9. mark w visited 10. end if11. End for12. End while
NoteNote
Order in which nodes are popped
= order in which they are pushed
= order in which are born
= order in which they give birth
ExampleExample
a
bc
c
e
f
d
g h
1
ExampleExample
a
bc
c
e
f
d
g h
1
2
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
6
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
67
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
67
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
67 8
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
67 8
9
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
67 8
9
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
67 8
9
ExampleExample
a
bc
c
e
f
d
g h
1
2
3
4
5
67 8
9
AnalysisAnalysis
Time = (m + n)
Applications: Computing the distances