<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: knapsack code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 07:10:11 GMT</pubDate>
    <description>DZone Snippets: knapsack 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>
    <item>
      <title>The Fractional Knapsack Problem in C</title>
      <link>http://snippets.dzone.com/posts/show/4800</link>
      <description>This is the classic Greedy algorithm implementation for solving the &lt;a href="http://en.wikipedia.org/wiki/Knapsack_problem"&gt;Fractional Knapsack Problem&lt;/a&gt; in C.&lt;br /&gt;&lt;br /&gt;Further explanations &lt;a href="http://compprog.wordpress.com/2007/11/20/the-fractional-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;int n = 5; /* The number of objects */&lt;br /&gt;int c[10] = {12, 1, 2, 1, 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] = {4, 2, 2, 1, 10}; /* 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 = 15; /* The maximum weight you can take */&lt;br /&gt;&lt;br /&gt;void simple_fill() {&lt;br /&gt;	int cur_w;&lt;br /&gt;	float tot_v;&lt;br /&gt;	int i, maxi;&lt;br /&gt;	int used[10];&lt;br /&gt;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		used[i] = 0; /* I have not used the ith object yet */&lt;br /&gt;&lt;br /&gt;	cur_w = W;&lt;br /&gt;	while (cur_w &gt; 0) { /* while there's still room*/&lt;br /&gt;		/* Find the best object */&lt;br /&gt;		maxi = -1;&lt;br /&gt;		for (i = 0; i &lt; n; ++i)&lt;br /&gt;			if ((used[i] == 0) &amp;&amp;&lt;br /&gt;				((maxi == -1) || ((float)v[i]/c[i] &gt; (float)v[maxi]/c[maxi])))&lt;br /&gt;				maxi = i;&lt;br /&gt;&lt;br /&gt;		used[maxi] = 1; /* mark the maxi-th object as used */&lt;br /&gt;		cur_w -= c[maxi]; /* with the object in the bag, I can carry less */&lt;br /&gt;		tot_v += v[maxi];&lt;br /&gt;		if (cur_w &gt;= 0)&lt;br /&gt;			printf("Added object %d (%d$, %dKg) completly in the bag. Space left: %d.\n", maxi + 1, v[maxi], c[maxi], cur_w);&lt;br /&gt;		else {&lt;br /&gt;			printf("Added %d%% (%d$, %dKg) of object %d in the bag.\n", (int)((1 + (float)cur_w/c[maxi]) * 100), v[maxi], c[maxi], maxi + 1);&lt;br /&gt;			tot_v -= v[maxi];&lt;br /&gt;			tot_v += (1 + (float)cur_w/c[maxi]) * v[maxi];&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	printf("Filled the bag with objects worth %.2f$.\n", tot_v);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	simple_fill();&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 Nov 2007 13:39:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4800</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
  </channel>
</rss>
