In words
What it is, why it matters, and what it is like.
Why am I learning this?
This technique lets you solve problems where you must make a sequence of choices, each narrowing the possibilities for the next. It is how you find a valid Sudoku solution by filling one cell at a time, or how a program finds a route through a network. You try one possibility, see if it leads to a result, and if it hits a dead end, you step back and try another. This matters because many real-world tasks — from scheduling meetings to planning travel — involve navigating a huge number of possibilities. Without this method, you would either have to check every single combination (which takes too long) or guess randomly. With it, you can systematically explore the 'tree' of options, ignoring branches that clearly won't work, and reliably find a valid path.
The idea, in plain terms
Imagine you are trying to solve a puzzle where you place items one by one into slots. For example, placing chess queens on an 8x8 board so that no two attack each other.
1. Try: You pick the first empty spot and place a queen there.
2. Check: You look at the current board. Is it still possible to solve the puzzle from here? Or have you already made two queens attack each other? If you haven't hit a contradiction, you move to the next slot and repeat. This is the 'deep' part of depth-first search: you commit to a choice and follow it as far as you can.
3. Backtrack: Suppose you place a queen in slot 5, and later you realize that no matter where you put the remaining queens, this board configuration is impossible. You have hit a dead end. The key step is to 'undo' your decision at slot 5. You remove the queen from slot 5. Now you are back at slot 4, where you originally chose where to put that queen. You move that queen to a different spot and try again.
You do not need to remember every dead end you ever hit. You only need to keep track of the current path. When you 'return' from a deeper level of your process (because that path failed), the computer automatically wipes away the temporary notes you made for that specific step, restoring the previous notes as if that step never happened. This automatic cleanup is why backtracking is easy: you don't have to manually delete old data; just returning from the current function call does it for you.
You also 'prune' (cut off) branches early. If placing a queen in slot 2 creates a conflict with slot 1, you don't need to even look at slots 3 through 8 for that specific branch. You stop exploring that path immediately because you know it will fail. This saves enormous amounts of work.
An analogy
Think of finding your way through a vast underground cave system with no map. You have a backpack that holds a list of the tunnels you have currently entered.
You start at the entrance tunnel (Level 1). You enter Tunnel A (Level 2). Inside, you find two more tunnels: A1 and A2. You pick A1. Inside A1, there is only one dead end. You realize you are stuck. Instead of forgetting where you came from, you simply walk back out of A1 into Tunnel A. Your backpack now lists only 'Entrance -> A'. The fact that you were in A1 is gone from your memory because you left that level.
Now you are still in Tunnel A. You try the other option: A2. Inside A2, you find it leads to a large chamber (a solution). Success!
But suppose A2 also led to a dead end. You walk back out of A2. Your backpack again just says 'Entrance -> A'. Now all exits from A are tried. You walk back out of A to the Entrance. Your backpack is empty except for 'Entrance'. You now try Tunnel B.
This is depth-first search: you go as deep as possible down one branch, and when you hit a wall, you pop back up one level (forgetting the details of the lower level) to try the next option at that higher level. The 'stack' of tunnels you are currently in is managed by your physical position: you can only be in one tunnel at a time, and leaving it erases its presence from your immediate context.
Definition
Depth-first search with backtracking is a problem-solving method that explores possible choices by going as deep as possible down one path, and if that path fails, stepping back to the previous choice point and trying the next available option, using the natural undoing of function returns to manage the state of progress.
Where this sits
This concept relies heavily on recursion, which is a technique where a function calls itself to solve smaller versions of the same problem; each call remembers where it left off when it returns. It also connects to state management in programming, which is how code tracks variables that change over time, ensuring that when you 'step back' in your logic, the variables revert to their earlier values automatically.