Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d052748
Create Unbounded_knapsack.cpp
yeolesanskruti Oct 7, 2024
8b9f0c5
Update Unbounded_knapsack.cpp
yeolesanskruti Oct 8, 2024
a68f0e5
Update dynamic_programming/Unbounded_knapsack.cpp
yeolesanskruti Oct 8, 2024
f641dd3
Update dynamic_programming/Unbounded_knapsack.cpp
yeolesanskruti Oct 8, 2024
ff9f842
Update dynamic_programming/Unbounded_knapsack.cpp
yeolesanskruti Oct 8, 2024
43bc2ce
Update dynamic_programming/Unbounded_knapsack.cpp
yeolesanskruti Oct 8, 2024
5cc7245
Delete dynamic_programming/Unbounded_knapsack.cpp
yeolesanskruti Oct 8, 2024
6a8ca41
Create Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 8, 2024
e786129
Update Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 8, 2024
47e2d1d
Merge branch 'TheAlgorithms:master' into master
yeolesanskruti Oct 8, 2024
d9c63ed
Update Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 8, 2024
541ea54
docs: add docs for main
realstealthninja Oct 9, 2024
4d4c49c
Update dynamic_programming/Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 9, 2024
17f398f
Update dynamic_programming/Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 9, 2024
d7d96d7
Merge branch 'master' into master
yeolesanskruti Oct 9, 2024
122b453
Update Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 10, 2024
59ef9d1
Merge branch 'master' into master
yeolesanskruti Oct 10, 2024
ddf1857
Merge branch 'master' into master
yeolesanskruti Oct 11, 2024
e99d523
Update Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 11, 2024
4f17fb8
Merge branch 'master' into master
yeolesanskruti Oct 13, 2024
0cf4734
Update Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 13, 2024
6faf04d
Update dynamic_programming/Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 14, 2024
675bc2b
Merge branch 'master' into master
yeolesanskruti Oct 14, 2024
783b156
Update Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 15, 2024
20195f1
Update Unbounded_0_1_Knapsack.cpp
yeolesanskruti Oct 15, 2024
1e3cbd5
Merge branch 'master' into master
yeolesanskruti Oct 19, 2024
86ef381
Merge branch 'master' into master
realstealthninja Oct 24, 2024
b01227f
Merge branch 'master' into master
realstealthninja Oct 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions dynamic_programming/Unbounded_knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
The Unbounded Knapsack problem allows you to take unlimited quantities of each item. The goal is to maximize the total value
without exceeding the given knapsack capacity. Unlike the 0/1 knapsack, where each item can be taken only once, in this
variation, you can pick any item as many times as needed, as long as the total weight stays within the knapsack's capacity.
The problem is commonly solved using dynamic programming.

TASK - Given a set of N items, each with a weight and a value, represented by the array wt and val respectively. Also, a knapsack with weight limit W.
The task is to fill the knapsack in such a way that we can get the maximum profit. Return the maximum profit.
Note: Each item can be taken any number of times.

Test Cases -
Input:
N = 4, W = 8, val[] = {6, 1, 7, 7}, wt[] = {1, 3, 4, 5}
Output:
48
*/

// Code
#include <bits/stdc++.h>
using namespace std;

int KnapSackFilling(int i,int W, vector<int> val,vector<int> wt, vector<vector<int>> &dp){
if(i==0){
if(wt[0]<=W){
return (W/wt[0])*val[0];
}else{
return 0;
}
}
if(dp[i][W]!=-1)return dp[i][W];
int nottake=KnapSackFilling(i-1,W,val,wt,dp);
int take=0;
if(W>=wt[i]){
take=val[i]+KnapSackFilling(i,W-wt[i],val,wt,dp);
}
return dp[i][W]=max(take,nottake);
}
int unboundedKnapsack(int N, int W, vector<int> val, vector<int> wt)
{
vector<vector<int>> dp(N,vector<int>(W+1,-1));
return KnapSackFilling(N-1,W,val,wt,dp);
}
int main() {
int N;
N=4;
vector<int> wt= {1, 3, 4, 5};
vector<int> val= {6, 1, 7, 7};
int W = 8;

cout << "The Maximum value of items is " << unboundedKnapsack(N, W, val, wt) << endl;

return 0;
}

Loading