ABCDEFGH
AlgoPlus//graph / dfs
Read the theory

Depth-First Search

Dive as deep as possible, then backtrack.

Stability
In-Place
Space Complexity
Avg Time
Source
Target
Legend
Current
Frontier
Visited
Path / MST
AI Tutor Workspace
In a nutshell
Depth-first search dives as far as it can down one path, then backs up to the last fork and tries the next branch. It relies on a stack — last in, first out — which in practice is usually the call stack of recursion. It doesn't find shortest paths, but it's the natural tool for visiting every corner, detecting cycles, and ordering dependencies.
Ready
Press play to begin the cinematic walkthrough.
A single ray probes as deep as possible before backtracking and exploring sibling branches.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Depth-First Search.
Code Simulator
1
def dfs(graph, node, visited=None):
2
    if visited is None: visited = set()
3
    visited.add(node)  # Enter / Visit
4
    for nb in graph[node]:
5
        if nb not in visited:  # Traverse
6
            dfs(graph, nb, visited)  # Backtrack
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.