-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path00_BacktrackingOverview.cpp
More file actions
211 lines (166 loc) · 6.22 KB
/
Copy path00_BacktrackingOverview.cpp
File metadata and controls
211 lines (166 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
================================================================================
BACKTRACKING - OVERVIEW
================================================================================
Backtracking is a systematic way to explore all possible solutions by building
candidates incrementally and abandoning a candidate ("backtrack") as soon as
it determines that this candidate cannot lead to a valid solution.
Key Idea: Try → Explore → Undo (if needed)
Time Complexity: Usually exponential O(k^n) or O(n!)
Space Complexity: O(n) for recursion stack
================================================================================
CORE TEMPLATE
================================================================================
void backtrack(state, choices, result) {
// BASE CASE: Found valid solution
if (is_goal(state)) {
result.add(state);
return;
}
// RECURSIVE CASE: Try each choice
for (choice in choices) {
if (is_valid(choice)) {
// 1. MAKE choice
make_choice(state, choice);
// 2. EXPLORE further
backtrack(state, remaining_choices, result);
// 3. UNDO choice (backtrack)
undo_choice(state, choice);
}
}
}
================================================================================
PATTERN CLASSIFICATION
================================================================================
1. SUBSETS / POWER SET
────────────────────
Generate all possible subsets of a set.
Key: For each element, decide to INCLUDE or EXCLUDE.
Problems:
- Subsets (no duplicates)
- Subsets II (with duplicates)
- Letter Case Permutation
Template:
void subsets(nums, index, current, result) {
result.add(current); // Every state is valid
for (i = index; i < n; i++) {
current.add(nums[i]);
subsets(nums, i + 1, current, result);
current.remove_last();
}
}
2. PERMUTATIONS
─────────────
Generate all possible orderings.
Key: Each element must be used exactly once.
Problems:
- Permutations (no duplicates)
- Permutations II (with duplicates)
- Next Permutation
Template:
void permute(nums, used, current, result) {
if (current.size() == n) {
result.add(current);
return;
}
for (i = 0; i < n; i++) {
if (used[i]) continue;
used[i] = true;
current.add(nums[i]);
permute(nums, used, current, result);
current.remove_last();
used[i] = false;
}
}
3. COMBINATIONS
─────────────
Choose k elements from n (order doesn't matter).
Key: Similar to subsets but stop at size k.
Problems:
- Combinations (n choose k)
- Combination Sum (can reuse)
- Combination Sum II (no reuse, with duplicates)
- Combination Sum III (1-9, k numbers, sum = n)
Template:
void combine(n, k, start, current, result) {
if (current.size() == k) {
result.add(current);
return;
}
for (i = start; i <= n; i++) {
current.add(i);
combine(n, k, i + 1, current, result);
current.remove_last();
}
}
4. PARTITIONING
─────────────
Divide input into parts satisfying conditions.
Problems:
- Palindrome Partitioning
- Partition to K Equal Sum Subsets
- Restore IP Addresses
5. GRID/BOARD SEARCH
──────────────────
Explore paths on a 2D grid.
Problems:
- Word Search
- N-Queens
- Sudoku Solver
- Rat in a Maze
6. GENERATE VALID SEQUENCES
─────────────────────────
Build strings/sequences satisfying constraints.
Problems:
- Generate Parentheses
- Letter Combinations of Phone Number
- Expression Add Operators
================================================================================
KEY TECHNIQUES
================================================================================
1. PRUNING - Skip invalid branches early
if (sum > target) return; // Don't explore further
2. HANDLING DUPLICATES - Sort + skip same elements
if (i > start && nums[i] == nums[i-1]) continue;
3. EARLY TERMINATION - Stop when goal found
if (found) return;
4. STATE REPRESENTATION
- Use vector for path/current state
- Use boolean array for visited/used
- Use bitmask for small state space
5. AVOIDING REVISITS
- Pass start index for combinations
- Use visited array for permutations
- Mark/unmark cells for grid problems
================================================================================
FILE ORGANIZATION
================================================================================
01_SubsetsPatterns.cpp - Subsets, power set problems
02_Permutations.cpp - All ordering problems
03_Combinations.cpp - Combination sum variations
04_Partitioning.cpp - String/array partitioning
05_GridBoard.cpp - N-Queens, Sudoku, Word Search
06_GenerateSequences.cpp - Parentheses, expressions
================================================================================
*/
#include <bits/stdc++.h>
using namespace std;
// Quick Demo: Basic Backtracking Template
void backtrackDemo(vector<int>& nums, int start, vector<int>& current,
vector<vector<int>>& result) {
result.push_back(current);
for (int i = start; i < nums.size(); i++) {
current.push_back(nums[i]); // Make choice
backtrackDemo(nums, i + 1, current, result); // Explore
current.pop_back(); // Undo choice
}
}
int main() {
cout << "=== Backtracking Patterns ===" << endl;
vector<int> nums = {1, 2, 3};
vector<int> current;
vector<vector<int>> result;
backtrackDemo(nums, 0, current, result);
cout << "Subsets of [1,2,3]: " << result.size() << " subsets" << endl;
return 0;
}