<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Algorithms on Daniel Pomfret</title>
    <link>https://pomfret.uk/tags/algorithms/</link>
    <description>Recent content in Algorithms on Daniel Pomfret</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <copyright>© Daniel Pomfret</copyright>
    <lastBuildDate>Tue, 27 Sep 2016 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://pomfret.uk/tags/algorithms/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Knapsack Problem (0-1 solution) - Dynamic Algorithm</title>
      <link>https://pomfret.uk/posts/knapsack-problem-0-1-solution-dynamic-algorithm/</link>
      <pubDate>Tue, 27 Sep 2016 00:00:00 +0000</pubDate>
      <guid>https://pomfret.uk/posts/knapsack-problem-0-1-solution-dynamic-algorithm/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve recently dug up old code from my University days, which I thought I&amp;rsquo;d share for the benefit/misfortune of others.&lt;/p&gt;&#xA;&lt;p&gt;There&amp;rsquo;s a common problem in programming called the knapsack problem. Here was my solution based on the dynamic algorithm.&lt;/p&gt;&#xA;&lt;p&gt;# Math is used to floor/round the floats to an interger and back!&#xA;import math&lt;/p&gt;&#xA;&lt;h1 id=&#34;total-allowed-weight&#34;&gt;Total allowed weight&lt;/h1&gt;&#xA;&lt;p&gt;totalWeight = 2392;&lt;/p&gt;&#xA;&lt;h1 id=&#34;define-the-items-name-weight-profit&#34;&gt;Define the items name, Weight, Profit&lt;/h1&gt;&#xA;&lt;p&gt;items = ((&amp;ldquo;Weapon and Ammunition&amp;rdquo;,    4.13, 1.4),&#xA;(&amp;ldquo;Water&amp;rdquo;, 2.13, 2.74),&#xA;(&amp;ldquo;Pith Helmet&amp;rdquo;, 3.03, 1.55),&#xA;(&amp;ldquo;Sun Cream&amp;rdquo;, 2.26, 0.82),&#xA;(&amp;ldquo;Tent&amp;rdquo;, 3.69, 2.38),&#xA;(&amp;ldquo;Flare Gun&amp;rdquo;, 3.45, 2.93),&#xA;(&amp;ldquo;Olive Oil&amp;rdquo;, 1.09, 1.77),&#xA;(&amp;ldquo;Firewood&amp;rdquo;, 2.89, 0.53),&#xA;(&amp;ldquo;Kendal Mint Cake&amp;rdquo;, 1.08, 2.77),&#xA;(&amp;ldquo;Snake Repellant Spray&amp;rdquo;, 3.23, 4.29),&#xA;(&amp;ldquo;Bread&amp;rdquo;, 2.29, 2.85),&#xA;(&amp;ldquo;Pot Noodles&amp;rdquo;, 0.55, 0.34),&#xA;(&amp;ldquo;Software Engineering Textbook&amp;rdquo;, 2.82, -0.45),&#xA;(&amp;ldquo;Tinned Food&amp;rdquo;, 2.31, 2.17),&#xA;(&amp;ldquo;Pork Pie&amp;rdquo;, 1.63, 1.62))&lt;/p&gt;</description>
    </item>
    <item>
      <title>Knapsack Problem (0-1 solution) - Greedy Algorithm</title>
      <link>https://pomfret.uk/posts/knapsack-problem-0-1-solution-greedy-algorithm/</link>
      <pubDate>Tue, 27 Sep 2016 00:00:00 +0000</pubDate>
      <guid>https://pomfret.uk/posts/knapsack-problem-0-1-solution-greedy-algorithm/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve recently dug up old code from my University days, which I thought I&amp;rsquo;d share for the benefit/misfortune of others.&lt;/p&gt;&#xA;&lt;p&gt;There&amp;rsquo;s a common problem in programming called the knapsack problem. Here was my solution based on the greedy algorithm.&lt;/p&gt;&#xA;&lt;p&gt;# Name, Weight and Value of Items&#xA;items = [(&amp;ldquo;Weapon and Ammunition&amp;rdquo;,    4.13, 1.4),&#xA;(&amp;ldquo;Water&amp;rdquo;, 2.13, 2.74),&#xA;(&amp;ldquo;Pith Helmet&amp;rdquo;, 3.03, 1.55),&#xA;(&amp;ldquo;Sun Cream&amp;rdquo;, 2.26, 0.82),&#xA;(&amp;ldquo;Tent&amp;rdquo;, 3.69, 2.38),&#xA;(&amp;ldquo;Flare Gun&amp;rdquo;, 3.45, 2.93),&#xA;(&amp;ldquo;Olive Oil&amp;rdquo;, 1.09, 1.77),&#xA;(&amp;ldquo;Firewood&amp;rdquo;, 2.89, 0.53),&#xA;(&amp;ldquo;Kendal Mint Cake&amp;rdquo;, 1.08, 2.77),&#xA;(&amp;ldquo;Snake Repellant Spray&amp;rdquo;, 3.23, 4.29),&#xA;(&amp;ldquo;Bread&amp;rdquo;, 2.29, 2.85),&#xA;(&amp;ldquo;Pot Noodles&amp;rdquo;, 0.55, 0.34),&#xA;(&amp;ldquo;Software Engineering Textbook&amp;rdquo;, 2.82, -0.45),&#xA;(&amp;ldquo;Tinned Food&amp;rdquo;, 2.31, 2.17),&#xA;(&amp;ldquo;Pork Pie&amp;rdquo;, 1.63, 1.62)]&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
