Depth-First Search visualization
Go as deep as possible along each branch before backtracking.
DFS dives into the first unvisited neighbour, then its first unvisited neighbour, and so on. When stuck, it backtracks — the call stack remembers where to return.
Discovery and finish times power cycle detection, topological sorting and strongly connected components.
Complexity
Time: best O(V + E), average O(V + E), worst O(V + E). Space: O(V). Difficulty: Beginner.
Pseudocode
procedure DFS(G, u):
visited.add(u); record discovery of u
for each neighbor v of u:
if v not visited:
DFS(G, v)
u is finished (backtrack)More graphs algorithms
- Breadth-First Search — Explore level by level with a queue — gives shortest paths in unweighted graphs.
- Dijkstra's Shortest Path — Greedily settle the closest unsettled vertex using a priority queue.
- Bellman–Ford — Relax every edge V−1 times; handles negative weights and detects negative cycles.
- Prim's MST — Grow one tree by always adding the cheapest edge leaving it.
- Kruskal's MST — Sort edges by weight and add each one unless it would create a cycle (union–find).
- Topological Sort (Kahn) — Order a DAG so every edge points forward, by repeatedly removing zero in-degree vertices.
- Floyd–Warshall — All-pairs shortest paths by allowing one more intermediate vertex k at a time.
- A* Pathfinding — Best-first search on a grid using cost-so-far g plus a heuristic h towards the goal.