Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Unbounded Knapsack Problem in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Unbounded Knapsack Problem
Start
↓
Initialize DP array with zeros
↓
For each weight from 1 to W
↓
For each item
↓
If item weight <= current weight
↓
Update DP[current weight
↓
Repeat for all weights and items
↓
Return DP[W
↓
End
We fill a DP array from 0 to W, for each weight we try all items and update max value allowing unlimited item use.
Execution Sample
DSA Typescript
const W = 7;
const items = [{w:1, v:1}, {w:3, v:4}, {w:4, v:5}];
const dp = Array(W+1).fill(0);
for(let weight=1; weight<=W; weight++) {
  for(const item of items) {
    if(item.w <= weight) dp[weight] = Math.max(dp[weight], dp[weight - item.w] + item.v);
  }
}
console.log(dp[W]);
Calculates max value for knapsack capacity 7 with items reusable unlimited times.
Execution Table
StepOperationCurrent WeightItem (w,v)DP UpdateDP Array State
1Initialize DP array---[0,0,0,0,0,0,0,0]
2weight=1, item=(1,1)1(1,1)dp[1]=max(0, dp[0]+1)=1[0,1,0,0,0,0,0,0]
3weight=1, item=(3,4)1(3,4)No update (3>1)[0,1,0,0,0,0,0,0]
4weight=1, item=(4,5)1(4,5)No update (4>1)[0,1,0,0,0,0,0,0]
5weight=2, item=(1,1)2(1,1)dp[2]=max(0, dp[1]+1)=2[0,1,2,0,0,0,0,0]
6weight=2, item=(3,4)2(3,4)No update (3>2)[0,1,2,0,0,0,0,0]
7weight=2, item=(4,5)2(4,5)No update (4>2)[0,1,2,0,0,0,0,0]
8weight=3, item=(1,1)3(1,1)dp[3]=max(0, dp[2]+1)=3[0,1,2,3,0,0,0,0]
9weight=3, item=(3,4)3(3,4)dp[3]=max(3, dp[0]+4)=4[0,1,2,4,0,0,0,0]
10weight=3, item=(4,5)3(4,5)No update (4>3)[0,1,2,4,0,0,0,0]
11weight=4, item=(1,1)4(1,1)dp[4]=max(0, dp[3]+1)=5[0,1,2,4,5,0,0,0]
12weight=4, item=(3,4)4(3,4)dp[4]=max(5, dp[1]+4)=5[0,1,2,4,5,0,0,0]
13weight=4, item=(4,5)4(4,5)dp[4]=max(5, dp[0]+5)=5[0,1,2,4,5,0,0,0]
14weight=5, item=(1,1)5(1,1)dp[5]=max(0, dp[4]+1)=6[0,1,2,4,5,6,0,0]
15weight=5, item=(3,4)5(3,4)dp[5]=max(6, dp[2]+4)=6[0,1,2,4,5,6,0,0]
16weight=5, item=(4,5)5(4,5)dp[5]=max(6, dp[1]+5)=6[0,1,2,4,5,6,0,0]
17weight=6, item=(1,1)6(1,1)dp[6]=max(0, dp[5]+1)=7[0,1,2,4,5,6,7,0]
18weight=6, item=(3,4)6(3,4)dp[6]=max(7, dp[3]+4)=8[0,1,2,4,5,6,8,0]
19weight=6, item=(4,5)6(4,5)dp[6]=max(8, dp[2]+5)=8[0,1,2,4,5,6,8,0]
20weight=7, item=(1,1)7(1,1)dp[7]=max(0, dp[6]+1)=9[0,1,2,4,5,6,8,9]
21weight=7, item=(3,4)7(3,4)dp[7]=max(9, dp[4]+4)=9[0,1,2,4,5,6,8,9]
22weight=7, item=(4,5)7(4,5)dp[7]=max(9, dp[3]+5)=9[0,1,2,4,5,6,8,9]
23Return result---Max value = 9
💡 All weights from 1 to 7 processed with all items, dp[7] holds max value 9
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 9After Step 13After Step 17After Step 20Final
dp[0,0,0,0,0,0,0,0][0,1,0,0,0,0,0,0][0,1,2,0,0,0,0,0][0,1,2,4,0,0,0,0][0,1,2,4,5,0,0,0][0,1,2,4,5,6,8,0][0,1,2,4,5,6,8,9][0,1,2,4,5,6,8,9]
weightN/A1234677
Key Moments - 3 Insights
Why do we update dp[weight] multiple times for different items at the same weight?
Because each item can be used unlimited times, we check all items to find the best value for dp[weight]. See execution_table rows 8-10 where dp[3] updates twice.
Why do we use dp[weight - item.w] + item.v to update dp[weight]?
This means we add the current item's value to the best value for the remaining weight after taking this item. See execution_table row 9 where dp[3] = max(3, dp[0]+4).
Why does dp array start with zeros and length W+1?
dp[i] stores max value for weight i. Starting with zeros means no items taken yet. Length W+1 covers weights from 0 to W inclusive. See execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 9, what is the value of dp[3] after processing item (3,4)?
A3
B4
C0
D1
💡 Hint
Check the DP Update column at step 9 in execution_table
At which step does dp[5] first get updated to 6?
AStep 14
BStep 15
CStep 16
DStep 13
💡 Hint
Look for dp[5] updates in execution_table rows 14-16
If we remove the item (1,1), how would dp[7] change at the end?
AIt would stay 9
BIt would decrease
CIt would increase
DIt would be zero
💡 Hint
Without the smallest weight item, fewer combinations exist to fill weight 7, see variable_tracker dp values
Concept Snapshot
Unbounded Knapsack:
- dp array size W+1, dp[i] = max value for weight i
- For each weight 1..W, try all items
- If item weight <= current weight, dp[i] = max(dp[i], dp[i - item.w] + item.v)
- Items can be used unlimited times
- Result is dp[W]
Full Transcript
The Unbounded Knapsack problem finds the maximum value achievable with unlimited copies of given items within a weight limit W. We use a dp array where dp[i] stores the max value for weight i. Starting from 0 to W, for each weight, we try all items. If an item's weight fits, we update dp[i] by comparing current dp[i] and dp[i - item weight] plus item value. This allows repeated use of items. After filling dp, dp[W] holds the answer. The execution table shows step-by-step updates of dp array for W=7 and items (1,1), (3,4), (4,5). Key moments clarify why multiple updates per weight happen and why dp array starts with zeros. The visual quiz tests understanding of dp updates and effects of removing items.

Practice

(1/5)
1. What is the main difference between the 0/1 Knapsack Problem and the Unbounded Knapsack Problem?
easy
A. In Unbounded Knapsack, you can use unlimited copies of each item.
B. In Unbounded Knapsack, items cannot be repeated.
C. Unbounded Knapsack only works with fractional items.
D. Unbounded Knapsack has a fixed number of items to choose from.

Solution

  1. Step 1: Understand the 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 the problem constraints

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

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

    Unlimited item usage = In Unbounded Knapsack, you can use unlimited copies of each item. [OK]
Hint: Unbounded means unlimited items allowed [OK]
Common Mistakes:
  • Confusing unbounded with fractional knapsack
  • Thinking items cannot repeat
  • Assuming fixed item count
2. Which of the following TypeScript function signatures correctly represents the Unbounded Knapsack function?
easy
A. function unboundedKnapsack(W: number, wt: string[], val: number[], n: number): number
B. function unboundedKnapsack(W: string, wt: number[], val: number[], n: number): number
C. function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number
D. function unboundedKnapsack(W: number, wt: number[], val: number[], n: string): number

Solution

  1. Step 1: Check parameter types for correctness

    Weight capacity W should be a number, weights wt and values val arrays of numbers, and n the number of items as a number.
  2. Step 2: Identify the correct signature

    function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number matches all types correctly. Others have wrong types like string instead of number.
  3. Final Answer:

    function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number -> Option C
  4. Quick Check:

    Correct types for all parameters = function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number [OK]
Hint: Check parameter types carefully [OK]
Common Mistakes:
  • Using string instead of number for weights or capacity
  • Mixing array types
  • Incorrect return type
3. What will be the output of the following TypeScript code snippet for Unbounded Knapsack?
const val = [10, 40, 50];
const wt = [1, 3, 4];
const W = 6;

function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number {
  const dp = Array(W + 1).fill(0);
  for (let i = 1; i <= W; i++) {
    for (let j = 0; j < n; j++) {
      if (wt[j] <= i) {
        dp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j]);
      }
    }
  }
  return dp[W];
}

console.log(unboundedKnapsack(W, wt, val, val.length));
medium
A. 90
B. 100
C. 120
D. 80

Solution

  1. Step 1: Understand the DP array construction

    We build dp array from 0 to W=6. For each capacity, we check all items and update dp[i] with max value including unlimited items.
  2. Step 2: Calculate dp values step-by-step

    dp[1]=max(0, dp[0]+10)=10
    dp[2]=max(0, dp[1]+10)=20
    dp[3]=max(dp[3], dp[0]+40)=40, also dp[3]=max(40, dp[2]+10)=40
    dp[4]=max(dp[4], dp[1]+40)=50, also dp[4]=max(50, dp[0]+50)=50
    dp[5]=max(dp[5], dp[2]+40)=60, also dp[5]=max(60, dp[1]+50)=60
    dp[6]=max(dp[6], dp[3]+40)=80, also dp[6]=max(80, dp[2]+50)=90
  3. Final Answer:

    90 -> Option A
  4. Quick Check:

    Max value for W=6 = 90 [OK]
Hint: Build dp array incrementally for each capacity [OK]
Common Mistakes:
  • Not considering multiple copies of items
  • Incorrect dp array updates
  • Mixing 0/1 and unbounded logic
4. Identify the bug in this Unbounded Knapsack TypeScript code snippet:
function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number {
  const dp = Array(W + 1).fill(0);
  for (let j = 0; j < n; j++) {
    for (let i = 1; i <= W; i++) {
      if (wt[j] <= i) {
        dp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j]);
      }
    }
  }
  return dp[W];
}
medium
A. The condition wt[j] <= i should be wt[j] < i
B. Initialization of dp array is incorrect
C. Return statement should be dp[0] instead of dp[W]
D. Loops are reversed; inner loop should be over items, outer over capacity

Solution

  1. Step 1: Analyze loop order for unbounded knapsack

    For unbounded knapsack, outer loop should iterate over capacities (i), inner loop over items (j) to allow multiple uses of items.
  2. Step 2: Identify the bug in loop order

    Here, outer loop is over items and inner over capacity, which is incorrect and leads to wrong results.
  3. Final Answer:

    Loops are reversed; inner loop should be over items, outer over capacity -> Option D
  4. Quick Check:

    Correct loop order = capacity outer, items inner [OK]
Hint: Outer loop on capacity for unbounded knapsack [OK]
Common Mistakes:
  • Swapping loop order
  • Wrong dp initialization
  • Incorrect return index
5. Given items with values val = [5, 10, 15] and weights wt = [1, 3, 4], and a knapsack capacity W = 7, what is the maximum value achievable using the Unbounded Knapsack approach?
hard
A. 25
B. 30
C. 35
D. 40

Solution

  1. Step 1: Calculate dp array values for capacities 1 to 7

    dp[0]=0
    dp[1]=max(0, dp[0]+5)=5
    dp[2]=max(0, dp[1]+5)=10
    dp[3]=max(dp[3], dp[0]+10)=10, also dp[3]=max(10, dp[2]+5)=10
    dp[4]=max(dp[4], dp[1]+10)=15, also dp[4]=max(15, dp[0]+15)=15
    dp[5]=max(dp[5], dp[2]+10)=20, also dp[5]=max(20, dp[1]+15)=20
    dp[6]=max(dp[6], dp[3]+10)=20, also dp[6]=max(20, dp[2]+15)=25
    dp[7]=max(dp[7], dp[4]+10)=25, also dp[7]=max(25, dp[3]+15)=25
  2. Step 2: Find maximum value at capacity 7

    The maximum value achievable is 25, but check if better combinations exist:
    Try dp[7] = dp[3] + val[2] = 10 + 15 = 25
    Try dp[7] = dp[4] + val[1] = 15 + 10 = 25
    Try dp[7] = dp[6] + val[0] = 25 + 5 = 30 (better)
    Update dp[7] = 30
    Try dp[7] = dp[3] + val[2] = 10 + 15 = 25 (less than 30)
    So final dp[7] = 30
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Maximum value for W=7 = 30 [OK]
Hint: Check all item combinations for each capacity [OK]
Common Mistakes:
  • Ignoring multiple item usage
  • Stopping at first max value found
  • Not updating dp array correctly
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