---
title: "Differences between DFS and BFS"
subject: "Data Structure and Algorithm"
chapter: 10
key: "semester_4/data_structure_and_algorithm:dfs-vs-bfs-differences"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/dfs-vs-bfs-differences
site: BCT Engineering
---

# Differences between DFS and BFS

Data Structure and Algorithm · Chapter 10: Graphs

- Asked in: 2072 Magh, 5 marks
- Video: [Comparison and Complexity of DFS and BFS Algorithms](https://www.youtube.com/watch?v=jNlk61uTWb8) — TutorialsPoint
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/dfs-vs-bfs-differences
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-10

## Question

What are the differences between DFS and BFS?

## Short answer

DFS uses a stack (or recursion), going deep before backtracking; BFS uses a queue, visiting level by level. DFS memory is O(depth), BFS O(width). BFS finds shortest paths in unweighted graphs; DFS does not guarantee that.

## Answer

| Point | DFS (depth first) | BFS (breadth first) |
|---|---|---|
| Data structure | Stack (or recursion) | Queue |
| Order | Goes deep along a path, then backtracks | Visits level by level |
| Memory | O(depth) of the path in a tree | O(width), can be large (all vertices of a level) |
| Shortest path | Does not guarantee it | Finds shortest path in an unweighted graph |
| Time | O(V + E) with lists | O(V + E) with lists |
| Applications | Cycle detection, topological sort, connected components, maze solving | Shortest path, level-order, nearest neighbours, web crawling |
| Completeness | May go very deep (infinite path problem) | Finds a solution at the shallowest level |
| Implementation | Simple with recursion | Needs explicit queue |

Example: for the tree A-(B, C), B-(D, E), BFS gives A B C D E, DFS gives A B D E C.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which structures do DFS and BFS use?
   A. DFS queue, BFS stack
   B. DFS stack, BFS queue
   C. Both queue
   D. Both stack
   Answer: B. LIFO vs FIFO. (Key point: Structures)

2. Which finds shortest path in an unweighted graph?
   A. DFS
   B. Both
   C. BFS
   D. Neither
   Answer: C. Level order. (Key point: Shortest path)

3. Which can use large memory with a wide level?
   A. DFS
   B. Neither
   C. Both equally
   D. BFS
   Answer: D. Stores whole level. (Key point: Memory)

4. Which is a DFS application?
   A. Cycle detection and topological sort
   B. Shortest path
   C. Web crawling
   D. Nearest neighbours
   Answer: A. Typical DFS uses. (Key point: Application)

## Related in Data Structure and Algorithm

- [BFS and DFS with example](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abfs-dfs-explain-example) — Data Structure and Algorithm, chapter 10, asked 4×

## Related in other subjects

- [Searching: BFS vs DFS comparison](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Ablind-search-dfs-bfs-compare) — Artificial Intelligence, chapter 3, asked 5×
- [Informed vs blind search; DFS vs BFS](https://www.bctengineer.com/question.md?key=semester_6%2Fartificial_intelligence%3Ainformed-vs-blind-search-dfs-bfs) — Artificial Intelligence, chapter 3
- [DFS; stateless vs stateful services](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Adfs-stateless-stateful) — Distributed System, chapter 2
