Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
0_1_knapsack.cpp File Reference

Implementation of [0-1 Knapsack Problem] (https://en.wikipedia.org/wiki/Knapsack_problem) More...

#include <array>
#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for 0_1_knapsack.cpp:

Namespaces

namespace  dynamic_programming
 Dynamic Programming algorithms.
 
namespace  Knapsack
 Implementation of 0-1 Knapsack problem.
 

Functions

template<size_t n>
int dynamic_programming::knapsack::maxKnapsackValue (const int capacity, const std::array< int, n > &weight, const std::array< int, n > &value)
 Picking up all those items whose combined weight is below the given capacity and calculating the value of those picked items. Trying all possible combinations will yield the maximum knapsack value.
 
static void test ()
 Function to test the above algorithm.
 
int main ()
 Main function.
 

Detailed Description

Implementation of [0-1 Knapsack Problem] (https://en.wikipedia.org/wiki/Knapsack_problem)

Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property)

Algorithm

The idea is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.

Author
Anmol
Pardeep

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
126 {
127 // Testing
128 test();
129 return 0;
130}
static void test()
Function to test the above algorithm.
Definition 0_1_knapsack.cpp:96
Here is the call graph for this function:

◆ maxKnapsackValue()

template<size_t n>
int dynamic_programming::knapsack::maxKnapsackValue ( const int capacity,
const std::array< int, n > & weight,
const std::array< int, n > & value )

Picking up all those items whose combined weight is below the given capacity and calculating the value of those picked items. Trying all possible combinations will yield the maximum knapsack value.

Template Parameters
nsize of the weight and value array
Parameters
capacitycapacity of the carrying bag
weightarray representing the weight of items
valuearray representing the value of items
Returns
maximum value obtainable with a given capacity.
52 {
53 std::vector<std::vector<int> > maxValue(n + 1,
54 std::vector<int>(capacity + 1, 0));
55 // outer loop will select no of items allowed
56 // inner loop will select the capacity of the knapsack bag
57 int items = sizeof(weight) / sizeof(weight[0]);
58 for (size_t i = 0; i < items + 1; ++i) {
59 for (size_t j = 0; j < capacity + 1; ++j) {
60 if (i == 0 || j == 0) {
61 // if no of items is zero or capacity is zero, then maxValue
62 // will be zero
63 maxValue[i][j] = 0;
64 } else if (weight[i - 1] <= j) {
65 // if the ith item's weight(in the actual array it will be at i-1)
66 // is less than or equal to the allowed weight i.e. j then we
67 // can pick that item for our knapsack. maxValue will be the
68 // obtained either by picking the current item or by not picking
69 // current item
70
71 // picking the current item
72 int profit1 = value[i - 1] + maxValue[i - 1][j - weight[i - 1]];
73
74 // not picking the current item
75 int profit2 = maxValue[i - 1][j];
76
77 maxValue[i][j] = std::max(profit1, profit2);
78 } else {
79 // as the weight of the current item is greater than the allowed weight, so
80 // maxProfit will be profit obtained by excluding the current item.
81 maxValue[i][j] = maxValue[i - 1][j];
82 }
83 }
84 }
85
86 // returning maximum value
87 return maxValue[items][capacity];
88}
T max(T... args)
Here is the call graph for this function:

◆ test()

static void test ( )
static

Function to test the above algorithm.

Returns
void
96 {
97 // Test 1
98 const int n1 = 3; // number of items
99 std::array<int, n1> weight1 = {10, 20, 30}; // weight of each item
100 std::array<int, n1> value1 = {60, 100, 120}; // value of each item
101 const int capacity1 = 50; // capacity of carrying bag
103 capacity1, weight1, value1);
104 const int expected_max_value1 = 220;
105 assert(max_value1 == expected_max_value1);
106 std::cout << "Maximum Knapsack value with " << n1 << " items is "
107 << max_value1 << std::endl;
108
109 // Test 2
110 const int n2 = 4; // number of items
111 std::array<int, n2> weight2 = {24, 10, 10, 7}; // weight of each item
112 std::array<int, n2> value2 = {24, 18, 18, 10}; // value of each item
113 const int capacity2 = 25; // capacity of carrying bag
115 capacity2, weight2, value2);
116 const int expected_max_value2 = 36;
117 assert(max_value2 == expected_max_value2);
118 std::cout << "Maximum Knapsack value with " << n2 << " items is "
119 << max_value2 << std::endl;
120}
int maxKnapsackValue(const int capacity, const std::array< int, n > &weight, const std::array< int, n > &value)
Picking up all those items whose combined weight is below the given capacity and calculating the valu...
Definition 0_1_knapsack.cpp:51
T endl(T... args)
Here is the call graph for this function: