Graph traversal-BFS & DFS

gillrajandeep 2,456 views 35 slides Mar 04, 2019
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

Graph traversal methods- breadth-first search and depth-first search ie BFS and DFS


Slide Content

Graph Traversal
Depth-First Search
• Using Stack
Breadth-First Search
• Using Queue

Overview
•Goal
–To systematically visit the nodes of a graph
•A tree is a directed, acyclic, graph (DAG)
•If the graph is a tree,
–DFS is exhibited by preorder, postorder, and
(for binary trees) inorder traversals
–BFS is exhibited by level-order traversal

Depth-First Search
// recursive, preorder, depth-first search
void dfs (Node v) {
if (v == null)
return;
if (v not yet visited)
visit&mark(v); // visit node before adjacent nodes
for (each w adjacent to v)
if (w has not yet been visited)
dfs(w);
} // dfs

Example
0
7
1
5
4
3
2
6
Policy: Visit adjacent nodes in increasing index order

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6 4

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
Push 5

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5

Pop/Visit/Mark 5

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
1
2
Push 2, Push 1

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1

2
Pop/Visit/Mark 1

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1
0
4
2
Push 4, Push 0

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
4
2
Pop/Visit/Mark 0

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
3
7
4
2
Push 7, Push 3

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
7
4
2
Pop/Visit/Mark 3

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
7
4
2
2 is already in stack

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7
7
4
2
Pop/Mark/Visit 7

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7
6
4
2
Push 6

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6
4
2
Pop/Mark/Visit 6

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6 4


2
Pop/Mark/Visit 4

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6 4 2



Pop/Mark/Visit 2

DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 7 6 4 2


Done

Breadth-first Search
•Ripples in a pond
•Visit designated node
•Then visited unvisited nodes a distance i
away, where i = 1, 2, 3, etc.
•For nodes the same distance away, visit
nodes in systematic manner (eg. increasing
index order)

Breadth-First Search
// non-recursive, preorder, breadth-first search
void bfs (Node v) {
if (v == null)
return;
enqueue(v);
while (queue is not empty) {
dequeue(v);
if (v has not yet been visited)
mark&visit(v);
for (each w adjacent to v)
if (w has not yet been visited && has not been queued)
enqueue(w);
} // while
} // bfs

BFS: Start with Node 5
7
1
5
4
3
2
6
0

BFS: Start with Node 5
7
1
5
4
3
2
6
0
5

BFS: Node one-away
7
1
5
4
3
2
6
5
0
5 1 2

BFS: Visit 1 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1
0
5 1 2 0 4

BFS: Visit 2 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2
0
5 1 2 0 4

BFS: Visit 0 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0
0
5 1 2 0 4 3 7

BFS: Visit 4 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4
0
5 1 2 0 4 3 7

BFS: Visit 3 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4 3
0
5 1 2 0 4 3 7

BFS: Visit 7 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4 3 7
0
5 1 2 0 4 3 7 6

BFS: Visit 6 and add its adjacent
nodes
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0
5 1 2 0 4 3 7 6

BFS Traversal Result
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0

Applications of BFS
•Computing Distances: Given a source vertex x, compute the distance
of all vertices from x.
•Checking for cycles in a graph: Given an undirected graph G, report
whether there exists a cycle in the graph or not. (Note: won’t work for
directed graphs)
•Checking for bipartite graph: Given a graph, check whether it is
bipartite or not? A graph is said to be bipartite if there is a partition of
the vertex set V into two sets V
1
and V
2
such that if two vertices are
adjacent, either both are in V
1
or both are in V
2
.
•Reachability: Given a graph G and vertices x and y, determine if there
exists a path from x to y.

Applications of DFS
•Computing Strongly Connected Components: A directed graph is
strongly connected if there exists a path from every vertex to every
other vertex. Trivial Algo: Perform DFS n times. Efficient Algo:
Single DFS.
•Checking for Biconnected Graph: A graph is biconnected if removal of
any vertex does not affect connectivity of the other vertices. Used to
test if network is robust to failure of certain nodes. A single DFS
traversal algorithm.
•Topological Ordering: Topological sorting for Directed Acyclic Graph
(DAG) is a linear ordering of vertices such that for every directed edge
(x, y), vertex x comes before y in the ordering