0% found this document useful (0 votes)
25 views3 pages

BFS DFS

The document explains two graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS explores nodes level by level using a queue, while DFS explores as deeply as possible along each branch using recursion or a stack. Each algorithm has distinct applications, with BFS being ideal for finding shortest paths in unweighted graphs and DFS suited for pathfinding, cycle detection, and topological sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

BFS DFS

The document explains two graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS explores nodes level by level using a queue, while DFS explores as deeply as possible along each branch using recursion or a stack. Each algorithm has distinct applications, with BFS being ideal for finding shortest paths in unweighted graphs and DFS suited for pathfinding, cycle detection, and topological sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Breadth-First Search (BFS) explores a graph level by level, systematically

visiting all the neighbors of a node before moving on to the next level. Here's a
step-by-step breakdown

1. Initialization:
 Start with an empty queue and a set (or array) to keep track of visited nodes.
 Choose a starting node (also called the root) and add it to the queue.
 Mark the starting node as visited.
2. Iteration:
 While the queue is not empty:
o Dequeue (remove) a node from the front of the queue.
o Process the dequeued node (e.g., print its value, perform an action).
o Find all unvisited neighbors of the dequeued node.
o Enqueue each unvisited neighbor into the queue.
o Mark each enqueued neighbor as visited.
3. Termination:
 The algorithm terminates when the queue is empty, meaning all reachable nodes have been
visited.

Example:
Let's say you have a graph with nodes A, B, C, D, and E, and connections like this: A-
B, A-C, B-D, C-E.
1. Initialization: Queue: [A], Visited: {A}
2. Dequeue A: Queue: [B, C], Visited: {A, B, C}
3. Dequeue B: Queue: [C, D], Visited: {A, B, C, D}
4. Dequeue C: Queue: [D, E], Visited: {A, B, C, D, E}
5. Dequeue D: Queue: [E], Visited: {A, B, C, D, E}
6. Dequeue E: Queue: [], Visited: {A, B, C, D, E} (Queue is now empty, BFS is complete)
Key Points:
 BFS explores the graph layer by layer.
 It uses a queue to maintain the order of exploration.
 It's often used to find the shortest path in an unweighted graph.
 It can be implemented iteratively (as shown above) or recursively.

Depth-First Search (DFS) is an algorithm for traversing or searching tree or


graph data structures. It explores as far as possible along each branch before
backtracking. Here's a step-by-step explanation:
1. Start: Choose a starting node (vertex) in the graph.
2. Mark as Visited: Mark the current node as visited.
3. Explore Neighbors: Examine the neighbors (adjacent nodes) of the current
node.

4. Recursive Step:
 If a neighbor is not visited, recursively apply DFS to that neighbor.
 If a neighbor has already been visited or is not accessible, backtrack to the
previous node.

5. Backtrack: If all neighbors of a node have been visited, go back to the node
from which the current node was reached and explore other unvisited
neighbors (if any) from that node.
6. Repeat: Continue steps 2-5 until all reachable nodes from the starting node
have been visited.
7. Termination: If the graph is disconnected, repeat steps 1-6 for other
unvisited nodes until all nodes have been visited.

In essence: DFS explores a graph by going deeper into each branch before
exploring its siblings. This process is often implemented using a stack data
structure. When a dead end is reached (all neighbors are visited), the
algorithm backtracks to the previous node to explore other paths
Both Depth-First Search (DFS) and Breadth-First Search (BFS) are graph
traversal algorithms with distinct applications. DFS explores as deeply as
possible along each branch before backtracking, while BFS explores all
neighbors at the current level before moving to the next. DFS is often
preferred for pathfinding, cycle detection, and topological sorting, while BFS
excels at finding shortest paths in unweighted graphs and network analysis.

DFS Applications:
 Pathfinding: DFS can be used to find a path between two nodes in a graph,
especially when the path is likely to be long.
 Cycle Detection: DFS can efficiently detect cycles in both directed and
undirected graphs.
 Topological Sorting: DFS is used to find a topological ordering of a directed
acyclic graph (DAG), which is useful for scheduling tasks with dependencies.
 Maze Solving: DFS can be adapted to find a path through a maze.
 Strongly Connected Components: DFS can identify strongly connected
components in a directed graph.
 Game Tree Search: DFS is used in game AI to explore game trees, especially
when the search space is deep.

BFS Applications:
 Shortest Path in Unweighted Graphs: BFS is the algorithm of choice for finding
the shortest path between two nodes in an unweighted graph.
 Network Broadcasting: BFS can be used to efficiently distribute information
across a network.
 Social Network Analysis: BFS can be used to explore connections in social
networks and identify communities.
 Web Crawling: BFS can be used to systematically explore and index websites.
 Bipartite Graph Checking: BFS can be used to determine if a graph is
bipartite.
 Finding Connected Components: BFS can find all nodes reachable from a
starting node.

You might also like