0
0
DSA Cprogramming~10 mins

0 1 Knapsack Problem in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - 0 1 Knapsack Problem
Start with empty knapsack
For each item i
For each weight w from 0 to W
Check if item i fits (weight[i
Include item i: value[i
Update dp[i
Repeat for all items and weights
Final answer in dp[n
We fill a table dp where each cell dp[i][w] stores the best value using first i items with max weight w. For each item and weight, we decide to include or exclude the item to maximize value.
Execution Sample
DSA C
int dp[4][8] = {0};
int weights[] = {1, 3, 4};
int values[] = {1, 4, 5};
int W = 7;

// Fill dp table
This code fills a dp table for 3 items and max weight 7 to find max value.
Execution Table
StepOperationItem iWeight wInclude Item ValueExclude Item Valuedp[i][w] Updateddp Table State
1Initialize dp row 000-7--0[0,0,0,0,0,0,0,0]
2Process item 110-dp[0][0]=00[0,0,0,0,0,0,0,0]
3Process item 111value=1 + dp[0][0]=0dp[0][1]=01[0,1,0,0,0,0,0,0]
4Process item 112value=1 + dp[0][1]=0dp[0][2]=01[0,1,1,0,0,0,0,0]
5Process item 113value=1 + dp[0][2]=0dp[0][3]=01[0,1,1,1,0,0,0,0]
6Process item 114value=1 + dp[0][3]=0dp[0][4]=01[0,1,1,1,1,0,0,0]
7Process item 115value=1 + dp[0][4]=0dp[0][5]=01[0,1,1,1,1,1,0,0]
8Process item 116value=1 + dp[0][5]=0dp[0][6]=01[0,1,1,1,1,1,1,0]
9Process item 117value=1 + dp[0][6]=0dp[0][7]=01[0,1,1,1,1,1,1,1]
10Process item 220-dp[1][0]=00[0,1,1,1,1,1,1,1]
11Process item 221-dp[1][1]=11[0,1,1,1,1,1,1,1]
12Process item 222-dp[1][2]=11[0,1,1,1,1,1,1,1]
13Process item 223value=4 + dp[1][0]=0dp[1][3]=14[0,1,1,4,1,1,1,1]
14Process item 224value=4 + dp[1][1]=1dp[1][4]=15[0,1,1,4,5,1,1,1]
15Process item 225value=4 + dp[1][2]=1dp[1][5]=15[0,1,1,4,5,5,1,1]
16Process item 226value=4 + dp[1][3]=4dp[1][6]=18[0,1,1,4,5,5,8,1]
17Process item 227value=4 + dp[1][4]=5dp[1][7]=19[0,1,1,4,5,5,8,9]
18Process item 330-dp[2][0]=00[0,1,1,4,5,5,8,9]
19Process item 331-dp[2][1]=11[0,1,1,4,5,5,8,9]
20Process item 332-dp[2][2]=11[0,1,1,4,5,5,8,9]
21Process item 333-dp[2][3]=44[0,1,1,4,5,5,8,9]
22Process item 334value=5 + dp[2][0]=0dp[2][4]=55[0,1,1,4,5,5,8,9]
23Process item 335value=5 + dp[2][1]=1dp[2][5]=56[0,1,1,4,5,6,8,9]
24Process item 336value=5 + dp[2][2]=1dp[2][6]=86[0,1,1,4,5,6,8,9]
25Process item 337value=5 + dp[2][3]=4dp[2][7]=99[0,1,1,4,5,6,8,9]
26End-----Final dp table filled
💡 All items processed for all weights up to W=7, dp table complete
Variable Tracker
VariableStartAfter Step 9After Step 17After Step 25Final
dp row[0,0,0,0,0,0,0,0][0,1,1,1,1,1,1,1][0,1,1,4,5,5,8,9][0,1,1,4,5,6,8,9][0,1,1,4,5,6,8,9]
i (item index)01233
w (weight)07777
Key Moments - 3 Insights
Why do we add value[i] to dp[i-1][w-weight[i]] when including an item?
Because dp[i-1][w-weight[i]] represents the best value achievable with previous items and remaining weight after including current item. See rows 13,14 where inclusion sums value[i] + dp from smaller weight.
Why do we sometimes keep dp[i][w] same as dp[i-1][w]?
When the item doesn't fit or excluding it gives better value, we keep dp[i][w] same as dp[i-1][w]. For example, in rows 2,3, and 10,11 exclusion is chosen.
Why does the dp table have one extra row and column?
Row 0 and column 0 represent base cases: no items or zero capacity. This simplifies logic by providing initial values, as seen in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 16, what is the value of dp[2][6]?
A6
B5
C8
D9
💡 Hint
Check the 'dp[i][w] Updated' column at step 16 in execution_table
At which step does the dp table first update dp[i][w] to 4?
AStep 21
BStep 13
CStep 14
DStep 22
💡 Hint
Look for dp[i][w] Updated column with value 4 in execution_table
If the weight of item 2 changed from 3 to 2, how would dp[2][3] change at step 13?
AIt would become 5
BIt would remain 4
CIt would become 1
DIt would become 0
💡 Hint
Including item 2 at weight 3 would add value[2] + dp[i-1][w-weight[i]]; with smaller weight, dp index changes
Concept Snapshot
0 1 Knapsack Problem:
- Use dp[i][w] for max value with first i items and weight limit w
- For each item and weight, choose max(include, exclude)
- Include: value[i] + dp[i-1][w-weight[i]] if fits
- Exclude: dp[i-1][w]
- Final answer at dp[n][W]
Full Transcript
The 0 1 Knapsack problem uses a table dp where dp[i][w] stores the best value using first i items with max weight w. We fill this table row by row, checking for each item if it fits in the current weight. If it fits, we consider including it by adding its value plus the best value for remaining weight. Otherwise, we exclude it and keep previous best. The final answer is in dp[n][W]. The execution table shows step-by-step how dp is updated for each item and weight. Key moments clarify why we add values and why base cases exist. The visual quiz tests understanding of dp values at specific steps and effects of weight changes.