Include dependency graph for 0_1_knapsack.cpp:</div>
<divclass="dyncontent">
<divclass="center"><iframescrolling="no"frameborder="0"src="../../d6/da8/0__1__knapsack_8cpp__incl.svg"width="331"height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<trclass="memdesc:d7/daf/namespace_knapsack"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Implementation of 0-1 <aclass="el"href="../../d7/daf/namespace_knapsack.html"title="Implementation of 0-1 Knapsack problem.">Knapsack</a> problem. <br/></td></tr>
<trclass="memitem:a15edf30f336885e5b851f6b7199c6cd1"><tdclass="memTemplItemLeft"align="right"valign="top">int </td><tdclass="memTemplItemRight"valign="bottom"><aclass="el"href="../../db/d16/0__1__knapsack_8cpp.html#a15edf30f336885e5b851f6b7199c6cd1">dynamic_programming::knapsack::maxKnapsackValue</a> (const int capacity, const <aclass="elRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>< int, n >&weight, const <aclass="elRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>< int, n >&value)</td></tr>
<trclass="memdesc:a15edf30f336885e5b851f6b7199c6cd1"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Picking up all those items whose combined weight is below given capacity and calculating value of those picked items.Trying all possible combinations will yield the maximum knapsack value. <ahref="../../db/d16/0__1__knapsack_8cpp.html#a15edf30f336885e5b851f6b7199c6cd1">More...</a><br/></td></tr>
<trclass="memdesc:aa8dca7b867074164d5f45b0f3851269d"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Function to test above algorithm. <ahref="../../db/d16/0__1__knapsack_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><br/></td></tr>
<divclass="textblock"><p>Implementation of <ahref="https://en.wikipedia.org/wiki/Knapsack_problem">0-1 Knapsack Problem</a></p>
<p>Given weights and values of n items, put these items in a knapsack of capacity <code>W</code> to get the maximum total value in the knapsack. In other words, given two integer arrays <code>val[0..n-1]</code> and <code>wt[0..n-1]</code> 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 <code>val[]</code> 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)</p>
<h3><aclass="anchor"id="autotoc_md48"></a>
Algorithm</h3>
<p>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 <code>W</code>. From all such subsets, pick the maximum value subset.</p>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../db/d16/0__1__knapsack_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg"width="274"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<tdclass="paramtype">const <aclass="elRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>< int, n >& </td>
<tdclass="paramname"><em>weight</em>, </td>
</tr>
<tr>
<tdclass="paramkey"></td>
<td></td>
<tdclass="paramtype">const <aclass="elRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>< int, n >& </td>
<tdclass="paramname"><em>value</em> </td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><divclass="memdoc">
<p>Picking up all those items whose combined weight is below given capacity and calculating value of those picked items.Trying all possible combinations will yield the maximum knapsack value. </p>
<divclass="line"><aname="l00055"></a><spanclass="lineno"> 55</span> <spanclass="comment">// outer loop will select no of items allowed</span></div>
<divclass="line"><aname="l00056"></a><spanclass="lineno"> 56</span> <spanclass="comment">// inner loop will select capcity of knapsack bag</span></div>
<divclass="line"><aname="l00060"></a><spanclass="lineno"> 60</span> <spanclass="keywordflow">if</span> (i == 0 || j == 0) {</div>
<divclass="line"><aname="l00061"></a><spanclass="lineno"> 61</span> <spanclass="comment">// if no of items is zero or capacity is zero, then maxValue</span></div>
<divclass="line"><aname="l00062"></a><spanclass="lineno"> 62</span> <spanclass="comment">// will be zero</span></div>
<divclass="line"><aname="l00065"></a><spanclass="lineno"> 65</span> <spanclass="comment">// if the ith item's weight(in actual array it will be at i-1)</span></div>
<divclass="line"><aname="l00066"></a><spanclass="lineno"> 66</span> <spanclass="comment">// is less than or equal to the allowed weight i.e. j then we</span></div>
<divclass="line"><aname="l00067"></a><spanclass="lineno"> 67</span> <spanclass="comment">// can pick that item for our knapsack. maxValue will be the</span></div>
<divclass="line"><aname="l00068"></a><spanclass="lineno"> 68</span> <spanclass="comment">// obtained either by picking the current item or by not picking</span></div>
<divclass="line"><aname="l00069"></a><spanclass="lineno"> 69</span> <spanclass="comment">// current item</span></div>
<divclass="line"><aname="l00079"></a><spanclass="lineno"> 79</span> <spanclass="comment">// as weight of current item is greater than allowed weight, so</span></div>
<divclass="line"><aname="l00080"></a><spanclass="lineno"> 80</span> <spanclass="comment">// maxProfit will be profit obtained by excluding current item.</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../db/d16/0__1__knapsack_8cpp_a15edf30f336885e5b851f6b7199c6cd1_cgraph.svg"width="342"height="76"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="line"><aname="l00096"></a><spanclass="lineno"> 96</span> <spanclass="comment">// Test 1</span></div>
<divclass="line"><aname="l00097"></a><spanclass="lineno"> 97</span> <spanclass="keyword">const</span><spanclass="keywordtype">int</span> n1 = 3; <spanclass="comment">// number of items</span></div>
<divclass="line"><aname="l00098"></a><spanclass="lineno"> 98</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int, n1></a> weight1 = {10, 20, 30}; <spanclass="comment">// weight of each item</span></div>
<divclass="line"><aname="l00099"></a><spanclass="lineno"> 99</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int, n1></a> value1 = {60, 100, 120}; <spanclass="comment">// value of each item</span></div>
<divclass="line"><aname="l00100"></a><spanclass="lineno"> 100</span> <spanclass="keyword">const</span><spanclass="keywordtype">int</span> capacity1 = 50; <spanclass="comment">// capacity of carrying bag</span></div>
<divclass="line"><aname="l00105"></a><spanclass="lineno"> 105</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"Maximum Knapsack value with "</span><< n1 <<<spanclass="stringliteral">" items is "</span></div>
<divclass="line"><aname="l00108"></a><spanclass="lineno"> 108</span> <spanclass="comment">// Test 2</span></div>
<divclass="line"><aname="l00109"></a><spanclass="lineno"> 109</span> <spanclass="keyword">const</span><spanclass="keywordtype">int</span> n2 = 4; <spanclass="comment">// number of items</span></div>
<divclass="line"><aname="l00110"></a><spanclass="lineno"> 110</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int, n2></a> weight2 = {24, 10, 10, 7}; <spanclass="comment">// weight of each item</span></div>
<divclass="line"><aname="l00111"></a><spanclass="lineno"> 111</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/container/array.html">std::array<int, n2></a> value2 = {24, 18, 18, 10}; <spanclass="comment">// value of each item</span></div>
<divclass="line"><aname="l00112"></a><spanclass="lineno"> 112</span> <spanclass="keyword">const</span><spanclass="keywordtype">int</span> capacity2 = 25; <spanclass="comment">// capacity of carrying bag</span></div>
<divclass="line"><aname="l00117"></a><spanclass="lineno"> 117</span> <aclass="codeRef"target="_blank"href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a><<<spanclass="stringliteral">"Maximum Knapsack value with "</span><< n2 <<<spanclass="stringliteral">" items is "</span></div>
<divclass="center"><iframescrolling="no"frameborder="0"src="../../db/d16/0__1__knapsack_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg"width="175"height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
<divclass="ttc"id="a0__1__knapsack_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><divclass="ttname"><ahref="../../db/d16/0__1__knapsack_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><divclass="ttdeci">static void test()</div><divclass="ttdoc">Function to test above algorithm.</div><divclass="ttdef"><b>Definition:</b> 0_1_knapsack.cpp:95</div></div>
<divclass="ttc"id="a0__1__knapsack_8cpp_html_a15edf30f336885e5b851f6b7199c6cd1"><divclass="ttname"><ahref="../../db/d16/0__1__knapsack_8cpp.html#a15edf30f336885e5b851f6b7199c6cd1">dynamic_programming::knapsack::maxKnapsackValue</a></div><divclass="ttdeci">int maxKnapsackValue(const int capacity, const std::array< int, n >&weight, const std::array< int, n >&value)</div><divclass="ttdoc">Picking up all those items whose combined weight is below given capacity and calculating value of tho...</div><divclass="ttdef"><b>Definition:</b> 0_1_knapsack.cpp:51</div></div>
<liclass="footer">Generated by <ahref="http://www.doxygen.org/index.html"><imgclass="footer"src="../../doxygen.svg"width="104"height="31"alt="doxygen"/></a> 1.8.20 </li>
<area shape="rect" id="node1" title="Picking up all those items whose combined weight is below given capacity and calculating value of tho..." alt="" coords="5,29,215,71"/>
<gid="a_node2"><axlink:href="../../db/d16/0__1__knapsack_8cpp.html#aa8dca7b867074164d5f45b0f3851269d"target="_top"xlink:title="Function to test above algorithm.">
<divclass="textblock"><p>Implementation of <ahref="https://en.wikipedia.org/wiki/Kadane%27s_algorithm">Kadane Algorithm</a></p>
<p>Kadane algorithm is used to find the maximum sum subarray in an array and maximum sum subarray problem is the task of finding a contiguous subarray with the largest sum</p>
<h3><aclass="anchor"id="autotoc_md51"></a>
<h3><aclass="anchor"id="autotoc_md52"></a>
Algorithm</h3>
<p>The simple idea of the algorithm is to search for all positive contiguous segments of the array and keep track of maximum sum contiguous segment among all positive segments(curr_sum is used for this) Each time we get a positive sum we compare it with max_sum and update max_sum if it is greater than curr_sum</p>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.