<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: fractional code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 19:00:05 GMT</pubDate>
    <description>DZone Snippets: fractional code</description>
    <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>
