Depth-first search from 0 with an explicit stack. Pop the top, and if it is new, visit it and push its unvisited neighbours. The stack makes the search dive: the vertex pushed last is explored first, before its siblings.
Depth-first search from vertex 0 with a stack.
Pop 0: new, so visit it as number 1. Its adjacency list: 1, 2. Push the unvisited ones, 1, 2, so that 1 is on top.
Vertex 0 is visited as number 1. 2 neighbours are pushed.
Pop 1: new, so visit it as number 2. Its adjacency list: 0, 3. Push the unvisited ones, 3, so that 3 is on top.
Vertex 1 is visited as number 2. 1 neighbours are pushed.
Pop 3: new, so visit it as number 3. Its adjacency list: 1, 2, 4. Push the unvisited ones, 2, 4, so that 2 is on top.
Vertex 3 is visited as number 3. 2 neighbours are pushed.
Pop 2: new, so visit it as number 4. Its adjacency list: 0, 3, 5. Push the unvisited ones, 5, so that 5 is on top.
Vertex 2 is visited as number 4. 1 neighbours are pushed.
Pop 5: new, so visit it as number 5. Its adjacency list: 2, 4. Push the unvisited ones, 4, so that 4 is on top.
Vertex 5 is visited as number 5. 1 neighbours are pushed.
Pop 4: new, so visit it as number 6. Its adjacency list: 3, 5. Nothing unvisited to push, so the search will climb back.
Vertex 4 is visited as number 6. 0 neighbours are pushed.
Pop 4, but it was already visited by another route. Discard it.
4 is popped and skipped, already visited.
Pop 2, but it was already visited by another route. Discard it.
2 is popped and skipped, already visited.
Visited in order 0, 1, 3, 2, 5, 4, numbered on the vertices. The highlighted edges form the DFS tree. Same O(V + E) as BFS, opposite shape: a stack dives, a queue spreads.
Depth-first search visited 0, 1, 3, 2, 5, 4.