mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-21 04:18:34 +08:00
Documentation for c26eea874d
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<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="generator" content="Doxygen 1.13.2"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>TheAlgorithms/C++: dynamic_programming/unbounded_0_1_knapsack.cpp File Reference</title>
|
||||
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
|
||||
@@ -60,7 +60,7 @@ window.MathJax = {
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.12.0 -->
|
||||
<!-- Generated by Doxygen 1.13.2 -->
|
||||
<script type="text/javascript">
|
||||
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
||||
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
||||
@@ -248,20 +248,19 @@ Algorithm</h3>
|
||||
<div class="line"><span class="lineno"> 67</span> <span class="keywordflow">return</span> 0; <span class="comment">// Can'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"> 70</span> <span class="keywordflow">if</span> (dp[i][W] != -1)</div>
|
||||
<div class="line"><span class="lineno"> 71</span> <span class="keywordflow">return</span> dp[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"> 74</span> <a class="code hl_function" href="#afe447a5979582174908695952c8a079c">KnapSackFilling</a>(i - 1, W, val, wt, dp); <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 >= 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"> 78</span> dp); <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"> 80</span> <span class="keywordflow">return</span> dp[i][W] =</div>
|
||||
<div class="line"><span class="lineno"> 81</span> std::max(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="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> <a href="../../de/d8c/partition__problem_8cpp_source.html#l00039">partition_problem.cpp:39</a></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< std::uint16_t > &val, const std::vector< std::uint16_t > &wt, std::vector< std::vector< int > > &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> <a href="../../d9/dec/unbounded__0__1__knapsack_8cpp_source.html#l00058">unbounded_0_1_knapsack.cpp:58</a></div></div>
|
||||
</div><!-- fragment -->
|
||||
</div>
|
||||
@@ -311,7 +310,7 @@ Algorithm</h3>
|
||||
</table>
|
||||
</td>
|
||||
<td class="mlabels-right">
|
||||
<span class="mlabels"><span class="mlabel">static</span></span> </td>
|
||||
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
|
||||
</tr>
|
||||
</table>
|
||||
</div><div class="memdoc">
|
||||
@@ -327,7 +326,7 @@ Algorithm</h3>
|
||||
<div class="line"><span class="lineno"> 115</span> std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; <span class="comment">// Values of the items</span></div>
|
||||
<div class="line"><span class="lineno"> 116</span> std::uint16_t 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"> 118</span> assert(<a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
<div class="line"><span class="lineno"> 119</span> N1, W1, val1, wt1) == 48);</div>
|
||||
<div class="line"><span class="lineno"> 120</span> std::cout << <span class="stringliteral">"Maximum Knapsack value "</span></div>
|
||||
<div class="line"><span class="lineno"> 121</span> << <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
@@ -340,7 +339,7 @@ Algorithm</h3>
|
||||
<div class="line"><span class="lineno"> 128</span> std::vector<std::uint16_t> val2 = {60, 100, 120}; <span class="comment">// Values of the items</span></div>
|
||||
<div class="line"><span class="lineno"> 129</span> std::uint16_t 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"> 131</span> assert(<a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
<div class="line"><span class="lineno"> 132</span> N2, W2, val2, wt2) == 0);</div>
|
||||
<div class="line"><span class="lineno"> 133</span> std::cout << <span class="stringliteral">"Maximum Knapsack value "</span></div>
|
||||
<div class="line"><span class="lineno"> 134</span> << <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
@@ -353,7 +352,7 @@ Algorithm</h3>
|
||||
<div class="line"><span class="lineno"> 141</span> std::vector<std::uint16_t> val3 = {5, 11, 13}; <span class="comment">// Values of the items</span></div>
|
||||
<div class="line"><span class="lineno"> 142</span> std::uint16_t 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"> 144</span> assert(<a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
<div class="line"><span class="lineno"> 145</span> N3, W3, val3, wt3) == 27);</div>
|
||||
<div class="line"><span class="lineno"> 146</span> std::cout << <span class="stringliteral">"Maximum Knapsack value "</span></div>
|
||||
<div class="line"><span class="lineno"> 147</span> << <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
@@ -365,7 +364,7 @@ Algorithm</h3>
|
||||
<div class="line"><span class="lineno"> 153</span> std::vector<std::uint16_t> wt4 = {}; <span class="comment">// Weights of the items</span></div>
|
||||
<div class="line"><span class="lineno"> 154</span> std::vector<std::uint16_t> val4 = {}; <span class="comment">// Values of the items</span></div>
|
||||
<div class="line"><span class="lineno"> 155</span> std::uint16_t 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"> 156</span> assert(<a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
<div class="line"><span class="lineno"> 157</span> N4, W4, val4, wt4) == 0);</div>
|
||||
<div class="line"><span class="lineno"> 158</span> std::cout << <span class="stringliteral">"Maximum Knapsack value for empty arrays: "</span></div>
|
||||
<div class="line"><span class="lineno"> 159</span> << <a class="code hl_function" href="#a1bcff7f76de48fa7f629480f8f18b5ef">dynamic_programming::unbounded_knapsack::unboundedKnapsack</a>(</div>
|
||||
@@ -427,6 +426,7 @@ Algorithm</h3>
|
||||
<div class="line"><span class="lineno"> 99</span> N, std::vector<int>(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 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> <a href="../../de/d8c/partition__problem_8cpp_source.html#l00039">partition_problem.cpp:39</a></div></div>
|
||||
</div><!-- fragment -->
|
||||
</div>
|
||||
</div>
|
||||
@@ -436,7 +436,7 @@ Algorithm</h3>
|
||||
<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>
|
||||
<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.13.2 </li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user