Skip to content

Commit 1b7088c

Browse files
committed
docs: enhance backtracking_exploration pattern documentation
- Add comprehensive core concepts section in _header.md - Expand _templates.md with detailed explanations for each template - Include usage scenarios, complexity analysis, and LeetCode problems - Add template variants (duplicates handling, constraints, etc.)
1 parent f48275d commit 1b7088c

File tree

9 files changed

+1242
-1414
lines changed

9 files changed

+1242
-1414
lines changed
File renamed without changes.

docs/patterns/backtracking_exploration/templates.md

Lines changed: 608 additions & 0 deletions
Large diffs are not rendered by default.

docs/patterns/backtracking_exploration_templates.md

Lines changed: 0 additions & 1409 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Pattern Documentation Configuration
2+
# Controls the order of files when composing templates.md
3+
4+
# Header files (appear first)
5+
header_files = [
6+
"_header.md"
7+
]
8+
9+
# Problem files (appear in middle, ordered by LeetCode number or custom order)
10+
problem_files = [
11+
]
12+
13+
# Footer files (appear last, typically templates)
14+
footer_files = [
15+
"_templates.md"
16+
]
17+
18+
# Output configuration
19+
# Generate to: docs/patterns/backtracking_exploration/templates.md
20+
[output]
21+
subdirectory = "backtracking_exploration"
22+
filename = "templates.md"
23+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Backtracking Exploration Patterns: Complete Reference
2+
3+
> **API Kernel**: `BacktrackingExploration`
4+
> **Core Mechanism**: Systematically explore all candidate solutions by building them incrementally, abandoning paths that violate constraints (pruning), and undoing choices to try alternatives.
5+
6+
This document presents the **canonical backtracking template** and all its major variations. Each implementation follows consistent naming conventions and includes detailed algorithmic explanations.
7+
8+
---
9+
10+
## Core Concepts
11+
12+
### The Backtracking Process
13+
14+
Backtracking is a systematic search technique that builds solutions incrementally and abandons partial solutions that cannot lead to valid complete solutions.
15+
16+
```
17+
Backtracking State:
18+
┌─────────────────────────────────────────────────────────┐
19+
│ [choice₁] → [choice₂] → [choice₃] → ... → [choiceₙ] │
20+
│ │ │ │ │ │
21+
│ └───────────┴───────────┴──────────────┘ │
22+
│ Path (current partial solution) │
23+
│ │
24+
│ When constraint violated: │
25+
│ Backtrack: undo last choice, try next alternative │
26+
└─────────────────────────────────────────────────────────┘
27+
```
28+
29+
### Universal Template Structure
30+
31+
```python
32+
def backtracking_template(problem_state):
33+
"""
34+
Generic backtracking template.
35+
36+
Key components:
37+
1. Base Case: Check if current path is a complete solution
38+
2. Pruning: Abandon paths that violate constraints
39+
3. Choices: Generate all valid choices at current state
40+
4. Make Choice: Add choice to path, update state
41+
5. Recurse: Explore further with updated state
42+
6. Backtrack: Undo choice, restore state
43+
"""
44+
results = []
45+
46+
def backtrack(path, state):
47+
# BASE CASE: Check if solution is complete
48+
if is_complete(path, state):
49+
results.append(path[:]) # Copy path
50+
return
51+
52+
# PRUNING: Abandon invalid paths early
53+
if violates_constraints(path, state):
54+
return
55+
56+
# CHOICES: Generate all valid choices
57+
for choice in generate_choices(path, state):
58+
# MAKE CHOICE: Add to path, update state
59+
path.append(choice)
60+
update_state(state, choice)
61+
62+
# RECURSE: Explore further
63+
backtrack(path, state)
64+
65+
# BACKTRACK: Undo choice, restore state
66+
path.pop()
67+
restore_state(state, choice)
68+
69+
backtrack([], initial_state)
70+
return results
71+
```
72+
73+
### Backtracking Family Overview
74+
75+
| Sub-Pattern | Key Characteristic | Primary Use Case |
76+
|-------------|-------------------|------------------|
77+
| **Permutation** | All elements used, order matters | Generate all arrangements |
78+
| **Subset/Combination** | Select subset, order doesn't matter | Generate all subsets/combinations |
79+
| **Target Sum** | Constraint on sum/value | Find combinations meeting target |
80+
| **Grid Search** | 2D space exploration | Path finding, word search |
81+
| **Constraint Satisfaction** | Multiple constraints | N-Queens, Sudoku |
82+
83+
### When to Use Backtracking
84+
85+
- **Exhaustive Search**: Need to explore all possible solutions
86+
- **Constraint Satisfaction**: Multiple constraints must be satisfied simultaneously
87+
- **Decision Problem**: Need to find ANY valid solution (can optimize with early return)
88+
- **Enumeration**: Need to list ALL valid solutions
89+
- **Pruning Opportunity**: Can eliminate large portions of search space early
90+
91+
### Why It Works
92+
93+
Backtracking systematically explores the solution space by:
94+
1. **Building incrementally**: Each recursive call extends the current partial solution
95+
2. **Pruning early**: Invalid paths are abandoned immediately, saving computation
96+
3. **Exploring exhaustively**: All valid paths are explored through recursion
97+
4. **Undoing choices**: Backtracking allows exploring alternative paths from the same state
98+
99+
The key insight is that by maintaining state and undoing choices, we can explore all possibilities without storing all partial solutions explicitly.
100+
101+
---

0 commit comments

Comments
 (0)