This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hacktoberfest
- Loading branch information
Showing
6 changed files
with
77 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
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,41 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int max(int a, int b) { return (a > b) ? a : b; } | ||
|
||
int knapsack(int W, int wt[], int val[], int n) { | ||
if (n == 0 || W == 0) | ||
return 0; | ||
if (wt[n - 1] > W) | ||
return knapsack(W, wt, val, n - 1); | ||
else | ||
return max( | ||
val[n - 1] + knapsack(W - wt[n - 1], wt, val, n - 1), | ||
knapsack(W, wt, val, n - 1)); | ||
} | ||
|
||
int main() { | ||
int n; | ||
cout << "Enter the number of items: "; | ||
cin >> n; | ||
|
||
int values[n]; | ||
int weights[n]; | ||
|
||
cout << "Enter the values of the items:" << endl; | ||
for (int i = 0; i < n; i++) { | ||
cin >> values[i]; | ||
} | ||
|
||
cout << "Enter the weights of the items:" << endl; | ||
for (int i = 0; i < n; i++) { | ||
cin >> weights[i]; | ||
} | ||
|
||
int capacity; | ||
cout << "Enter the knapsack capacity: "; | ||
cin >> capacity; | ||
|
||
cout << "Maximum profit: " << knapsack(capacity, weights, values, n) << endl; | ||
return 0; | ||
} |
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,8 @@ | ||
# 0/1 Knapsack Problem | ||
|
||
Problem Statement: | ||
You are given a set of N items, each with a specific weight and profit. Additionally, you have a bag with a limited capacity, denoted as W. The objective is to determine how to select items to maximize the total profit while ensuring that their combined weight does not exceed the bag's capacity. | ||
|
||
I have attached the source code in C++ along with the sample output in this folder. | ||
|
||
--- |
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,7 @@ | ||
Enter the number of items: 3 | ||
Enter the values of the items: | ||
60 100 120 | ||
Enter the weights of the items: | ||
10 20 30 | ||
Enter the knapsack capacity: 50 | ||
Maximum profit: 220 |
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
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