Files
C-Plus-Plus/d9/dec/unbounded__0__1__knapsack_8cpp.html
2024-11-04 12:32:06 +00:00

440 lines
40 KiB
HTML

<!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.12.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: dynamic_programming/unbounded_0_1_knapsack.cpp File Reference</title>
<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="../../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="../../resize.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/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.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="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.12.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(1); });
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
/* @license-end */
</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">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('d9/dec/unbounded__0__1__knapsack_8cpp.html','../../'); initResizable(true); });
/* @license-end */
</script>
<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="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">unbounded_0_1_knapsack.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Implementation of the Unbounded 0/1 <a class="el" href="../../d7/daf/namespace_knapsack.html" title="Implementation of 0-1 Knapsack problem.">Knapsack</a> Problem.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;cstdint&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;vector&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for unbounded_0_1_knapsack.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d85/unbounded__0__1__knapsack_8cpp__incl.svg" width="336" 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><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">namespace &#160;</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">&#160;</td><td class="mdescRight">Dynamic Programming algorithms. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">namespace &#160;</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">&#160;</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>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:afe447a5979582174908695952c8a079c" id="r_afe447a5979582174908695952c8a079c"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#afe447a5979582174908695952c8a079c">dynamic_programming::unbounded_knapsack::KnapSackFilling</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> i, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> W, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;val, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;wt, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; int &gt; &gt; &amp;dp)</td></tr>
<tr class="memdesc:afe447a5979582174908695952c8a079c"><td class="mdescLeft">&#160;</td><td class="mdescRight">Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach. <br /></td></tr>
<tr class="separator:afe447a5979582174908695952c8a079c"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a1bcff7f76de48fa7f629480f8f18b5ef" id="r_a1bcff7f76de48fa7f629480f8f18b5ef"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> N, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> W, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;val, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;wt)</td></tr>
<tr class="memdesc:a1bcff7f76de48fa7f629480f8f18b5ef"><td class="mdescLeft">&#160;</td><td class="mdescRight">Wrapper function to initiate the unbounded knapsack calculation. <br /></td></tr>
<tr class="separator:a1bcff7f76de48fa7f629480f8f18b5ef"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a483bb8ccf42aaf7375a83e91490eda1e" id="r_a483bb8ccf42aaf7375a83e91490eda1e"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a> ()</td></tr>
<tr class="memdesc:a483bb8ccf42aaf7375a83e91490eda1e"><td class="mdescLeft">&#160;</td><td class="mdescRight">self test implementation <br /></td></tr>
<tr class="separator:a483bb8ccf42aaf7375a83e91490eda1e"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">main function <br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Implementation of the Unbounded 0/1 <a class="el" href="../../d7/daf/namespace_knapsack.html" title="Implementation of 0-1 Knapsack problem.">Knapsack</a> Problem. </p>
<p>The Unbounded 0/1 <a class="el" href="../../d7/daf/namespace_knapsack.html" title="Implementation of 0-1 Knapsack problem.">Knapsack</a> problem allows taking unlimited quantities of each item. The goal is to maximize the total value without exceeding the given knapsack capacity. Unlike the 0/1 knapsack, where each item can be taken only once, in this variation, any item can be picked any number of times as long as the total weight stays within the knapsack's capacity.</p>
<p>Given a set of N items, each with a weight and a value, represented by the arrays <code>wt</code> and <code>val</code> respectively, and a knapsack with a weight limit W, the task is to fill the knapsack to maximize the total value.</p>
<dl class="section note"><dt>Note</dt><dd>weight and value of items is greater than zero</dd></dl>
<h3><a class="anchor" id="autotoc_md74"></a>
Algorithm</h3>
<p>The approach uses dynamic programming to build a solution iteratively. A 2D array is used for memoization to store intermediate results, allowing the function to avoid redundant calculations.</p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/yeolesanskruti" target="_blank">Sanskruti Yeole</a> </dd></dl>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="../../db/d16/0__1__knapsack_8cpp.html" title="Implementation of [0-1 Knapsack Problem] (https://en.wikipedia.org/wiki/Knapsack_problem)">dynamic_programming/0_1_knapsack.cpp</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="afe447a5979582174908695952c8a079c" name="afe447a5979582174908695952c8a079c"></a>
<h2 class="memtitle"><span class="permalink"><a href="#afe447a5979582174908695952c8a079c">&#9670;&#160;</a></span>KnapSackFilling()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> dynamic_programming::unbounded_knapsack::KnapSackFilling </td>
<td>(</td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a></td> <td class="paramname"><span class="paramname"><em>i</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a></td> <td class="paramname"><span class="paramname"><em>W</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;</td> <td class="paramname"><span class="paramname"><em>val</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;</td> <td class="paramname"><span class="paramname"><em>wt</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; int &gt; &gt; &amp;</td> <td class="paramname"><span class="paramname"><em>dp</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">i</td><td>Current index in the value and weight vectors. </td></tr>
<tr><td class="paramname">W</td><td>Remaining capacity of the knapsack. </td></tr>
<tr><td class="paramname">val</td><td>Vector of values corresponding to the items. </td></tr>
</table>
</dd>
</dl>
<dl class="section note"><dt>Note</dt><dd>"val" data type can be changed according to the size of the input. </dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">wt</td><td>Vector of weights corresponding to the items. </td></tr>
</table>
</dd>
</dl>
<dl class="section note"><dt>Note</dt><dd>"wt" data type can be changed according to the size of the input. </dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">dp</td><td>2D vector for memoization to avoid redundant calculations. </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>The maximum value that can be obtained for the given index and capacity. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 61</span> {</div>
<div class="line"><span class="lineno"> 62</span> <span class="keywordflow">if</span> (i == 0) {</div>
<div class="line"><span class="lineno"> 63</span> <span class="keywordflow">if</span> (wt[0] &lt;= W) {</div>
<div class="line"><span class="lineno"> 64</span> <span class="keywordflow">return</span> (W / wt[0]) *</div>
<div class="line"><span class="lineno"> 65</span> val[0]; <span class="comment">// Take as many of the first item as possible</span></div>
<div class="line"><span class="lineno"> 66</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 67</span> <span class="keywordflow">return</span> 0; <span class="comment">// Can&#39;t take the first item</span></div>
<div class="line"><span class="lineno"> 68</span> }</div>
<div class="line"><span class="lineno"> 69</span> }</div>
<div class="line"><span class="lineno"> 70</span> <span class="keywordflow">if</span> (<a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][W] != -1)</div>
<div class="line"><span class="lineno"> 71</span> <span class="keywordflow">return</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][W]; <span class="comment">// Return result if available</span></div>
<div class="line"><span class="lineno"> 72</span> </div>
<div class="line"><span class="lineno"> 73</span> <span class="keywordtype">int</span> nottake =</div>
<div class="line"><span class="lineno"> 74</span> <a class="code hl_function" href="#afe447a5979582174908695952c8a079c">KnapSackFilling</a>(i - 1, W, val, wt, <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>); <span class="comment">// Value without taking item i</span></div>
<div class="line"><span class="lineno"> 75</span> <span class="keywordtype">int</span> take = 0;</div>
<div class="line"><span class="lineno"> 76</span> <span class="keywordflow">if</span> (W &gt;= wt[i]) {</div>
<div class="line"><span class="lineno"> 77</span> take = val[i] + <a class="code hl_function" href="#afe447a5979582174908695952c8a079c">KnapSackFilling</a>(i, W - wt[i], val, wt,</div>
<div class="line"><span class="lineno"> 78</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>); <span class="comment">// Value taking item i</span></div>
<div class="line"><span class="lineno"> 79</span> }</div>
<div class="line"><span class="lineno"> 80</span> <span class="keywordflow">return</span> <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>[i][W] =</div>
<div class="line"><span class="lineno"> 81</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a>(take, nottake); <span class="comment">// Store and return the maximum value</span></div>
<div class="line"><span class="lineno"> 82</span>}</div>
<div class="ttc" id="amax_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a></div><div class="ttdeci">T max(T... args)</div></div>
<div class="ttc" id="anamespacedp_html"><div class="ttname"><a href="../../df/d88/namespacedp.html">dp</a></div><div class="ttdoc">for std::vector</div><div class="ttdef"><b>Definition</b> partition_problem.cpp:39</div></div>
<div class="ttc" id="aunbounded__0__1__knapsack_8cpp_html_afe447a5979582174908695952c8a079c"><div class="ttname"><a href="#afe447a5979582174908695952c8a079c">dynamic_programming::unbounded_knapsack::KnapSackFilling</a></div><div class="ttdeci">std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W, const std::vector&lt; std::uint16_t &gt; &amp;val, const std::vector&lt; std::uint16_t &gt; &amp;wt, std::vector&lt; std::vector&lt; int &gt; &gt; &amp;dp)</div><div class="ttdoc">Recursive function to calculate the maximum value obtainable using an unbounded knapsack approach.</div><div class="ttdef"><b>Definition</b> unbounded_0_1_knapsack.cpp:58</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/dec/unbounded__0__1__knapsack_8cpp_afe447a5979582174908695952c8a079c_cgraph.svg" width="287" height="91"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</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 successful exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 170</span> {</div>
<div class="line"><span class="lineno"> 171</span> <a class="code hl_function" href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a>(); <span class="comment">// Run self test implementation</span></div>
<div class="line"><span class="lineno"> 172</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 173</span>}</div>
<div class="ttc" id="aunbounded__0__1__knapsack_8cpp_html_a483bb8ccf42aaf7375a83e91490eda1e"><div class="ttname"><a href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a></div><div class="ttdeci">static void tests()</div><div class="ttdoc">self test implementation</div><div class="ttdef"><b>Definition</b> unbounded_0_1_knapsack.cpp:111</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/dec/unbounded__0__1__knapsack_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="278" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="a483bb8ccf42aaf7375a83e91490eda1e" name="a483bb8ccf42aaf7375a83e91490eda1e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a483bb8ccf42aaf7375a83e91490eda1e">&#9670;&#160;</a></span>tests()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void tests </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</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>self test implementation </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 111</span> {</div>
<div class="line"><span class="lineno"> 112</span> <span class="comment">// Test Case 1</span></div>
<div class="line"><span class="lineno"> 113</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> N1 = 4; <span class="comment">// Number of items</span></div>
<div class="line"><span class="lineno"> 114</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> wt1 = {1, 3, 4, 5}; <span class="comment">// Weights of the items</span></div>
<div class="line"><span class="lineno"> 115</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> val1 = {6, 1, 7, 7}; <span class="comment">// Values of the items</span></div>
<div class="line"><span class="lineno"> 116</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> W1 = 8; <span class="comment">// Maximum capacity of the knapsack</span></div>
<div class="line"><span class="lineno"> 117</span> <span class="comment">// Test the function and assert the expected output</span></div>
<div class="line"><span class="lineno"> 118</span> assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(</div>
<div class="line"><span class="lineno"> 119</span> N1, W1, val1, wt1) == 48);</div>
<div class="line"><span class="lineno"> 120</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Maximum Knapsack value &quot;</span></div>
<div class="line"><span class="lineno"> 121</span> &lt;&lt; <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
<div class="line"><span class="lineno"> 122</span> N1, W1, val1, wt1)</div>
<div class="line"><span class="lineno"> 123</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 124</span> </div>
<div class="line"><span class="lineno"> 125</span> <span class="comment">// Test Case 2</span></div>
<div class="line"><span class="lineno"> 126</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> N2 = 3; <span class="comment">// Number of items</span></div>
<div class="line"><span class="lineno"> 127</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> wt2 = {10, 20, 30}; <span class="comment">// Weights of the items</span></div>
<div class="line"><span class="lineno"> 128</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> val2 = {60, 100, 120}; <span class="comment">// Values of the items</span></div>
<div class="line"><span class="lineno"> 129</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> W2 = 5; <span class="comment">// Maximum capacity of the knapsack</span></div>
<div class="line"><span class="lineno"> 130</span> <span class="comment">// Test the function and assert the expected output</span></div>
<div class="line"><span class="lineno"> 131</span> assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(</div>
<div class="line"><span class="lineno"> 132</span> N2, W2, val2, wt2) == 0);</div>
<div class="line"><span class="lineno"> 133</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Maximum Knapsack value &quot;</span></div>
<div class="line"><span class="lineno"> 134</span> &lt;&lt; <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
<div class="line"><span class="lineno"> 135</span> N2, W2, val2, wt2)</div>
<div class="line"><span class="lineno"> 136</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 137</span> </div>
<div class="line"><span class="lineno"> 138</span> <span class="comment">// Test Case 3</span></div>
<div class="line"><span class="lineno"> 139</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> N3 = 3; <span class="comment">// Number of items</span></div>
<div class="line"><span class="lineno"> 140</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> wt3 = {2, 4, 6}; <span class="comment">// Weights of the items</span></div>
<div class="line"><span class="lineno"> 141</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> val3 = {5, 11, 13}; <span class="comment">// Values of the items</span></div>
<div class="line"><span class="lineno"> 142</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> W3 = 27; <span class="comment">// Maximum capacity of the knapsack</span></div>
<div class="line"><span class="lineno"> 143</span> <span class="comment">// Test the function and assert the expected output</span></div>
<div class="line"><span class="lineno"> 144</span> assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(</div>
<div class="line"><span class="lineno"> 145</span> N3, W3, val3, wt3) == 27);</div>
<div class="line"><span class="lineno"> 146</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Maximum Knapsack value &quot;</span></div>
<div class="line"><span class="lineno"> 147</span> &lt;&lt; <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
<div class="line"><span class="lineno"> 148</span> N3, W3, val3, wt3)</div>
<div class="line"><span class="lineno"> 149</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 150</span> </div>
<div class="line"><span class="lineno"> 151</span> <span class="comment">// Test Case 4</span></div>
<div class="line"><span class="lineno"> 152</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> N4 = 0; <span class="comment">// Number of items</span></div>
<div class="line"><span class="lineno"> 153</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> wt4 = {}; <span class="comment">// Weights of the items</span></div>
<div class="line"><span class="lineno"> 154</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::uint16_t&gt;</a> val4 = {}; <span class="comment">// Values of the items</span></div>
<div class="line"><span class="lineno"> 155</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> W4 = 10; <span class="comment">// Maximum capacity of the knapsack</span></div>
<div class="line"><span class="lineno"> 156</span> assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(</div>
<div class="line"><span class="lineno"> 157</span> N4, W4, val4, wt4) == 0);</div>
<div class="line"><span class="lineno"> 158</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Maximum Knapsack value for empty arrays: &quot;</span></div>
<div class="line"><span class="lineno"> 159</span> &lt;&lt; <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
<div class="line"><span class="lineno"> 160</span> N4, W4, val4, wt4)</div>
<div class="line"><span class="lineno"> 161</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 162</span> </div>
<div class="line"><span class="lineno"> 163</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;All test cases passed!&quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 164</span>}</div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
<div class="ttc" id="ainteger_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a></div></div>
<div class="ttc" id="aunbounded__0__1__knapsack_8cpp_html_a1bcff7f76de48fa7f629480f8f18b5ef"><div class="ttname"><a href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a></div><div class="ttdeci">std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W, const std::vector&lt; std::uint16_t &gt; &amp;val, const std::vector&lt; std::uint16_t &gt; &amp;wt)</div><div class="ttdoc">Wrapper function to initiate the unbounded knapsack calculation.</div><div class="ttdef"><b>Definition</b> unbounded_0_1_knapsack.cpp:93</div></div>
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/dec/unbounded__0__1__knapsack_8cpp_a483bb8ccf42aaf7375a83e91490eda1e_cgraph.svg" width="180" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
<a id="a1bcff7f76de48fa7f629480f8f18b5ef" name="a1bcff7f76de48fa7f629480f8f18b5ef"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1bcff7f76de48fa7f629480f8f18b5ef">&#9670;&#160;</a></span>unboundedKnapsack()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> dynamic_programming::unbounded_knapsack::unboundedKnapsack </td>
<td>(</td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a></td> <td class="paramname"><span class="paramname"><em>N</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a></td> <td class="paramname"><span class="paramname"><em>W</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;</td> <td class="paramname"><span class="paramname"><em>val</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/types/integer.html">std::uint16_t</a> &gt; &amp;</td> <td class="paramname"><span class="paramname"><em>wt</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Wrapper function to initiate the unbounded knapsack calculation. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">N</td><td>Number of items. </td></tr>
<tr><td class="paramname">W</td><td>Maximum weight capacity of the knapsack. </td></tr>
<tr><td class="paramname">val</td><td>Vector of values corresponding to the items. </td></tr>
<tr><td class="paramname">wt</td><td>Vector of weights corresponding to the items. </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>The maximum value that can be obtained for the given capacity. </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 95</span> {</div>
<div class="line"><span class="lineno"> 96</span> <span class="keywordflow">if</span> (N == 0)</div>
<div class="line"><span class="lineno"> 97</span> <span class="keywordflow">return</span> 0; <span class="comment">// Expect 0 since no items</span></div>
<div class="line"><span class="lineno"> 98</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;std::vector&lt;int&gt;</a>&gt; <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>(</div>
<div class="line"><span class="lineno"> 99</span> N, <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;int&gt;</a>(W + 1, -1)); <span class="comment">// Initialize memoization table</span></div>
<div class="line"><span class="lineno"> 100</span> <span class="keywordflow">return</span> <a class="code hl_function" href="#afe447a5979582174908695952c8a079c">KnapSackFilling</a>(N - 1, W, val, wt, <a class="code hl_namespace" href="../../df/d88/namespacedp.html">dp</a>); <span class="comment">// Start the calculation</span></div>
<div class="line"><span class="lineno"> 101</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/dec/unbounded__0__1__knapsack_8cpp_a1bcff7f76de48fa7f629480f8f18b5ef_cgraph.svg" width="490" height="91"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_8a20dd5bfd5341a725342bf72b6b686f.html">dynamic_programming</a></li><li class="navelem"><a class="el" href="../../d9/dec/unbounded__0__1__knapsack_8cpp.html">unbounded_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.12.0 </li>
</ul>
</div>
</body>
</html>