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

BFS & DFS Theory

The document explains two main graph traversal methods: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS explores nodes level by level using a queue, while DFS explores as far as possible along a path using a stack or recursion. It also compares the two methods in terms of their features, applications, and time complexity.
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)
8 views

BFS & DFS Theory

The document explains two main graph traversal methods: Breadth-First Search (BFS) and Depth-First Search (DFS). BFS explores nodes level by level using a queue, while DFS explores as far as possible along a path using a stack or recursion. It also compares the two methods in terms of their features, applications, and time complexity.
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

✅ Graph Traversal: BFS and DFS

Graphs can be traversed (visited) using two main methods:


• BFS (Breadth-First Search)
• DFS (Depth-First Search)

1. BFS – Breadth-First Search

BFS explores the graph level by level, starting from the source node and visiting all its neighbors first.

How BFS Works:

1. Start from the source node.


2. Visit it and mark as visited.
3. Put it in a queue.
4. While the queue is not empty:
o Remove a node from the front.
o Visit all its unvisited neighbors.
o Mark them as visited and add them to the queue.

Pseudocode (Simple Steps):

BFS(graph, start):
create a queue Q
mark start as visited
enqueue start into Q

while Q is not empty:


current = dequeue Q
for each neighbor of current:
if neighbor not visited:
mark neighbor as visited
enqueue neighbor into Q

Data Structure Used:

• Queue
• Visited array or set

Time Complexity:

• O(V + E) for adjacency list


• O(V²) for adjacency matrix

Applications:

• Finding the shortest path in unweighted graphs


• Web crawlers
• Social network analysis
• GPS navigation
2. DFS – Depth-First Search

DFS explores the graph deeply, going as far as possible along one path before backtracking.

How DFS Works:

1. Start from the source node.


2. Visit it and mark as visited.
3. Recursively or using a stack, go to an unvisited neighbor.
4. Repeat until no more unvisited neighbors, then backtrack.

Pseudocode (Recursive Style):

DFS(graph, start):
mark start as visited
for each neighbor of start:
if neighbor not visited:
DFS(graph, neighbor)

Data Structure Used:

• Stack (explicit or recursion stack)


• Visited array or set Example

Time Complexity: Imagine this graph:

A
• O(V + E) for adjacency list / \
• O(V²) for adjacency matrix B C
| \
Applications: D E

• BFS(A): A → B → C → D → E
• Cycle detection
• DFS(A): A → B → D → C → E
• Topological sorting
• Solving puzzles (e.g., mazes, Sudoku)
• Pathfinding in AI

BFS vs DFS Comparison


Feature BFS DFS

Method Level by level Go deep, then backtrack

Uses Queue Stack or Recursion

Finds Shortest Path Yes (in unweighted graphs) No

Space Used More (stores full level) Less

Good for Shortest paths, AI, routing Puzzle solving, tree/graph analysis

You might also like