Watch depth-first search go deep along each path, then backtrack. Worked example from A to E, cycle detection, recursive or iterative, on a graph you can edit.
About this algorithm
Depth-first search (DFS) explores a graph by following one path as far as it can go, then backing up to the most recent node that still has an unexplored edge. This page drives it with a stack: push the start, pop the newest node, push its unvisited neighbours, and repeat. It tells you whether one node can reach another, lists everything reachable from a start, and is the building block for cycle detection, topological sorting and finding connected components.
Use DFS when you need to visit everything or test whether a route exists at all: checking that a maze has an exit, finding every node connected to a start, detecting a cycle, or ordering tasks so each one comes after the tasks it depends on (topological order). Pick BFS instead when you want the route with the fewest edges, because DFS returns whichever route it reaches first. When edges carry costs and the total matters, use Dijkstra or Bellman-Ford.
Create a stack holding only the source and mark it visited. A stack hands back the newest node first, which sends the search deeper instead of wider. The recursive version uses the call stack in the same way.
Take the node off the top of the stack. If it is the target, stop.
For each neighbour not yet visited, mark it visited, remember the node you came from, and push it. This page pushes neighbours in reverse alphabetical order, so the alphabetically first one ends up on top and is explored next.
A node with no new neighbours pushes nothing, so the next pop comes from an older entry: the search has backed up to the last place with an unexplored branch. It ends when the target is popped or the stack is empty. Follow parent links from the target to read off the route.
The page loads six nodes, A to F, with source A and target E, joined by the edges A-B, A-D, B-C, B-D, B-E, C-E, D-E, D-F and E-F. Pop A and push D, then B, so B is on top. Pop B: A and D are already visited, so push E and then C. The stack is now [D, E, C] with C on top. Pop C: its neighbours B and E are both visited, a dead end. Pop E: it is the target. The visit order is A, B, C, E and the route is A → B → E. D was pushed but never explored, and F was never reached. Here DFS happens to find a two-edge route, but that is luck. Delete the B-E edge and run again: DFS returns A → B → C → E, three edges, while BFS on the same graph returns A → D → E, two edges.
The recursive form is shorter: visit a node, then call DFS on each unvisited neighbour. It relies on the program call stack, which can overflow on a long chain of nodes, so the explicit stack shown here is the safer choice for large inputs.
In a directed graph, keep a second set holding the nodes on the current path. Reaching a node that is still on the path means you have gone round a loop. Remove each node from that set when the search backs up out of it.
Time: each node is pushed at most once and each edge is checked once from each end, so the work grows with V + E. Space: the stack, the visited set and the parent links each hold at most one entry per node.