Comparison Mode
Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.
BFS vs DFSDijkstra vs Bellman-FordKruskal vs PrimMerge Sort vs Quick SortHeap vs Priority QueueHash Map vs Tree MapBFS vs DijkstraSliding Window vs Two PointersPrefix Sum vs Segment TreeGreedy vs Dynamic ProgrammingMemoization vs TabulationTarjan vs KosarajuSegment Tree vs Fenwick TreeArray vs Linked ListStack vs QueueQuick Sort vs Heap SortKMP vs Rabin-KarpUnion-Find vs DFSTrie vs Hash MapAVL Tree vs Red-Black Tree
BFSGraph Algos | DFSGraph Algos | |
|---|---|---|
| Use case | Shortest path by edge count, level-order processing, nearest target first. | Connectivity, cycle detection, topological order, exhaustive path enumeration, flood fill. |
| Requirements | A queue; graph or implicit state space with uniform move cost. | A stack or recursion; a visited set (plus colors for directed cycle detection). |
| Time complexity | O(V + E). | O(V + E). |
| Space complexity | O(V) for the queue, which can hold an entire level (wide for grids and trees). | O(V) stack in the worst case (a long path); usually small on bushy graphs. |
| Strengths | Finds the minimum number of steps on first visit; no recursion depth issues. | Simple recursive code; gives entry/exit times, low-links, back edges for free. |
| Weaknesses | Memory-heavy on wide graphs; cannot easily enumerate complete paths or detect back edges. | Does not find shortest paths; recursion depth can overflow on 10^5-node chains. |
| Example problems | Word ladder, rotting oranges, shortest path in binary matrix, binary tree level order. | Number of islands, clone graph, course schedule (cycle), critical connections. |
| Choose this when | Choose BFS when the question contains "minimum steps", "fewest moves" or "nearest", or when you must process a tree level by level. | Choose DFS when you need to know what is connected, whether a cycle exists, an ordering of a DAG, or every path from a source. |