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

BFS and DFS

This document discusses and provides examples of breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. BFS uses a queue data structure to traverse nodes level-by-level from the starting node, while DFS uses a stack data structure to explore nodes as deep as possible before backtracking. Pseudocode and step-by-step examples are provided to demonstrate the process of each algorithm on a sample graph.

Uploaded by

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

BFS and DFS

This document discusses and provides examples of breadth-first search (BFS) and depth-first search (DFS) graph traversal algorithms. BFS uses a queue data structure to traverse nodes level-by-level from the starting node, while DFS uses a stack data structure to explore nodes as deep as possible before backtracking. Pseudocode and step-by-step examples are provided to demonstrate the process of each algorithm on a sample graph.

Uploaded by

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

BFS AND DFS

• Graph Traversal
• Graph traversal means visiting each and exactly one node.
• Graph traversal is a process of checking or updating each vertex in a
graph. It is also known as Graph Search.
• Tree traversal is a special case of graph traversal.
• There are two techniques used in graph traversal:
BFS (Breadth First Search)
DFS (Depth First Search)
• BFS- Breadth First Search Graph traversal algorithm
• traverses a graph in a breadth ward motion
• uses a queue to remember to get the next vertex to start a search,
when a dead end occurs in any iteration.

• DFS (Depth First Search)


• Start from a vertex, visit all the reachable vertices in a depth first
manner
• Uses Stack for non-recursive implementation
BFS
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display
it. Insert it in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from
the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
BFS
• Step 1 - Define a Queue of size total number of vertices in the graph.
• Step 2 - Select any vertex as starting point for traversal. Visit that vertex
and insert it into the Queue.
• Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at
front of the Queue and insert them into the Queue.
• Step 4 - When there is no new vertex to be visited from the vertex which
is at front of the Queue then delete that vertex.
• Step 5 - Repeat steps 3 and 4 until queue becomes empty.
• Step 6 - When queue becomes empty, then produce final spanning tree
by removing unused edges from the graph
Enqueue the unvisited neighbor nodes: 1, 3, 8 Enqueue the unvisited neighbor nodes: 7
Next, visit the first node in the queue: 1 Next, visit the first node in the queue: 3
Enqueue the unvisited neighbor
nodes: none (Note: 4 is enqueued again,
Enqueue the unvisited neighbor nodes: 2, 4 but won't be visited twice, so I leave it out)
Next, visit the first node in the queue: 8 Next, visit the first node in the queue: 7
Enqueue the unvisited neighbor
nodes: none (Note: 2 is enqueued again, but won't
Enqueue the unvisited neighbor nodes: 5
be visited twice, so I leave it out) Next, visit the first node in the queue: 4
Next, visit the first node in the queue: 2
Enqueue the unvisited neighbor nodes: none
Next, visit the first node in the queue: 5

Enqueue the unvisited neighbor nodes: 6
Next, visit the first node in the queue: 6
2

nextNode = 0 0
nextNode = 1 8
nextNode = 3 1
nextNode = 8 3
nextNode = 7 4
nextNode = 2
nextNode = 4
nextNode = 5
nextNode = 6
public void BFS()
{
// BFS uses Queue data structure

Queue q = new LinkedList(); // I use Queue class in Java's


library

for (i = 0; i < visited.length; i++)


visited[i] = false; // Clear visited[]

q.add(0); // Start the "to visit" at node 0

/* ===========================================
Loop as long as there are "active" node
while( ! q.isEmpty() )
{
int nextNode; // Next node to visit
int i;

nextNode = q.remove();

if ( ! visited[nextNode] )
{
visited[nextNode] = true; // Mark node as visited
System.out.println("nextNode = " + nextNode );

for ( i = 0; i < NNodes; i++ )


if ( adjMatrix[nextNode][i] > 0 && ! visited[i] )
q.add(i);
} } }
public static void main(String[] args)
{
// 0 1 2 3 4 5 6 7 8
// ===================================================
int[][] conn = { { 0, 1, 0, 1, 0, 0, 0, 0, 1 }, // 0
{ 1, 0, 0, 0, 0, 0, 0, 1, 0 }, // 1
{ 0, 0, 0, 1, 0, 1, 0, 1, 0 }, // 2
{ 1, 0, 1, 0, 1, 0, 0, 0, 0 }, // 3
{ 0, 0, 0, 1, 0, 0, 0, 0, 1 }, // 4
{ 0, 0, 1, 0, 0, 0, 1, 0, 0 }, // 5
{ 0, 0, 0, 0, 0, 1, 0, 0, 0 }, // 6
{ 0, 1, 1, 0, 0, 0, 0, 0, 0 }, // 7
{ 1, 0, 0, 0, 1, 0, 0, 0, 0 } };// 8
Graph G = new Graph(conn);
G.BFS(); }
DFS traversal Algorithm
• Step 1 - Define a Stack of size is total number of vertices in the graph.
• Step 2 - Select any vertex as starting point for traversal. Visit that
vertex and push it on to the Stack.
• Step 3 - Visit any one of the non-visited adjacent vertices of a vertex
which is at the top of stack and push it on to the stack.
• Step 4 - Repeat step 3 until there is no new vertex to be visited from
the vertex which is at the top of the stack.
• Step 5 - When there is no new vertex to visit then use back tracking
and pop one vertex from the stack.
• Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7 - When stack becomes Empty, then produce final spanning tree
by removing unused edges from the graph
Push the unvisited neighbor nodes: 8, 3, 1 (I used the reverse
order to visit smaller node id first)
Next, visit the top node in the stack: 1

Push the unvisited neighbor nodes: 7
Next, visit the top node in the stack: 7
Push the unvisited neighbor nodes: 2
Next, visit the top node in the stack: 2

Push the unvisited neighbor nodes: 5, 3 (Note: 3 is pushed


again, and the previous value will be cancelled later -- as we
will see)
Next, visit the top node in the stack: 3
Push the unvisited neighbor nodes: 4
Next, visit the top node in the stack: 4

Push the unvisited neighbor nodes: 8 (Note: 8 is
pushed again, and the previous value will be cancelled
later -- as we will see)
Next, visit the top node in the stack: 8
Push the unvisited neighbor nodes: none
Next, visit the top node in the stack: 5

Push the unvisited neighbor nodes: 6
Next, visit the top node in the stack: 6
Push the unvisited neighbor nodes: none
Next, visit the top node in the stack: 3
3 is visited: skip

Next, visit the top node in the stack: 8
8 is visited: skip
nextNode = 0
nextNode = 1
nextNode = 7
nextNode = 2
nextNode = 3
nextNode = 4
nextNode = 8
nextNode = 5

nextNode = 6
public void DFS()
{
// DFS uses a Stack data structure
Stack s = new Stack(); // I use Stack class in Java's library
for (i = 0; i < visited.length; i++)
visited[i] = false; // Clear visited[]
s.push(0); // Start the "to visit" at node 0

/* ===========================================
Loop as long as there are "active" node
=========================================== */
while( ! s.isEmpty() )
{
int nextNode; // Next node to visit
int i;
nextNode = s.pop();
if ( ! visited[nextNode] )
{
visited[nextNode] = true; // Mark node as visited
System.out.println("nextNode = " + nextNode );

for ( i = NNodes-1; i >= 0; i-- )


if ( adjMatrix[nextNode][i] > 0 && ! visited[i] )
s.push(i);
}
}
}

You might also like