Bird
Raised Fist0
DSA C++programming~10 mins

Unbounded Knapsack Problem in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Unbounded Knapsack Problem
Start
↓
Initialize DP array with 0
↓
For each capacity from 1 to W
↓
For each item
↓
If item weight <= capacity
↓
Update dp[capacity
↓
Repeat for all capacities
↓
Final answer = dp[W
↓
End
We fill a dp array where each index represents max value for that capacity. For each capacity, we try all items and update max value considering unlimited item usage.
Execution Sample
DSA C++
int W = 7;
int wt[] = {1, 3, 4};
int val[] = {1, 4, 5};
int n = 3;
int dp[W+1] = {0};
for (int i = 1; i <= W; i++) {
  for (int j = 0; j < n; j++) {
    if (wt[j] <= i) {
      dp[i] = max(dp[i], dp[i - wt[j]] + val[j]);
    }
  }
}
Calculates max value for capacity 7 with items of weights [1,3,4] and values [1,4,5] allowing unlimited item use.
Execution Table
StepCapacity (i)Item (j)Condition (wt[j] <= i)dp[i] beforedp[i - wt[j]] + val[j]dp[i] afterDP Array State
1101 <= 1: True0dp[0]+1=0+1=11[0,1,0,0,0,0,0,0]
2113 <= 1: False1-1[0,1,0,0,0,0,0,0]
3124 <= 1: False1-1[0,1,0,0,0,0,0,0]
4201 <= 2: True0dp[1]+1=1+1=22[0,1,2,0,0,0,0,0]
5213 <= 2: False2-2[0,1,2,0,0,0,0,0]
6224 <= 2: False2-2[0,1,2,0,0,0,0,0]
7301 <= 3: True0dp[2]+1=2+1=33[0,1,2,3,0,0,0,0]
8313 <= 3: True3dp[0]+4=0+4=44[0,1,2,4,0,0,0,0]
9324 <= 3: False4-4[0,1,2,4,0,0,0,0]
10401 <= 4: True0dp[3]+1=4+1=55[0,1,2,4,5,0,0,0]
11413 <= 4: True5dp[1]+4=1+4=55[0,1,2,4,5,0,0,0]
12424 <= 4: True5dp[0]+5=0+5=55[0,1,2,4,5,0,0,0]
13501 <= 5: True0dp[4]+1=5+1=66[0,1,2,4,5,6,0,0]
14513 <= 5: True6dp[2]+4=2+4=66[0,1,2,4,5,6,0,0]
15524 <= 5: True6dp[1]+5=1+5=66[0,1,2,4,5,6,0,0]
16601 <= 6: True0dp[5]+1=6+1=77[0,1,2,4,5,6,7,0]
17613 <= 6: True7dp[3]+4=4+4=88[0,1,2,4,5,6,8,0]
18624 <= 6: True8dp[2]+5=2+5=78[0,1,2,4,5,6,8,0]
19701 <= 7: True0dp[6]+1=8+1=99[0,1,2,4,5,6,8,9]
20713 <= 7: True9dp[4]+4=5+4=99[0,1,2,4,5,6,8,9]
21724 <= 7: True9dp[3]+5=4+5=99[0,1,2,4,5,6,8,9]
22------[Final dp array: 0,1,2,4,5,6,8,9]
💡 Completed all capacities from 1 to 7, dp array fully computed.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7After Step 10After Step 13After Step 16After Step 19Final
dp[0]000000000
dp[1]011111111
dp[2]002222222
dp[3]000344444
dp[4]000055555
dp[5]000006666
dp[6]000000788
dp[7]000000099
Key Moments - 3 Insights
Why do we update dp[i] multiple times for the same capacity i?
Because we try all items for capacity i, and each item can improve dp[i] if it fits. See execution_table rows 7-9 for capacity 3 where dp[3] updates from 0 to 4 after checking items.
Why do we use dp[i - wt[j]] + val[j] when updating dp[i]?
This represents the value if we include the current item j once more. We add its value to the best value for remaining capacity (i - wt[j]). See execution_table row 1 for capacity 1 and item 0.
How does this allow unlimited item usage?
Because dp[i - wt[j]] may already include item j multiple times, so adding val[j] again counts another usage. This is why we loop over capacities in increasing order and update dp[i] repeatedly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what is dp[3] after considering item 1?
A3
B4
C1
D0
💡 Hint
Check the 'dp[i] after' column at step 8 for capacity 3 and item 1.
At which capacity does dp first reach the value 6?
ACapacity 4
BCapacity 5
CCapacity 6
DCapacity 7
💡 Hint
Look at the DP Array State column in execution_table rows 10-15.
If we remove the item with weight 1 and value 1, how would dp[7] change?
AIt would stay 9
BIt would decrease
CIt would increase
DIt would become 0
💡 Hint
Without the smallest item, dp cannot build up values as easily. Check variable_tracker for dp[7] dependency on smaller weights.
Concept Snapshot
Unbounded Knapsack Problem:
- Find max value with unlimited items
- Use dp array of size W+1
- dp[i] = max value for capacity i
- For each capacity i, try all items j
- If wt[j] <= i, dp[i] = max(dp[i], dp[i - wt[j]] + val[j])
- Final answer is dp[W]
Full Transcript
The Unbounded Knapsack Problem finds the maximum value achievable with unlimited copies of given items within a capacity W. We use a dp array where dp[i] stores the max value for capacity i. For each capacity from 1 to W, we check each item. If the item's weight fits, we update dp[i] by considering adding that item once more to the best solution for capacity i - item's weight. This allows unlimited usage because dp[i - wt[j]] may already include that item multiple times. The final answer is dp[W]. The execution table shows step-by-step updates of dp array for capacity 1 to 7 with items of weights 1,3,4 and values 1,4,5. Key moments clarify why dp[i] updates multiple times and how unlimited usage works. The visual quiz tests understanding of dp updates and effects of removing items.

Practice

(1/5)
1. What is the main difference between the Unbounded Knapsack Problem and the 0/1 Knapsack Problem?
easy
A. In Unbounded Knapsack, items cannot be repeated.
B. In Unbounded Knapsack, you can use unlimited copies of each item.
C. In 0/1 Knapsack, items can be used unlimited times.
D. There is no difference; both problems are the same.

Solution

  1. Step 1: Understand item usage rules

    In 0/1 Knapsack, each item can be chosen at most once. In Unbounded Knapsack, items can be chosen unlimited times.
  2. Step 2: Compare problem definitions

    Unbounded Knapsack allows multiple copies of the same item, unlike 0/1 Knapsack.
  3. Final Answer:

    In Unbounded Knapsack, you can use unlimited copies of each item. -> Option B
  4. Quick Check:

    Unbounded Knapsack = unlimited item usage [OK]
Hint: Remember: Unbounded means unlimited items allowed [OK]
Common Mistakes:
  • Confusing 0/1 Knapsack with Unbounded Knapsack
  • Thinking items cannot repeat in Unbounded Knapsack
  • Assuming both problems have same constraints
2. Which of the following is the correct way to initialize the DP array for the Unbounded Knapsack problem in C++?
easy
A. int dp[W+1]; fill(dp, dp+W+1, 0);
B. int dp[W]; // uninitialized array
C. int dp[W+1]; fill(dp, dp+W+1, -1);
D. int dp[W+1] = {0}; // W is max weight

Solution

  1. Step 1: Understand DP array role

    DP array stores max value for weights from 0 to W. It should start with 0 for no items chosen.
  2. Step 2: Choose correct initialization

    Filling dp with 0 means no value at start, which is correct. int dp[W+1]; fill(dp, dp+W+1, 0); uses fill with 0 correctly.
  3. Final Answer:

    int dp[W+1]; fill(dp, dp+W+1, 0); -> Option A
  4. Quick Check:

    DP array initialized to 0 for all weights [OK]
Hint: Initialize DP with zeros for max value calculations [OK]
Common Mistakes:
  • Leaving dp uninitialized causing garbage values
  • Initializing dp with -1 which is incorrect here
  • Using wrong array size causing out-of-bound errors
3. What will be the output of the following C++ code snippet for Unbounded Knapsack with weights = {2, 3}, values = {10, 15}, and max weight W = 5?
int weights[] = {2, 3};
int values[] = {10, 15};
int W = 5;
int dp[6] = {0};
for (int i = 0; i <= W; i++) {
  for (int j = 0; j < 2; j++) {
    if (weights[j] <= i) {
      dp[i] = max(dp[i], dp[i - weights[j]] + values[j]);
    }
  }
}
std::cout << dp[W] << std::endl;
medium
A. 30
B. 35
C. 25
D. 40

Solution

  1. Step 1: Trace dp array updates for weights 0 to 5

    Calculate dp[i]: dp[0]=0 For i=1: dp[1]=0 (no items fit) For i=2: dp[2]=max(dp[2], dp[0]+10)=10 For i=3: dp[3]=max(dp[3], dp[0]+15)=15 For i=4: dp[4]=max(dp[4], dp[2]+10=20, dp[1]+15=15)=20 For i=5: dp[5]=max(dp[5], dp[3]+10=25, dp[2]+15=25) so dp[5]=25
  2. Step 2: Confirm final dp[5] value

    Maximum value for weight 5 is 25 by choosing items (3 + 2) or (2 + 3).
  3. Final Answer:

    25 -> Option C
  4. Quick Check:

    Max value at W=5 is 25 [OK]
Hint: Check dp updates for each weight carefully [OK]
Common Mistakes:
  • Adding values incorrectly for dp updates
  • Ignoring multiple item usage possibilities
  • Mixing up indices in dp array
4. Identify the bug in this Unbounded Knapsack C++ code snippet:
int dp[W+1];
for (int i = 0; i <= W; i++) {
  for (int j = 0; j < n; j++) {
    if (weights[j] <= i) {
      dp[i] = max(dp[i], dp[i - weights[j]] + values[j]);
    }
  }
}
medium
A. dp array is not initialized before use.
B. Loop indices are incorrect.
C. Weights and values arrays are not declared.
D. The max function is used incorrectly.

Solution

  1. Step 1: Check dp array initialization

    dp array is declared but not initialized, so it contains garbage values affecting max calculations.
  2. Step 2: Importance of initializing dp

    dp should be initialized to 0 for all indices before the loops to ensure correct max value updates.
  3. Final Answer:

    dp array is not initialized before use. -> Option A
  4. Quick Check:

    Uninitialized dp causes wrong results [OK]
Hint: Always initialize dp array before loops [OK]
Common Mistakes:
  • Forgetting to initialize dp array
  • Assuming dp defaults to zero
  • Ignoring garbage values in dp
5. Given items with weights = {1, 3, 4}, values = {15, 50, 60}, and max weight W = 7, what is the maximum value achievable using the Unbounded Knapsack approach?
hard
A. 105
B. 120
C. 150
D. 115

Solution

  1. Step 1: Calculate dp values from 0 to 7

    dp[0]=0 For i=1: dp[1]=max(0, dp[0]+15)=15 For i=2: dp[2]=max(0, dp[1]+15)=30 For i=3: dp[3]=max(0, dp[0]+50, dp[2]+15)=50 For i=4: dp[4]=max(0, dp[1]+50, dp[0]+60, dp[3]+15)=65 For i=5: dp[5]=max(0, dp[2]+50, dp[1]+60, dp[4]+15)=80 For i=6: dp[6]=max(0, dp[3]+50, dp[2]+60, dp[5]+15)=100 For i=7: dp[7]=max(0, dp[4]+50=115, dp[3]+60=110, dp[6]+15=115)=115
  2. Step 2: Confirm maximum value at W=7

    The maximum value achievable is 115 by combining items optimally.
  3. Final Answer:

    115 -> Option D
  4. Quick Check:

    Max value at W=7 is 115 [OK]
Hint: Build dp array stepwise for each weight [OK]
Common Mistakes:
  • Not considering all item combinations
  • Incorrect dp updates for each weight
  • Mixing up weights and values indices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard