forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0-1 knapsack using dynamic programming
- Loading branch information
1 parent
b386391
commit 2fc6c7a
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//0-1 Knapsack problem using dynamic programming | ||
#include<iostream.h> | ||
|
||
int max(int a, int b) { return (a > b)? a : b; } | ||
|
||
int knapSack(int W, int wt[], int val[], int n) | ||
{ | ||
int i, w; | ||
int K[n+1][W+1]; | ||
|
||
for (i = 0; i <= n; i++) | ||
{ | ||
for (w = 0; w <= W; w++) | ||
{ | ||
if (i==0 || w==0) | ||
K[i][w] = 0; | ||
else if (wt[i-1] <= w) | ||
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); | ||
else | ||
K[i][w] = K[i-1][w]; | ||
} | ||
} | ||
|
||
return K[n][W]; | ||
} | ||
|
||
void main() | ||
{ | ||
int val[] = {50, 110, 115}; | ||
int wt[] = {10, 20, 30}; | ||
int W = 50; | ||
int n = 1; | ||
int c=knapSack(W, wt, val, n); | ||
cout<<c; | ||
} |