mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-21 20:31:43 +08:00
350 lines
25 KiB
HTML
350 lines
25 KiB
HTML
<!-- HTML header for doxygen 1.12.0-->
|
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
||
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
||
<meta name="generator" content="Doxygen 1.14.0"/>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||
<title>TheAlgorithms/C++: dynamic_programming/0_1_knapsack.cpp File Reference</title>
|
||
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
|
||
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
||
<script type="text/javascript" src="../../jquery.js"></script>
|
||
<script type="text/javascript" src="../../dynsections.js"></script>
|
||
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
|
||
<script type="text/javascript">
|
||
DoxygenAwesomeDarkModeToggle.init()
|
||
</script>
|
||
<script type="text/javascript" src="../../clipboard.js"></script>
|
||
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
||
<script type="text/javascript" src="../../navtreedata.js"></script>
|
||
<script type="text/javascript" src="../../navtree.js"></script>
|
||
<script type="text/javascript" src="../../cookie.js"></script>
|
||
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
||
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
||
<script type="text/javascript" src="../../search/search.js"></script>
|
||
<script type="text/javascript">
|
||
window.MathJax = {
|
||
options: {
|
||
ignoreHtmlClass: 'tex2jax_ignore',
|
||
processHtmlClass: 'tex2jax_process'
|
||
},
|
||
loader: {
|
||
load: ['[tex]/ams']
|
||
},
|
||
tex: {
|
||
macros: {},
|
||
packages: ['base','configmacros','ams']
|
||
}
|
||
};
|
||
</script>
|
||
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
|
||
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
||
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
|
||
</head>
|
||
<body>
|
||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||
<div id="titlearea">
|
||
<table cellspacing="0" cellpadding="0">
|
||
<tbody>
|
||
<tr id="projectrow">
|
||
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
|
||
<td id="projectalign">
|
||
<div id="projectname">TheAlgorithms/C++<span id="projectnumber"> 1.0.0</span>
|
||
</div>
|
||
<div id="projectbrief">All the algorithms implemented in C++</div>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<!-- end header part -->
|
||
<!-- Generated by Doxygen 1.14.0 -->
|
||
<script type="text/javascript">
|
||
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
||
</script>
|
||
<script type="text/javascript">
|
||
$(function() { codefold.init(); });
|
||
</script>
|
||
<script type="text/javascript" src="../../menudata.js"></script>
|
||
<script type="text/javascript" src="../../menu.js"></script>
|
||
<script type="text/javascript">
|
||
$(function() {
|
||
initMenu('../../',true,false,'search.php','Search',true);
|
||
$(function() { init_search(); });
|
||
});
|
||
</script>
|
||
<div id="main-nav"></div>
|
||
</div><!-- top -->
|
||
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
||
<div id="nav-tree">
|
||
<div id="nav-tree-contents">
|
||
<div id="nav-sync" class="sync"></div>
|
||
</div>
|
||
</div>
|
||
<div id="splitbar" style="-moz-user-select:none;"
|
||
class="ui-resizable-handle">
|
||
</div>
|
||
</div>
|
||
<script type="text/javascript">
|
||
$(function(){initNavTree('db/d16/0__1__knapsack_8cpp.html','../../',''); });
|
||
</script>
|
||
<div id="container">
|
||
<div id="doc-content">
|
||
<!-- window showing the filter options -->
|
||
<div id="MSearchSelectWindow"
|
||
onmouseover="return searchBox.OnSearchSelectShow()"
|
||
onmouseout="return searchBox.OnSearchSelectHide()"
|
||
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
||
</div>
|
||
|
||
<!-- iframe showing the search results (closed by default) -->
|
||
<div id="MSearchResultsWindow">
|
||
<div id="MSearchResults">
|
||
<div class="SRPage">
|
||
<div id="SRIndex">
|
||
<div id="SRResults"></div>
|
||
<div class="SRStatus" id="Loading">Loading...</div>
|
||
<div class="SRStatus" id="Searching">Searching...</div>
|
||
<div class="SRStatus" id="NoMatches">No Matches</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="header">
|
||
<div class="headertitle"><div class="title">0_1_knapsack.cpp File Reference</div></div>
|
||
</div><!--header-->
|
||
<div class="contents">
|
||
|
||
<p>Implementation of [0-1 <a class="el" href="../../d7/daf/namespace_knapsack.html" title="Implementation of 0-1 Knapsack problem.">Knapsack</a> Problem] (<a href="https://en.wikipedia.org/wiki/Knapsack_problem">https://en.wikipedia.org/wiki/Knapsack_problem</a>)
|
||
<a href="#details">More...</a></p>
|
||
<div class="textblock"><code>#include <array></code><br />
|
||
<code>#include <cassert></code><br />
|
||
<code>#include <iostream></code><br />
|
||
<code>#include <vector></code><br />
|
||
</div><div class="textblock"><div class="dynheader">
|
||
Include dependency graph for 0_1_knapsack.cpp:</div>
|
||
<div class="dyncontent">
|
||
<div class="center"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../d6/da8/0__1__knapsack_8cpp__incl.svg" width="326" height="126"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
||
</div>
|
||
</div>
|
||
<p><a href="../../db/d16/0__1__knapsack_8cpp_source.html">Go to the source code of this file.</a></p>
|
||
<table class="memberdecls">
|
||
<tr class="heading"><td colspan="2"><h2 id="header-namespaces" class="groupheader"><a id="namespaces" name="namespaces"></a>
|
||
Namespaces</h2></td></tr>
|
||
<tr class="memitem:dynamic_5Fprogramming" id="r_dynamic_5Fprogramming"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d24/namespacedynamic__programming.html">dynamic_programming</a></td></tr>
|
||
<tr class="memdesc:dd/d24/namespacedynamic__programming"><td class="mdescLeft"> </td><td class="mdescRight">Dynamic Programming algorithms. <br /></td></tr>
|
||
<tr class="memitem:Knapsack" id="r_Knapsack"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/daf/namespace_knapsack.html">Knapsack</a></td></tr>
|
||
<tr class="memdesc:d7/daf/namespace_knapsack"><td class="mdescLeft"> </td><td class="mdescRight">Implementation of 0-1 <a class="el" href="../../d7/daf/namespace_knapsack.html" title="Implementation of 0-1 Knapsack problem.">Knapsack</a> problem. <br /></td></tr>
|
||
</table><table class="memberdecls">
|
||
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
|
||
Functions</h2></td></tr>
|
||
<tr class="memitem:a15edf30f336885e5b851f6b7199c6cd1" id="r_a15edf30f336885e5b851f6b7199c6cd1"><td class="memTemplParams" colspan="2">template<size_t n> </td></tr>
|
||
<tr class="memitem:a15edf30f336885e5b851f6b7199c6cd1 template"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="#a15edf30f336885e5b851f6b7199c6cd1">dynamic_programming::knapsack::maxKnapsackValue</a> (const int capacity, const std::array< int, n > &weight, const std::array< int, n > &value)</td></tr>
|
||
<tr class="memdesc:a15edf30f336885e5b851f6b7199c6cd1"><td class="mdescLeft"> </td><td class="mdescRight">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. <br /></td></tr>
|
||
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d" id="r_aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void </td><td class="memItemRight" valign="bottom"><a class="el" href="#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
|
||
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft"> </td><td class="mdescRight">Function to test the above algorithm. <br /></td></tr>
|
||
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
|
||
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <br /></td></tr>
|
||
</table>
|
||
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
|
||
<div class="textblock"><p>Implementation of [0-1 <a class="el" href="../../d7/daf/namespace_knapsack.html" title="Implementation of 0-1 Knapsack problem.">Knapsack</a> Problem] (<a href="https://en.wikipedia.org/wiki/Knapsack_problem">https://en.wikipedia.org/wiki/Knapsack_problem</a>) </p>
|
||
<p>Given weights and values of n items, put these items in a knapsack of capacity <span class="tt">W</span> to get the maximum total value in the knapsack. In other words, given two integer arrays <span class="tt">val[0..n-1]</span> and <span class="tt">wt[0..n-1]</span> 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 <span class="tt">val[]</span> 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 class="doxsection"><a class="anchor" id="autotoc_md69"></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 <span class="tt">W</span>. From all such subsets, pick the maximum value subset.</p>
|
||
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Anmol3299" target="_blank">Anmol</a> </dd>
|
||
<dd>
|
||
<a href="https://github.com/Pardeep009" target="_blank">Pardeep</a> </dd></dl>
|
||
|
||
<p class="definition">Definition in file <a class="el" href="../../db/d16/0__1__knapsack_8cpp_source.html">0_1_knapsack.cpp</a>.</p>
|
||
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
|
||
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
||
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">◆ </a></span>main()</h2>
|
||
|
||
<div class="memitem">
|
||
<div class="memproto">
|
||
<table class="memname">
|
||
<tr>
|
||
<td class="memname">int main </td>
|
||
<td>(</td>
|
||
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
||
<td></td>
|
||
</tr>
|
||
</table>
|
||
</div><div class="memdoc">
|
||
|
||
<p>Main function. </p>
|
||
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
|
||
|
||
<p class="definition">Definition at line <a class="el" href="../../db/d16/0__1__knapsack_8cpp_source.html#l00126">126</a> of file <a class="el" href="../../db/d16/0__1__knapsack_8cpp_source.html">0_1_knapsack.cpp</a>.</p>
|
||
<div class="fragment"><div class="line"><span class="lineno"> 126</span> {</div>
|
||
<div class="line"><span class="lineno"> 127</span> <span class="comment">// Testing</span></div>
|
||
<div class="line"><span class="lineno"> 128</span> <a class="code hl_function" href="#aa8dca7b867074164d5f45b0f3851269d">test</a>();</div>
|
||
<div class="line"><span class="lineno"> 129</span> <span class="keywordflow">return</span> 0;</div>
|
||
<div class="line"><span class="lineno"> 130</span>}</div>
|
||
<div class="ttc" id="a0__1__knapsack_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Function to test the above algorithm.</div><div class="ttdef"><b>Definition</b> <a href="../../db/d16/0__1__knapsack_8cpp_source.html#l00096">0_1_knapsack.cpp:96</a></div></div>
|
||
</div><!-- fragment -->
|
||
</div>
|
||
</div>
|
||
<a id="a15edf30f336885e5b851f6b7199c6cd1" name="a15edf30f336885e5b851f6b7199c6cd1"></a>
|
||
<h2 class="memtitle"><span class="permalink"><a href="#a15edf30f336885e5b851f6b7199c6cd1">◆ </a></span>maxKnapsackValue()</h2>
|
||
|
||
<div class="memitem">
|
||
<div class="memproto">
|
||
<div class="memtemplate">
|
||
template<size_t n> </div>
|
||
<table class="memname">
|
||
<tr>
|
||
<td class="memname">int dynamic_programming::knapsack::maxKnapsackValue </td>
|
||
<td>(</td>
|
||
<td class="paramtype">const int</td> <td class="paramname"><span class="paramname"><em>capacity</em></span>, </td>
|
||
</tr>
|
||
<tr>
|
||
<td class="paramkey"></td>
|
||
<td></td>
|
||
<td class="paramtype">const std::array< int, n > &</td> <td class="paramname"><span class="paramname"><em>weight</em></span>, </td>
|
||
</tr>
|
||
<tr>
|
||
<td class="paramkey"></td>
|
||
<td></td>
|
||
<td class="paramtype">const std::array< int, n > &</td> <td class="paramname"><span class="paramname"><em>value</em></span> )</td>
|
||
</tr>
|
||
</table>
|
||
</div><div class="memdoc">
|
||
|
||
<p>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. </p>
|
||
<dl class="tparams"><dt>Template Parameters</dt><dd>
|
||
<table class="tparams">
|
||
<tr><td class="paramname">n</td><td>size of the weight and value array </td></tr>
|
||
</table>
|
||
</dd>
|
||
</dl>
|
||
<dl class="params"><dt>Parameters</dt><dd>
|
||
<table class="params">
|
||
<tr><td class="paramname">capacity</td><td>capacity of the carrying bag </td></tr>
|
||
<tr><td class="paramname">weight</td><td>array representing the weight of items </td></tr>
|
||
<tr><td class="paramname">value</td><td>array representing the value of items </td></tr>
|
||
</table>
|
||
</dd>
|
||
</dl>
|
||
<dl class="section return"><dt>Returns</dt><dd>maximum value obtainable with a given capacity. </dd></dl>
|
||
|
||
<p class="definition">Definition at line <a class="el" href="../../db/d16/0__1__knapsack_8cpp_source.html#l00051">51</a> of file <a class="el" href="../../db/d16/0__1__knapsack_8cpp_source.html">0_1_knapsack.cpp</a>.</p>
|
||
<div class="fragment"><div class="line"><span class="lineno"> 52</span> {</div>
|
||
<div class="line"><span class="lineno"> 53</span> std::vector<std::vector<int> > maxValue(n + 1,</div>
|
||
<div class="line"><span class="lineno"> 54</span> std::vector<int>(capacity + 1, 0));</div>
|
||
<div class="line"><span class="lineno"> 55</span> <span class="comment">// outer loop will select no of items allowed</span></div>
|
||
<div class="line"><span class="lineno"> 56</span> <span class="comment">// inner loop will select the capacity of the knapsack bag</span></div>
|
||
<div class="line"><span class="lineno"> 57</span> <span class="keywordtype">int</span> items = <span class="keyword">sizeof</span>(weight) / <span class="keyword">sizeof</span>(weight[0]);</div>
|
||
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i < items + 1; ++i) {</div>
|
||
<div class="line"><span class="lineno"> 59</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> j = 0; j < capacity + 1; ++j) {</div>
|
||
<div class="line"><span class="lineno"> 60</span> <span class="keywordflow">if</span> (i == 0 || j == 0) {</div>
|
||
<div class="line"><span class="lineno"> 61</span> <span class="comment">// if no of items is zero or capacity is zero, then maxValue</span></div>
|
||
<div class="line"><span class="lineno"> 62</span> <span class="comment">// will be zero</span></div>
|
||
<div class="line"><span class="lineno"> 63</span> maxValue[i][j] = 0;</div>
|
||
<div class="line"><span class="lineno"> 64</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (weight[i - 1] <= j) {</div>
|
||
<div class="line"><span class="lineno"> 65</span> <span class="comment">// if the ith item's weight(in the actual array it will be at i-1)</span></div>
|
||
<div class="line"><span class="lineno"> 66</span> <span class="comment">// is less than or equal to the allowed weight i.e. j then we</span></div>
|
||
<div class="line"><span class="lineno"> 67</span> <span class="comment">// can pick that item for our knapsack. maxValue will be the</span></div>
|
||
<div class="line"><span class="lineno"> 68</span> <span class="comment">// obtained either by picking the current item or by not picking</span></div>
|
||
<div class="line"><span class="lineno"> 69</span> <span class="comment">// current item</span></div>
|
||
<div class="line"><span class="lineno"> 70</span> </div>
|
||
<div class="line"><span class="lineno"> 71</span> <span class="comment">// picking the current item</span></div>
|
||
<div class="line"><span class="lineno"> 72</span> <span class="keywordtype">int</span> profit1 = value[i - 1] + maxValue[i - 1][j - weight[i - 1]];</div>
|
||
<div class="line"><span class="lineno"> 73</span> </div>
|
||
<div class="line"><span class="lineno"> 74</span> <span class="comment">// not picking the current item</span></div>
|
||
<div class="line"><span class="lineno"> 75</span> <span class="keywordtype">int</span> profit2 = maxValue[i - 1][j];</div>
|
||
<div class="line"><span class="lineno"> 76</span> </div>
|
||
<div class="line"><span class="lineno"> 77</span> maxValue[i][j] = std::max(profit1, profit2);</div>
|
||
<div class="line"><span class="lineno"> 78</span> } <span class="keywordflow">else</span> {</div>
|
||
<div class="line"><span class="lineno"> 79</span> <span class="comment">// as the weight of the current item is greater than the allowed weight, so</span></div>
|
||
<div class="line"><span class="lineno"> 80</span> <span class="comment">// maxProfit will be profit obtained by excluding the current item.</span></div>
|
||
<div class="line"><span class="lineno"> 81</span> maxValue[i][j] = maxValue[i - 1][j];</div>
|
||
<div class="line"><span class="lineno"> 82</span> }</div>
|
||
<div class="line"><span class="lineno"> 83</span> }</div>
|
||
<div class="line"><span class="lineno"> 84</span> }</div>
|
||
<div class="line"><span class="lineno"> 85</span> </div>
|
||
<div class="line"><span class="lineno"> 86</span> <span class="comment">// returning maximum value</span></div>
|
||
<div class="line"><span class="lineno"> 87</span> <span class="keywordflow">return</span> maxValue[items][capacity];</div>
|
||
<div class="line"><span class="lineno"> 88</span>}</div>
|
||
</div><!-- fragment -->
|
||
</div>
|
||
</div>
|
||
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
|
||
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">◆ </a></span>test()</h2>
|
||
|
||
<div class="memitem">
|
||
<div class="memproto">
|
||
<table class="mlabels">
|
||
<tr>
|
||
<td class="mlabels-left">
|
||
<table class="memname">
|
||
<tr>
|
||
<td class="memname">void test </td>
|
||
<td>(</td>
|
||
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
||
<td></td>
|
||
</tr>
|
||
</table>
|
||
</td>
|
||
<td class="mlabels-right">
|
||
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
|
||
</tr>
|
||
</table>
|
||
</div><div class="memdoc">
|
||
|
||
<p>Function to test the above algorithm. </p>
|
||
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
|
||
|
||
<p class="definition">Definition at line <a class="el" href="../../db/d16/0__1__knapsack_8cpp_source.html#l00096">96</a> of file <a class="el" href="../../db/d16/0__1__knapsack_8cpp_source.html">0_1_knapsack.cpp</a>.</p>
|
||
<div class="fragment"><div class="line"><span class="lineno"> 96</span> {</div>
|
||
<div class="line"><span class="lineno"> 97</span> <span class="comment">// Test 1</span></div>
|
||
<div class="line"><span class="lineno"> 98</span> <span class="keyword">const</span> <span class="keywordtype">int</span> n1 = 3; <span class="comment">// number of items</span></div>
|
||
<div class="line"><span class="lineno"> 99</span> std::array<int, n1> weight1 = {10, 20, 30}; <span class="comment">// weight of each item</span></div>
|
||
<div class="line"><span class="lineno"> 100</span> std::array<int, n1> value1 = {60, 100, 120}; <span class="comment">// value of each item</span></div>
|
||
<div class="line"><span class="lineno"> 101</span> <span class="keyword">const</span> <span class="keywordtype">int</span> capacity1 = 50; <span class="comment">// capacity of carrying bag</span></div>
|
||
<div class="line"><span class="lineno"> 102</span> <span class="keyword">const</span> <span class="keywordtype">int</span> max_value1 = <a class="code hl_function" href="#a15edf30f336885e5b851f6b7199c6cd1">dynamic_programming::knapsack::maxKnapsackValue</a>(</div>
|
||
<div class="line"><span class="lineno"> 103</span> capacity1, weight1, value1);</div>
|
||
<div class="line"><span class="lineno"> 104</span> <span class="keyword">const</span> <span class="keywordtype">int</span> expected_max_value1 = 220;</div>
|
||
<div class="line"><span class="lineno"> 105</span> assert(max_value1 == expected_max_value1);</div>
|
||
<div class="line"><span class="lineno"> 106</span> std::cout << <span class="stringliteral">"Maximum Knapsack value with "</span> << n1 << <span class="stringliteral">" items is "</span></div>
|
||
<div class="line"><span class="lineno"> 107</span> << max_value1 << std::endl;</div>
|
||
<div class="line"><span class="lineno"> 108</span> </div>
|
||
<div class="line"><span class="lineno"> 109</span> <span class="comment">// Test 2</span></div>
|
||
<div class="line"><span class="lineno"> 110</span> <span class="keyword">const</span> <span class="keywordtype">int</span> n2 = 4; <span class="comment">// number of items</span></div>
|
||
<div class="line"><span class="lineno"> 111</span> std::array<int, n2> weight2 = {24, 10, 10, 7}; <span class="comment">// weight of each item</span></div>
|
||
<div class="line"><span class="lineno"> 112</span> std::array<int, n2> value2 = {24, 18, 18, 10}; <span class="comment">// value of each item</span></div>
|
||
<div class="line"><span class="lineno"> 113</span> <span class="keyword">const</span> <span class="keywordtype">int</span> capacity2 = 25; <span class="comment">// capacity of carrying bag</span></div>
|
||
<div class="line"><span class="lineno"> 114</span> <span class="keyword">const</span> <span class="keywordtype">int</span> max_value2 = <a class="code hl_function" href="#a15edf30f336885e5b851f6b7199c6cd1">dynamic_programming::knapsack::maxKnapsackValue</a>(</div>
|
||
<div class="line"><span class="lineno"> 115</span> capacity2, weight2, value2);</div>
|
||
<div class="line"><span class="lineno"> 116</span> <span class="keyword">const</span> <span class="keywordtype">int</span> expected_max_value2 = 36;</div>
|
||
<div class="line"><span class="lineno"> 117</span> assert(max_value2 == expected_max_value2);</div>
|
||
<div class="line"><span class="lineno"> 118</span> std::cout << <span class="stringliteral">"Maximum Knapsack value with "</span> << n2 << <span class="stringliteral">" items is "</span></div>
|
||
<div class="line"><span class="lineno"> 119</span> << max_value2 << std::endl;</div>
|
||
<div class="line"><span class="lineno"> 120</span>}</div>
|
||
<div class="ttc" id="a0__1__knapsack_8cpp_html_a15edf30f336885e5b851f6b7199c6cd1"><div class="ttname"><a href="#a15edf30f336885e5b851f6b7199c6cd1">dynamic_programming::knapsack::maxKnapsackValue</a></div><div class="ttdeci">int maxKnapsackValue(const int capacity, const std::array< int, n > &weight, const std::array< int, n > &value)</div><div class="ttdoc">Picking up all those items whose combined weight is below the given capacity and calculating the valu...</div><div class="ttdef"><b>Definition</b> <a href="../../db/d16/0__1__knapsack_8cpp_source.html#l00051">0_1_knapsack.cpp:51</a></div></div>
|
||
</div><!-- fragment -->
|
||
</div>
|
||
</div>
|
||
</div><!-- contents -->
|
||
</div><!-- doc-content -->
|
||
<div id="page-nav" class="page-nav-panel">
|
||
<div id="page-nav-resize-handle"></div>
|
||
<div id="page-nav-tree">
|
||
<div id="page-nav-contents">
|
||
</div><!-- page-nav-contents -->
|
||
</div><!-- page-nav-tree -->
|
||
</div><!-- page-nav -->
|
||
</div><!-- container -->
|
||
<!-- start footer part -->
|
||
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
||
<ul>
|
||
<li class="navelem"><a href="../../dir_8a20dd5bfd5341a725342bf72b6b686f.html">dynamic_programming</a></li><li class="navelem"><a href="../../db/d16/0__1__knapsack_8cpp.html">0_1_knapsack.cpp</a></li>
|
||
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
|
||
</ul>
|
||
</div>
|
||
</body>
|
||
</html>
|