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

BFS and DFS

The document explains two graph traversal algorithms: Breadth-first Search (BFS) and Depth-first Search (DFS). BFS explores all vertices at the same level before moving deeper, using a queue to manage visited and unvisited nodes, while DFS explores as far as possible along each branch before backtracking, utilizing a stack for tracking. Both algorithms categorize vertices as visited or not visited to avoid cycles during traversal.

Uploaded by

Nancy P
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)
2 views

BFS and DFS

The document explains two graph traversal algorithms: Breadth-first Search (BFS) and Depth-first Search (DFS). BFS explores all vertices at the same level before moving deeper, using a queue to manage visited and unvisited nodes, while DFS explores as far as possible along each branch before backtracking, utilizing a stack for tracking. Both algorithms categorize vertices as visited or not visited to avoid cycles during traversal.

Uploaded by

Nancy P
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/ 3

BFS: Breadth-first Search (BFS) is a graph traversal algorithm that explores all the vertices

of a graph in a breadthwise order. It means it starts at a given vertex or node and visits all the
vertices at the same level before moving to the next level. It is a recursive algorithm for
searching all the vertices of a graph or tree data structure.

BFS Algorithm
For the BFS implementation, we will categorize each vertex of the graph into two categories:
 Visited
 Not Visited
The reason for this division is to prevent the traversal of the same node again thus avoiding
cycles in the graph.
Breadth First Traversal in Data Structure uses a queue to keep track of the vertices to visit.
BFS algorithm works:

1. Start by selecting a starting vertex or node.


2. Add the starting vertex to the end of the queue.
3. Mark the starting vertex as visited and add it to the visited array/list.
4. While the queue is not empty, do the following steps:
o Remove the front vertex from the queue.
o Visit the removed vertex and process it.
o Enqueue all the adjacent vertices of the removed vertex that have not been
visited yet.
o Mark each visited adjacent vertex as visited and add it to the visited array/list.
5. Repeat step 4 until the queue is empty.

create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited)
neighbours of u
DFS: The depth-first search algorithm works by starting from an arbitrary node of the graph
and exploring as far as possible before backtracking i.e. moving to an adjacent node until
there is no unvisited adjacent node. After backtracking, it repeats the same process for all the
remaining vertices which have not been visited till now. It is a recursive algorithm for
searching all the vertices of a graph or tree data structure.

DFS Algorithm
For the DFS implementation, we will categorize each vertex of the graph into two categories:
 Visited
 Not Visited
The reason for this division is to prevent the traversal of the same node again thus, avoiding
cycles in the graph. Depth First Traversal in Data Structure uses a stack to keep track of the
vertices to visit. A boolean visited array is used to mark the visited vertices.

DFS algorithm
1. Start by selecting a starting vertex or node.
2. Add the starting vertex on top of a stack.
3. Take the top item of the stack and add it to the visited list/array.
4. Create a list of that vertex's adjacent nodes. Add the ones that aren't in the visited list
to the top of the stack.
Keep repeating steps 3 and 4 until the stack is empty.

You might also like