<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[knapsack problem]]></title><description><![CDATA[<p>Hello,<br />
I implemented the knapsack problem in C++. it seems my final result is correct...but the intermediate result seem to be different when i display the entire 2d array...could someone give me some feedback on this?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;array&gt;
using namespace std;

const int n_size = 4;
const int w_size = 10;

int main() {
	// Knapsack Problem
	// Time Complexity: O(n*w), Space Complexity: O(n*w)

	// Values (stored in array v)
	// Weights (stored in array w)
	// Number of distinct items (n)
	// Knapsack capacity (W)

    	std::vector&lt;int&gt; v = {10, 40, 30, 50};
    	std::vector&lt;int&gt; w = {5, 4, 6, 3};
                              	   //columns //rows
	std::array&lt;std::array&lt;int, w_size+1&gt;, n_size+1&gt; A; 

	// initialization
	for(int w = 0; w &lt;= w_size; w++) {
		A[0][w] = 0;
	}

	// main loop
	for(int i = 1; i &lt;= n_size; i++) {
		for(int j = 0; j &lt;= w_size; j++) {
			if(w[i] &gt; j) {
				A[i][j] = A[i - 1][j];
			}
			else {
				A[i][j] = max(A[i - 1][j], v[i] + A[i - 1][j - w[i&rsqb;&rsqb;);
			}
		}
	}

	// display 2d array A
	for(int i = 0; i &lt;= n_size; i++) {
		for(int j = 0; j &lt;= w_size; j++) {
			cout &lt;&lt; A[i][j] &lt;&lt; &quot; &quot;;
		}
		cout &lt;&lt; endl;
	}

	cout &lt;&lt; endl;
	cout &lt;&lt; A[n_size][w_size] &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/321963/knapsack-problem</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Jul 2026 20:30:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/321963.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 30 Nov 2013 20:34:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to knapsack problem on Sat, 30 Nov 2013 20:34:07 GMT]]></title><description><![CDATA[<p>Hello,<br />
I implemented the knapsack problem in C++. it seems my final result is correct...but the intermediate result seem to be different when i display the entire 2d array...could someone give me some feedback on this?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;array&gt;
using namespace std;

const int n_size = 4;
const int w_size = 10;

int main() {
	// Knapsack Problem
	// Time Complexity: O(n*w), Space Complexity: O(n*w)

	// Values (stored in array v)
	// Weights (stored in array w)
	// Number of distinct items (n)
	// Knapsack capacity (W)

    	std::vector&lt;int&gt; v = {10, 40, 30, 50};
    	std::vector&lt;int&gt; w = {5, 4, 6, 3};
                              	   //columns //rows
	std::array&lt;std::array&lt;int, w_size+1&gt;, n_size+1&gt; A; 

	// initialization
	for(int w = 0; w &lt;= w_size; w++) {
		A[0][w] = 0;
	}

	// main loop
	for(int i = 1; i &lt;= n_size; i++) {
		for(int j = 0; j &lt;= w_size; j++) {
			if(w[i] &gt; j) {
				A[i][j] = A[i - 1][j];
			}
			else {
				A[i][j] = max(A[i - 1][j], v[i] + A[i - 1][j - w[i&rsqb;&rsqb;);
			}
		}
	}

	// display 2d array A
	for(int i = 0; i &lt;= n_size; i++) {
		for(int j = 0; j &lt;= w_size; j++) {
			cout &lt;&lt; A[i][j] &lt;&lt; &quot; &quot;;
		}
		cout &lt;&lt; endl;
	}

	cout &lt;&lt; endl;
	cout &lt;&lt; A[n_size][w_size] &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2369145</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2369145</guid><dc:creator><![CDATA[algoman1]]></dc:creator><pubDate>Sat, 30 Nov 2013 20:34:07 GMT</pubDate></item><item><title><![CDATA[Reply to knapsack problem on Sat, 30 Nov 2013 23:49:55 GMT]]></title><description><![CDATA[<p>It looks like the initialisation is wrong. Your code says, there is a knapsack weighting 10kg with 0 value. But obviously, such a knapsack doesn't exist. The standard hack around this is initializing everything except 0kg with INT_MIN:</p>
<pre><code class="language-cpp">#include &lt;limits&gt;

    // initialization
    A[0][0] = 0;
    for(int w = 1; w &lt;= w_size; w++)
        A[0][w] = std::numeric_limits&lt;int&gt;::min();
</code></pre>
<p>Then, every state with value &lt;0 means impossible.</p>
<p>(I recommend this hack, as it's much faster than a seperate bool array bool possible[w_size] since it's getting rid of the banching. And it should work just as fine.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2369159</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2369159</guid><dc:creator><![CDATA[aligatorman2]]></dc:creator><pubDate>Sat, 30 Nov 2013 23:49:55 GMT</pubDate></item></channel></rss>