<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: discrete code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 06:04:05 GMT</pubDate>
    <description>DZone Snippets: discrete code</description>
    <item>
      <title>The 0-1 Knapsack Problem in C</title>
      <link>http://snippets.dzone.com/posts/show/4802</link>
      <description>This is a dynamic-programming algorithm implementation for solving the &lt;a href="http://en.wikipedia.org/wiki/Knapsack_problem"&gt;the 0-1 Knapsack Problem&lt;/a&gt; in C.&lt;br /&gt;&lt;br /&gt;Further explanation is given &lt;a href="http://compprog.wordpress.com/2007/11/20/the-0-1-knapsack-problem/"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;#define MAXWEIGHT 100&lt;br /&gt;&lt;br /&gt;int n = 3; /* The number of objects */&lt;br /&gt;int c[10] = {8, 6, 4}; /* c[i] is the *COST* of the ith object; i.e. what&lt;br /&gt;				YOU PAY to take the object */&lt;br /&gt;int v[10] = {16, 10, 7}; /* v[i] is the *VALUE* of the ith object; i.e.&lt;br /&gt;				what YOU GET for taking the object */&lt;br /&gt;int W = 10; /* The maximum weight you can take */ &lt;br /&gt;&lt;br /&gt;void fill_sack() {&lt;br /&gt;	int a[MAXWEIGHT]; /* a[i] holds the maximum value that can be obtained&lt;br /&gt;				using at most i weight */&lt;br /&gt;	int last_added[MAXWEIGHT]; /* I use this to calculate which object were&lt;br /&gt;					added */&lt;br /&gt;	int i, j;&lt;br /&gt;	int aux;&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt;= W; ++i) {&lt;br /&gt;		a[i] = 0;&lt;br /&gt;		last_added[i] = -1;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	a[0] = 0;&lt;br /&gt;	for (i = 1; i &lt;= W; ++i)&lt;br /&gt;		for (j = 0; j &lt; n; ++j)&lt;br /&gt;			if ((c[j] &lt;= i) &amp;&amp; (a[i] &lt; a[i - c[j]] + v[j])) {&lt;br /&gt;				a[i] = a[i - c[j]] + v[j];&lt;br /&gt;				last_added[i] = j;&lt;br /&gt;			}&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt;= W; ++i)&lt;br /&gt;		if (last_added[i] != -1)&lt;br /&gt;			printf("Weight %d; Benefit: %d; To reach this weight I added object %d (%d$ %dKg) to weight %d.\n", i, a[i], last_added[i] + 1, v[last_added[i]], c[last_added[i]], i - c[last_added[i]]);&lt;br /&gt;		else&lt;br /&gt;			printf("Weight %d; Benefit: 0; Can't reach this exact weight.\n", i);&lt;br /&gt;&lt;br /&gt;	printf("---\n");&lt;br /&gt;&lt;br /&gt;	aux = W;&lt;br /&gt;	while ((aux &gt; 0) &amp;&amp; (last_added[aux] != -1)) {&lt;br /&gt;		printf("Added object %d (%d$ %dKg). Space left: %d\n", last_added[aux] + 1, v[last_added[aux]], c[last_added[aux]], aux - c[last_added[aux]]);&lt;br /&gt;		aux -= c[last_added[aux]];&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	printf("Total value added: %d$\n", a[W]);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	fill_sack();&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 Nov 2007 17:17:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4802</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
  </channel>
</rss>
