Algorithms · UGC NET: Design Techniques Extras
Backtracking
Overview
Backtracking is a systematic search technique for solving combinatorial problems by building a solution one component at a time and abandoning a partial solution as soon as it cannot lead to a valid complete solution.
It performs a depth-first search of an implicit state-space tree. The technique is central to Design Techniques Extras in UGC NET Computer Science and is asked through n-Queens, Subset Sum, m-Coloring, Hamiltonian Cycle, and the formal terminology of Horowitz, Sahni and Rajasekaran.
Before studying it, know trees, recursion, graph adjacency, DFS, and basic constraints such as “no two queens attack” or “adjacent vertices differ in colour.” The main skill is to identify the solution tuple, explicit constraints, implicit constraints, and bounding conditions.
Key concepts
- Backtracking is a controlled depth-first search over an implicit state-space tree; each level fixes one component of the solution tuple .
- A state-space tree has the empty partial solution at the root, decisions on edges, partial solutions at internal nodes, and candidate complete solutions at leaves.
- Explicit constraints restrict each component to its allowed set, e.g. for n-Queens.
- Implicit constraints are the actual rules that a complete tuple must satisfy, e.g. no two queens attack each other.
- A live node is a generated node whose children have not all been explored. An E-node or expanding node is a live node whose children are currently being generated.
A dead node cannot be expanded further or cannot lead to a solution. - A bounding function tests whether a node is promising. If not, the whole subtree is pruned; this is the difference between ordinary brute force and backtracking.
- Standard backtracking uses depth-first search; breadth-first search belongs to other state-space search techniques, not to textbook backtracking.
- n-Queens: use a 1-D array , where is the column of the queen in row . Two queens attack if or .
- Subset Sum: weights are positive; for sorted , and . Maintain current sum and remaining sum ; prune if or .
- m-Coloring: is the colour of vertex ; for every edge , .
- Hamiltonian Cycle: fix to avoid rotations; the sequence visits all vertices once, consecutive vertices are adjacent, and the last vertex is adjacent to the first.
- For a full binary tree in which every node has 0 or 2 children, is odd and the maximum height in edges is ; height 0 means a single node.
How it works
- Represent every partial solution as a tuple .
- Define the explicit constraints: the allowed values of each .
- Define the implicit constraints and, where useful, a bounding function.
- Start at the root and expand the current E-node in DFS order.
- For each child, check constraints. If feasible, make it the next E-node; if it is a complete solution, record it; otherwise continue.
- If no child is feasible, mark the node dead and backtrack to the parent.
- Stop when all nodes have been explored or pruned.
n-Queens procedure. Place queens row by row. At row , try columns to . A column is feasible only if no earlier queen has the same column or lies on either diagonal. If feasible, recurse to row ; if no column is feasible, backtrack.
Subset Sum procedure. Sort weights increasingly. At element , with current sum and remaining sum , try including only if . Exclude it only if . If , record the subset. If or , prune.
In the fixed-tuple-size tree, every level corresponds to one element and every edge is include/exclude, so the full binary tree has nodes including the root.
In the variable-tuple-size tree, a node stores the indices of selected elements, and children choose a later index; there are no explicit “exclude” branches.
m-Coloring procedure. Colour vertices one by one. At vertex , try colours to . Reject a colour if it equals the colour of an adjacent already-coloured vertex. If no colour works, backtrack.
Hamiltonian Cycle procedure. Start at vertex 1. At each step choose an unvisited adjacent vertex. When all vertices are used, check whether the last vertex is adjacent to vertex 1.
Drawing the diagram…
Formulas
- n-Queens constraints: ; for , and .
- Complete m-ary state-space tree for m-Coloring on vertices, including the root at depth 0: for . If a book excludes the root, subtract 1.
- Fixed-tuple Subset Sum binary tree with decision levels, including root: .
- Subset Sum pruning with next index , current sum , and remaining sum : prune if or ; solution if .
- Full binary tree with nodes, each node having 0 or 2 children, maximum height in edges: .
Worked examples
Example 1: 4-Queens count. For , valid tuples are and . Check : columns are all different; diagonals: , , etc. Both satisfy all constraints. Hence the number of distinct valid solutions is .
Example 2: Subset Sum trace. Weights , . Total remaining initially is . Include : . Include : . Include : , prune. Exclude , include : , prune.
Exclude : include : ; include gives , prune; exclude leaves , dead. Exclude : include gives , solution .
Backtrack to exclude : include : ; include gives , solution . Thus two solutions: and .
Example 3: m-Coloring tree size. For vertices and colours, a complete 3-ary state-space tree with four decision levels including the root has nodes. Excluding the root gives nodes.
Example 4: Full binary tree height. A full binary tree has nodes. Its maximum height in edges is . A skewed shape with internal nodes on one side achieves this; a balanced complete tree would have smaller height.
Example 5: Statement check. Statement I: “A live node may later become an E-node.” True. Statement II: “Explicit constraints define the allowed domain of each solution component.” True.
Statement III: “A dead node always means a complete solution has been found.” False; it only means the node cannot be expanded or cannot lead to a solution.
Statement IV: “Standard backtracking explores the state-space tree by BFS.” False; it uses DFS.
Comparison
| Concept | Meaning |
|---|---|
| Explicit constraint | Limits each to a domain, e.g. |
| Implicit constraint | Overall rule the solution tuple must satisfy, e.g. sum equals |
| Live node | Generated but not fully explored |
| E-node | Live node whose children are being generated |
| Dead node | Cannot be expanded or cannot lead to a solution |
| Bounding function | Early feasibility test that prunes a subtree |
| Point | Fixed-tuple state-space tree | Variable-tuple state-space tree |
|---|---|---|
| Tuple length | Fixed | Varies |
| Example | Subset Sum with | Subset Sum choosing next selected index |
| Tree shape | Full binary for Subset Sum | Nodes represent selected indices |
| Redundancy | Explicit exclude branches | Avoids explicit zero branches |
| Problem | Tuple | Main check |
|---|---|---|
| n-Queens | = column in row | No same column or diagonal |
| Subset Sum | Sum equals | |
| m-Coloring | = colour of vertex | Adjacent vertices differ |
| Hamiltonian Cycle | Sequence of vertices | All distinct; consecutive and closing edges exist |
Revision summary
- Backtracking = DFS + constraints + pruning.
- Explicit constraints bound each variable; implicit constraints define validity.
- Live node is generated; E-node is being expanded; dead node cannot continue.
- 4-Queens has exactly 2 distinct valid solutions; 8-Queens has 92 solutions, with 12 fundamental ones.
- m-Coloring complete m-ary tree nodes including root: .
- Subset Sum pruning: or .
- Fixed binary Subset Sum tree has nodes including root.
- Full binary tree maximum height is edges.
- Fix the first Hamiltonian vertex to avoid rotational duplicates.
- BFS is not standard backtracking; do not confuse the two.
Previous-year exam insights
- Direct concept questions ask which search strategy backtracking uses: depth-first search.
- Terminology questions ask the exact difference between explicit constraints and implicit constraints.
- Statement I/II/III questions test live node, E-node, dead node, and bounding function.
- n-Queens questions ask the algebraic attack condition or , and may ask the count for 4-Queens.
- Subset Sum questions require correct pruning with sorted positive weights and the remaining-sum condition.
- m-Coloring questions ask the size of the complete m-ary state-space tree; always check whether the root is included.
- Fixed-tuple versus variable-tuple state-space tree is a common comparison trap.
- Hamiltonian Cycle questions test start-vertex fixing, distinct vertices, and closing-edge condition.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Explain why backtracking uses DFS and how pruning differs from plain brute force.
- Write explicit and implicit constraints for n-Queens, Subset Sum, m-Coloring, and Hamiltonian Cycle.
- Trace Subset Sum on a small sorted array and state every prune using and .
- Compute the number of nodes in a complete m-ary state-space tree including the root.
- Solve and count 4-Queens solutions correctly.
- Distinguish live, E-, and dead nodes without mixing them.
- Compare fixed-tuple and variable-tuple state-space trees with an example.
- Identify the maximum height of a full binary tree with a given number of nodes.
- Check Hamiltonian cycle feasibility from an adjacency matrix or graph.
Attempt the sub-topic test first, then the subject test.