0% found this document useful (0 votes)
72 views2 pages

BFSvs DFS

Breadth-first search (BFS) and depth-first search (DFS) are two algorithms used to traverse graphs and trees. BFS explores a graph level-by-level using a queue, visiting all nodes at each depth before moving deeper. DFS explores as deep as possible along each branch using a stack, before backtracking. While BFS guarantees completeness and finds shortest paths, DFS uses less memory and is useful for topological sorting and puzzles. The optimal algorithm depends on the specific problem and graph properties.

Uploaded by

Bhavya
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)
72 views2 pages

BFSvs DFS

Breadth-first search (BFS) and depth-first search (DFS) are two algorithms used to traverse graphs and trees. BFS explores a graph level-by-level using a queue, visiting all nodes at each depth before moving deeper. DFS explores as deep as possible along each branch using a stack, before backtracking. While BFS guarantees completeness and finds shortest paths, DFS uses less memory and is useful for topological sorting and puzzles. The optimal algorithm depends on the specific problem and graph properties.

Uploaded by

Bhavya
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/ 2

BREADTH FIRST SEARCH V/S DEPTH FIRST SEARCH

Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used to
traverse and search through graphs and trees. These algorithms are commonly used in computer
science and are particularly useful in applications like pathfinding, network analysis, and puzzle
solving.

Breadth-First Search (BFS):

BFS explores a graph level by level, starting from the root (or source) and moving towards the leaves.
It uses a queue data structure to keep track of the nodes to be explored. The algorithm works as
follows:

1. Enqueue the root node.

2. Dequeue a node and process it.

3. Enqueue all its adjacent (or child) nodes.

4. Repeat steps 2-3 until the queue is empty.

BFS guarantees that it visits all nodes at the current depth level before moving on to nodes at the
next level.

Depth-First Search (DFS):

DFS explores a graph by going as deep as possible along one branch before backtracking. It uses a
stack (or recursion) to keep track of the nodes to be explored. The algorithm works as follows:

1. Start at the root node.

2. Process the current node.

3. Recursively visit all its adjacent (or child) nodes.

4. Repeat steps 2-3 until there are no more unvisited nodes in the current branch.

5. Backtrack to the previous node and repeat steps 2-4 until all nodes are visited.

DFS can be implemented using a stack or recursion. The order of processing nodes can vary
depending on whether you visit the left or right child first.
Differences:

1. Order of Visiting Nodes:

- BFS visits nodes level by level.

- DFS visits nodes depth by depth.

2. Data Structures:

- BFS uses a queue.

- DFS uses a stack (or recursion).

3. Completeness:

- BFS is complete for searching in finite graphs.

- DFS is not necessarily complete; it depends on the structure of the graph.

4. Memory Usage:

- BFS may use more memory as it needs to store all nodes at a given level in the queue.

- DFS may use less memory as it only needs to store the nodes along the current branch.

5. Applications:

- BFS is often used in finding the shortest path.

- DFS is often used in topological sorting and solving puzzles.

The choice between BFS and DFS depends on the specific problem and the characteristics of the
graph or tree being explored. Both algorithms have their strengths and weaknesses.

You might also like