Depth-First Search (DFS) Visualizer
Run depth-first search on your own graph and step through the stack and visit order.
Push A. Stack: [A]
Step-by-step working
- Push the start node on a stack.
- Pop a node; if unvisited, visit it and push its unvisited neighbours (reverse alphabetical so the smallest is explored first).
- Repeat until the stack is empty.
Mathematical Formula
Overview & Explanation
DFS explores as deep as possible along each branch before backtracking.
How It Works
- Edges are undirected; neighbours are explored alphabetically.
- A stack stores the frontier.
- Visited nodes are skipped when popped again.
Practical Applications
- Cycle detection and topological sort.
- Maze solving.
- Interview preparation.
Example 1: six-node graph from A
Tree-like with one cycle.
Inputs
Edges (e.g. A-B, A-C, B-D): A-B, A-C, B-D, B-E, C-F, E-F ยท Start node: A
Step 1
Push the start node on a stack.
Step 2
Pop a node; if unvisited, visit it and push its unvisited neighbours (reverse alphabetical so the smallest is explored first).
Step 3
Repeat until the stack is empty.
DFS visit order
Result
Example 2: square with diagonal from 1
Numeric node names.
Inputs
Edges (e.g. A-B, A-C, B-D): 1-2, 2-3, 3-4, 4-1, 1-3 ยท Start node: 1
Step 1
Push the start node on a stack.
Step 2
Pop a node; if unvisited, visit it and push its unvisited neighbours (reverse alphabetical so the smallest is explored first).
Step 3
Repeat until the stack is empty.
DFS visit order
Result
How it's calculated & sources
Iterative DFS: push start; while stack not empty: pop u; if unvisited, visit u and push unvisited neighbours (reverse order). Time O(V + E).
Frequently Asked Questions
DFS vs BFS?
Recursive vs iterative?
Disconnected graphs?
Related Calculators
Breadth First Search (BFS) Visualizer
Step-by-step BFS graph traversal visualizer tracking queue operations and visited node sets.
Dijkstra Algorithm Calculator & Visualizer
Step-by-step Dijkstra shortest path algorithm runner with distance tables, priority queue state, and edge relaxation logs.
Kruskal's Minimum Spanning Tree Visualizer
Step-by-step Kruskal's MST calculator showing sorted edge processing, disjoint-set (DSU) cycle checks, and accepted/rejected edges.
Bubble Sort Visualizer & Step Calculator
Interactive step-by-step Bubble Sort execution visualizer with comparisons, swaps, and time complexity breakdown.