Recursion vs Backtracking vs DP vs Greedy
Learn how to identify which algorithm paradigm to use - a practical comparison guide with decision frameworks, problem patterns, and real examples
Recursion vs Backtracking vs DP vs Greedy
Learn how to identify which algorithm paradigm to use - a practical comparison guide with decision frameworks, problem patterns, and real examples
Quick Comparison Table
Before diving deep, here's a quick reference to understand how these four paradigms differ. Bookmark this for quick revision!
| Aspect | Recursion | Backtracking | DP | Greedy |
|---|---|---|---|---|
| Core Idea | Try everything | Try valid paths only | Cache & reuse | Best choice now |
| Explores All Paths? | Yes | Valid only | No | No |
| Stores Results? | No | No | Yes | No |
| Undoes Choices? | Sometimes | Always | No | No |
| Guarantees Optimal? | Yes* | Yes* | Yes | Sometimes |
| Time Complexity | O(2^N), O(N!) | O(2^N) pruned | O(N), O(N²) | O(N), O(N log N) |
| Max Input Size | ≤ 15 | ≤ 20 | ≤ 10⁴ | ≤ 10⁶+ |
*If all paths are explored; finds all solutions, not necessarily "optimal"
One-Liner Definitions
Recursion
Divide problem into smaller subproblems, solve each the same way
Backtracking
Recursion + undo invalid choices (Choose → Explore → Undo)
Dynamic Programming
Recursion + cache results to avoid recomputation
Greedy
Make the locally optimal choice at each step, never look back
How They Relate
RECURSION
(base technique)
│
┌─────────────┼─────────────┐
│ │ │
▼ ▼ ▼
BACKTRACKING DP GREEDY
(+ pruning) (+ caching) (no exploration)
(+ undo)
"All valid "Optimal "One path,
solutions" answer" fast"
Key Insight
Backtracking and DP are both extensions of recursion.Backtracking adds pruning and undoing. DP adds memoization. Greedy abandons exploration entirely and just picks the best local choice.
Key Differences Explained
Let's understand the fundamental differences through the lens of the same problem: finding paths in a decision tree.
The Decision Tree Mental Model
Problem: Generate subsets of [1, 2]
[]
/ \
Include 1 Exclude 1
/ \
[1] []
/ \ / \
Inc 2 Exc 2 Inc 2 Exc 2
/ \ / \
[1,2] [1] [2] []
All leaf nodes = All subsets: [], [1], [2], [1,2]Difference 1: What Gets Explored
Recursion
Explores every single path in the tree, even invalid ones.
Backtracking
Explores only valid paths, prunes invalid branches early.
DP
Explores unique states only, caches repeated subproblems.
Greedy
Explores exactly one path, makes irrevocable choices.
Difference 2: Where the Answer Appears
| Paradigm | Answer Location | Example |
|---|---|---|
| Recursion | At leaf nodes | Base case returns value |
| Backtracking | Anywhere on path | Collect valid combinations mid-way |
| DP | At the root | Final answer bubbles up |
| Greedy | Accumulated along path | Build answer step by step |
Difference 3: Code Pattern
Recursion Pattern
Backtracking Pattern
DP Pattern (Top-Down)
Greedy Pattern
Difference 4: Same Problem, Different Questions
Problem: Climbing Stairs (1 or 2 steps at a time)
The Core Insight
The type of question determines the approach:
- "Find all solutions" → Backtracking
- "Find the count/optimal" + overlapping subproblems → DP
- "Find onesolution fast" + greedy property → Greedy
How to Identify the Right Approach
This is the most important section. Learn to recognize problem patterns and keywords that instantly reveal which paradigm to use.
Decision Flowchart
START: Read the problem
│
▼
┌─────────────────────────────┐
│ Does it ask for ALL │
│ combinations/permutations/ │──YES──▶ BACKTRACKING
│ subsets/paths? │
└─────────────────────────────┘
│ NO
▼
┌─────────────────────────────┐
│ Does it ask for COUNT or │
│ OPTIMAL (min/max) value? │──YES──┐
└─────────────────────────────┘ │
│ NO ▼
│ ┌─────────────────────────┐
│ │ Are there OVERLAPPING │
│ │ subproblems? │
│ └─────────────────────────┘
│ │ YES │ NO
│ ▼ ▼
│ DP Plain Recursion
▼
┌─────────────────────────────┐
│ Can you prove local optimal │
│ = global optimal? │──YES──▶ GREEDY
└─────────────────────────────┘
│ NO
▼
Consider DP or
Brute ForceKeyword Recognition Guide
🟠 Backtracking Keywords
Look for: "all", "every", "generate", "enumerate", constraint satisfaction
🔵 DP Keywords
Look for: optimization, counting, yes/no questions with overlapping choices
🟢 Greedy Keywords
Look for: intervals, scheduling, "obvious" local choice, large constraints (10⁵+)
Problem Type → Approach
| Problem Type | Approach | Example Problems |
|---|---|---|
| Generate all X | Backtracking | Subsets, Permutations, Combinations |
| Constraint satisfaction | Backtracking | N-Queens, Sudoku, Word Search |
| Count number of ways | DP | Climbing Stairs, Unique Paths, Coin Change II |
| Min/Max optimization | DP | Knapsack, LCS, Edit Distance |
| Yes/No reachability | DP | Word Break, Partition Equal Subset |
| Interval scheduling | Greedy | Meeting Rooms, Activity Selection |
| Fractional selection | Greedy | Fractional Knapsack, Gas Station |
| Graph shortest path | Greedy (Dijkstra) | Network Delay, Cheapest Flights |
Quick Mental Tests
Is it Backtracking?
- ✓Need to generate/list ALL solutions?
- ✓Constraints are small (N ≤ 15-20)?
- ✓Choices have validity rules?
Is it DP?
- ✓Solving same subproblem multiple times?
- ✓Optimal answer = f(optimal sub-answers)?
- ✓Asking for count or min/max?
Is it Greedy?
- ✓Local best choice leads to global best?
- ✓No need to reconsider past decisions?
- ✓Problem involves intervals or sorting?
DP vs Greedy?
- →Greedy: choices don't affect future options
- →DP: current choice affects future choices
- →When in doubt, try DP (always correct)
Interview Golden Rules
"ALL" in problem statement → Backtracking
Find all, generate all, list all, print all
"COUNT" or "OPTIMAL" + overlapping → DP
Number of ways, minimum cost, maximum profit, longest/shortest
Large constraints (N ≥ 10⁵) → Greedy or O(N) approach
DP won't work, must find greedy property or linear solution
Greedy feels "too easy" → Verify or use DP
Test with counter-examples before committing
Pro Tip: Start with Brute Force
In interviews, always start by explaining the brute force recursive solution first. Then optimize: add pruning (backtracking) or memoization (DP) based on the problem pattern. This shows your problem-solving process.
Constraints-Based Selection
The input size constraint is often the biggest hint about which approach will work. Learn to read constraints like a roadmap to the solution.
The Golden Rule
Constraints decide feasibility. If N = 10⁵, you can't use O(2^N). Time limit (usually 1-2 sec) means ~10⁸ operations max.
Constraint → Approach Mapping
| N (Input Size) | Max Complexity | Likely Approach | Why |
|---|---|---|---|
| ≤ 10 | O(N!) | Backtracking | 10! = 3.6M operations |
| ≤ 20 | O(2^N) | Backtracking / Bitmask | 2^20 = 1M operations |
| ≤ 100 | O(N³) | DP | 100³ = 1M operations |
| ≤ 1,000 | O(N²) | DP | 1000² = 1M operations |
| ≤ 10,000 | O(N²) careful | DP / Two Pointers | 10⁴² = 100M (borderline) |
| ≤ 10⁵ | O(N log N) | Greedy / Binary Search | O(N²) will TLE |
| ≤ 10⁶ | O(N) | Greedy / Sliding Window | Only linear works |
| ≤ 10⁹ | O(log N) | Math / Binary Search | Can't even iterate all |
Time Complexity Reference
Operations per Complexity
Safe Limits (1 sec)
Real Problem Examples
Generate all permutations of N distinct integers. N! grows fast, but 10! = 3.6M is fine.→ Backtracking
Two strings, each up to 1000 chars. O(N²) = 1M operations works perfectly.→ DP
Find minimum meeting rooms for N intervals. O(N²) would TLE. Sort + sweep works.→ Greedy (O(N log N))
Find max sum contiguous subarray. Must be O(N) - Kadane's algorithm.→ Greedy/DP (linear)
Interview Tip
Always check constraints first before coding. If N ≤ 20, don't waste time optimizing beyond O(2^N). If N ≥ 10⁵, don't even try O(N²) approaches.
Common Pitfalls & Traps
Even experienced developers fall into these traps. Learn to recognize them before they cost you an interview or hours of debugging.
Pitfall 1: The Greedy Trap
When Greedy Looks Right But Isn't
Coin Change Problem: Given coins [1, 3, 4], make amount 6.
Greedy (WRONG)
Pick largest first: 4 + 1 + 1 = 3 coins
DP (CORRECT)
Optimal: 3 + 3 = 2 coins
When Greedy Works vs Fails
| Problem | Greedy? | Why |
|---|---|---|
| Coin Change (arbitrary coins) | No | Larger coin might not fit optimally |
| Coin Change (1, 5, 10, 25) | Yes | Canonical coin system |
| 0/1 Knapsack | No | Can't take fractions |
| Fractional Knapsack | Yes | Can take best value/weight ratio |
| Activity Selection | Yes | Earliest end time works provably |
Pitfall 2: DP vs Backtracking Confusion
Choosing the Wrong One
Problem: "Find all subsets that sum to K"
Wrong: Using DP (it only gives count, not actual subsets)
Right: Backtracking (need to enumerate all solutions)
Problem: "Count subsets that sum to K"
Wrong: Backtracking (too slow for large N)
Right: DP (just need the count)
Quick Decision Rule
Need ALL solutions?
→ Backtracking
Need COUNT or OPTIMAL?
→ DP
Pitfall 3: Forgetting to Undo in Backtracking
Wrong (No Undo)
Correct (With Undo)
Pitfall 4: Incomplete Memoization Key
Common Bug: Missing State in Key
Pitfall 5: Missing Overlapping Subproblems
How to Check for Overlap
- Draw the recursion tree for a small input
- Look for repeated function calls with same arguments
- If you see them → DP will help
- If every call is unique → DP won't help (use backtracking or greedy)
Quick Reference: Avoid These Mistakes
Don't assume greedy works — test with counter-examples first
Use DP for count/optimal, Backtracking for enumerate all
Always undo choices in backtracking (Choose → Explore → Undo)
Include ALL changing state in memo key, not just the index
Check constraints first — they reveal the required complexity
Final Summary
Backtracking explores valid paths, DP caches overlapping work, Greedy trusts local choices, and constraints decide what's feasible.
When in doubt, start with brute force recursion, then optimize based on the problem pattern.