0% found this document useful (0 votes)
10 views3 pages

39 Daa4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

39 Daa4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Roll No: 39

Name: Pranjal Shailesh Teli

Assignment No: 04

Aim: Write a program to solve a 0-1 Knapsack problem using dynamic programming
or branch and bound strategy.

Code:

#include <bits/stdc++.h>

#include <chrono>

using namespace std;

using namespace std::chrono;

int knapsack(vector<int>& values, vector<int>& weights, int capacity, vector<int>&


solution) {

int n = values.size();

// Create a 2D table to store the results of subproblems.

// dp[i][w] represents the maximum value that can be obtained with i items and a knapsack
of capacity w.

vector<vector<int>> dp(n + 1, vector<int>(capacity + 1, 0));

// Fill in the table using dynamic programming.

for (int i = 0; i <= n; ++i) {

for (int w = 0; w <= capacity; ++w) {

if (i == 0 || w == 0) {

dp[i][w] = 0;

} else if (weights[i - 1] <= w) {

dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]);

} else {

dp[i][w] = dp[i - 1][w];

}}
// Trace the solution to get the included items.

int i = n, w = capacity;

while (i > 0 && w > 0) {

if (dp[i][w] != dp[i - 1][w]) {

solution[i - 1] = 1; // Item is included

w -= weights[i - 1];

i--;

// The value in the bottom-right corner of the table represents the maximum value that can
be obtained.

return dp[n][capacity];

int main() {

int n, capacity;

cout << "Enter the number of items: ";

cin >> n;

vector<int> values(n);

vector<int> weights(n);

cout << "Enter the values of items: ";

for (int i = 0; i < n; ++i) {

cin >> values[i];

cout << "Enter the weights of items: ";

for (int i = 0; i < n; ++i) {

cin >> weights[i];

}
cout << "Enter the knapsack capacity: ";

cin >> capacity;

vector<int> solution(n, 0);

// Measure the time taken by the algorithm

auto start_time = high_resolution_clock::now();

int max_value = knapsack(values, weights, capacity, solution);

auto end_time = high_resolution_clock::now();

auto duration = duration_cast<milliseconds>(end_time - start_time);

cout << "\nMaximum value: " << max_value << endl;

cout << "\nSolution vector (1 for included, 0 for not included): ";

for (int item : solution) {

cout << item << " ";

cout << endl;

cout << "\nTime taken: " << duration.count() << " milliseconds" << endl;

return 0;

Output:

Enter the number of items: 4

Enter the values of items: 10 20 30 40

Enter the weights of items: 43 65 54 23

Enter the knapsack capacity: 40

Maximum value: 40

Solution vector (1 for included, 0 for not included): 0 0 0 1

Time taken: 0 milliseconds

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy