0% found this document useful (0 votes)
504 views

Daa Lab 8 - Dfs and Bfs

Graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS) are used to process all vertices (or edges) of a graph systematically. DFS uses a stack and traverses vertices recursively, prioritizing depth over breadth. BFS uses a queue and searches all neighbors at each level before moving to the next level, prioritizing breadth over depth. These algorithms mark vertices as visited and process neighbors to find all reachable vertices from a starting point.

Uploaded by

Akshay Venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
504 views

Daa Lab 8 - Dfs and Bfs

Graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS) are used to process all vertices (or edges) of a graph systematically. DFS uses a stack and traverses vertices recursively, prioritizing depth over breadth. BFS uses a queue and searches all neighbors at each level before moving to the next level, prioritizing breadth over depth. These algorithms mark vertices as visited and process neighbors to find all reachable vertices from a starting point.

Uploaded by

Akshay Venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Graph Traversal Algorithms:

Many problems require processing all graph vertices (or


edges) in a systematic way.
Based on the order of visiting the vertices:
● Depth-first search (DFS)
○ Search everything related a one neighbor recursively
before starting with another
○ Uses Stack behavior
● Breadth-first search (BFS)
○ Search all the neighbors (first level) before the 2nd
level of nodes
○ Uses Queue behavior
0
0 123
1 5623
5 623
6 423
4 23
2 3
3
a
a bcd
b cefcd
c fefcd
f efcd
e fcd
cd
d
d g
g
A C
A BE C DGH
B E D HGH
E IJ H GKLGH
I JJ
G KKLGH
J J
K KLGH
<Empty>
LGH
L GH
F
H
F <Empty>
<Empty>
0
0 4
4 1
1 56
5 6
6 <Empty>

2
2 3
3 <Empty>
Algorithm DFS_MAIN( G(V, E) )
Mark each vertex in v with 0
for each vertex v in V
if (v is marked with 0)
dfs_recurse(v)

Procedure dfs_recurse(v)
Mark v with 1
for each vertex w in V adjacent to v
if (w is marked with 0)
dfs_recurse(w)
(a) Graph
(b) Stack of the DFS Traversal
(c) DFS Forest
i. Tree edges
ii. Back edges
(a) Graph
(b) Stack of the DFS Traversal
(c) DFS Forest
i. Tree edges
ii. Back edges
Breadth First Search (BFS)
(a) Graph
(b) Queue of the BFS Traversal
(c) BFS Forest
i. Tree edges
ii. Cross edges
Algorithm BFS_main( G(V, E) )
Mark each vertex in v with 0
for each vertex v in V
if(v is marked with 0)
bfs_node(v)

Procedure bfs_node(v)
Mark v with 1 and insert v into the Queue
while the Queue is not empty
v ← remove a vertex from the Queue
for each vertex w in V adjacent to v
if(w is marked with 0)
Mark w with 1
Add w to the Queue
Algorithm BFS(G)
Mark each vertex in v with 0
for each vertex v in V
if(v is marked with 0)
Mark v with 1
Insert v into the Queue
while the Queue is not empty
v ← remove a vertex from the Queue
for each vertex w in V adjacent to v
if(w is marked with 0)
Mark w with 1
Add w to the Queue
Problem for the lab:
Write a program to find the number of
components an undirected graph has. The graph
could be represented as an adjacency matrix.
(a) Using DFS traversal
(b) Using BFS traversal

The output is just a positive integer


indicating the number of components the given
undirected graph has.
Problem for the lab: Test-cases:
Sample Input 1:
3
0 1 0
1 0 0
0 0 0

Sample Output 1:
2

Sample Input 2:
2
0 0
0 0

Sample Output 2:
2
Problem for the lab: Test-cases:
Sample Input 3:
2
0 1
1 0

Sample Output 3:
1

Sample Input 4:
2
1 1
1 1

Sample Output 4:
1
Problem for the lab: Test-cases:
Sample Input 5:
3
0 0 0
0 0 0
0 0 0

Sample Output 5:
3

Sample Input 6:
3
1 1 1
1 0 0
1 0 0

Sample Output 6:
1
Problem for the lab: Test-cases:
Sample Input 7:
1
1

Sample Output 7:
1

Sample Input 8:
9
0 1 1 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1
0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0

Sample Output 8:
4
Practice problem for the lab:
Simulate a maze search using DFS technique.
Represent a 2-dimensional maze by grid of
nodes, where a node in the grid can
potentially connect to four other nodes in
the grid. Two nodes have an edge between them
if they are adjacent in the grid and there is
no obstacle in between. Print the index of
the nodes (i, j) visited during the maze
search. The search ends when all reachable
nodes from (0, 0) are visited.
Practice problem for the lab:
...
Input: ‘r’ rows and ‘c’ columns in the grid.
It is followed by r*c binary strings of 4
bits (like 1001) indicating the connection to
its right, bottom, left and top adjacent
nodes, respectively.

Output: Series of nodes with their locations


indices (like (4,0) indicating 4th row, 0th
column) in the order of their visit during
the maze search. The ones visited during
backtracking also to be printed.
Practice problem for the lab:
...
Sample
Input/
Output:

You might also like