0
0
DSA Cprogramming~10 mins

Why Backtracking and What Greedy Cannot Solve in DSA C - Why It Works

Choose your learning style9 modes available
Concept Flow - Why Backtracking and What Greedy Cannot Solve
Start Problem
Try Greedy Approach
Check if Greedy Solution is Optimal?
NoUse Backtracking
Explore All Possibilities
Return Greedy Solution
Choose Best Solution
End
Start with greedy; if it fails to find the best answer, use backtracking to explore all options and pick the best.
Execution Sample
DSA C
int solve(int choices[], int n) {
  // Try greedy first
  int result = greedy(choices, n);
  if (!isOptimal(result)) {
    // Use backtracking
    result = backtrack(choices, n, 0, 0);
  }
  return result;
}
This code tries a greedy solution first; if it is not optimal, it uses backtracking to find the best answer.
Execution Table
StepOperationGreedy ResultBacktracking StateVisual State
1Apply greedy approachPartial solution (e.g. 7)Not startedChoices: [2,3,5], Greedy picks 5 then 2
2Check if greedy is optimal7 (not optimal)Not startedOptimal sum should be 8 (3+5) but greedy missed it
3Start backtracking7Index=0, CurrentSum=0Explore including 2
4Backtracking include 27Index=1, CurrentSum=2Explore including 3
5Backtracking include 37Index=2, CurrentSum=5Explore including 5
6Backtracking include 57Index=3, CurrentSum=10Sum exceeds target, backtrack
7Backtracking exclude 57Index=3, CurrentSum=5Backtrack to previous
8Backtracking exclude 37Index=2, CurrentSum=2Explore including 5
9Backtracking include 57Index=3, CurrentSum=7Backtrack
10Backtracking exclude 57Index=3, CurrentSum=2Backtrack
11Backtracking exclude 27Index=1, CurrentSum=0Explore including 3
12Backtracking include 37Index=2, CurrentSum=3Explore including 5
13Backtracking include 57Index=3, CurrentSum=8Found better sum 8
14Backtracking exclude 57Index=3, CurrentSum=3Backtrack
15Backtracking exclude 37Index=2, CurrentSum=0Explore including 5
16Backtracking include 57Index=3, CurrentSum=5Backtrack
17Backtracking exclude 57Index=3, CurrentSum=0Backtrack
18Backtracking complete7DoneBest sum found: 8
19Return best solution7DoneReturn 8 from backtracking
💡 Backtracking explores all subsets after greedy fails; best sum 8 found and returned.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 13Final
Greedy ResultN/A7777
Backtracking Index0033Done
Backtracking CurrentSum001088
Best Sum Found00088
Key Moments - 3 Insights
Why does greedy fail to find the best sum in this example?
Because greedy picks the largest immediate choice (5), then 2, missing the combination 3+5=8 which is better. See execution_table rows 1 and 2.
How does backtracking find the better solution?
Backtracking tries all combinations by including or excluding each choice step-by-step, exploring sums like 3+5=8. See execution_table rows 3 to 17.
Why is backtracking slower but more reliable than greedy?
Backtracking checks all possibilities to guarantee the best answer, while greedy makes quick local choices that may miss better global solutions. See variable_tracker for state changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the greedy result and why is it not optimal?
AGreedy result is 8, which is optimal
BGreedy result is 7, but optimal sum is 8
CGreedy result is 5, which is not optimal
DGreedy result is 10, which is too large
💡 Hint
Check execution_table row 2 for greedy result and optimality check
At which step does backtracking find a better sum than greedy?
AStep 13
BStep 9
CStep 6
DStep 18
💡 Hint
Look for when CurrentSum becomes 8 in execution_table
If the greedy approach was optimal, what would happen in the code flow?
ABacktracking would run partially
BBacktracking would still run to confirm
CBacktracking would be skipped and greedy result returned
DThe program would crash
💡 Hint
Refer to concept_flow where greedy optimal check leads to either backtracking or return
Concept Snapshot
Why Backtracking and What Greedy Cannot Solve

- Greedy picks local best choices quickly
- Sometimes greedy misses global best solution
- Backtracking tries all possibilities
- Backtracking guarantees best solution but slower
- Use greedy first; if fails, use backtracking
- Backtracking explores include/exclude choices stepwise
Full Transcript
This concept explains why greedy algorithms sometimes fail to find the best solution and how backtracking solves this by exploring all possibilities. The flow starts by trying a greedy approach. If the greedy solution is not optimal, backtracking is used to explore all combinations step-by-step. The execution table shows how greedy picks a partial solution quickly but misses a better sum. Backtracking then tries including and excluding each choice, eventually finding the best sum. Variable tracking shows how the current sum and index change during backtracking. Key moments clarify why greedy fails, how backtracking works, and why backtracking is slower but more reliable. The visual quiz tests understanding of these steps. The snapshot summarizes the main points: greedy is fast but can miss the best answer, backtracking is slower but finds the best solution by checking all options.