Depth-First Search (DFS)

Explores as far as possible along each branch using a stack, not guaranteeing the shortest path in unweighted graphs.

Optimal:NoWeighted:No
Complexity
Best
O(V+E)
Average
O(V+E)
Worst
O(V+E)
Space
O(V)
When to use: Useful when you want any path quickly and memory usage matters more than optimality.Often uses less memory than BFS in wide open areasDoes not guarantee the shortest path
Pseudocode
stack ← [start]
parent[start] ← null
while stack not empty:
v ← pop(stack)
for each neighbor u of v:
if u not seen:
parent[u] ← v
push(stack, u)
if goal reached: reconstruct path
Waiting to start…