<?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 smarx.com</title>
    <link>https://smarx.com/tags/algorithms/</link>
    <description>Recent content on smarx.com</description>
    <language>en-us</language>
    <managingEditor>smarx@smarx.com (Steve Marx)</managingEditor>
    <webMaster>smarx@smarx.com (Steve Marx)</webMaster>
    <lastBuildDate>Wed, 23 Sep 2020 13:28:30 -0400</lastBuildDate>
    
        <atom:link href="https://smarx.com/tags/algorithms/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Dynamic Programming Is Easy</title>
      <link>https://smarx.com/posts/2020/09/dynamic-programming-is-easy/</link>
      <pubDate>Wed, 23 Sep 2020 13:28:30 -0400</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/09/dynamic-programming-is-easy/</guid>
      <description>&lt;p&gt;&lt;strong&gt;Dynamic programming!&lt;/strong&gt; Did I scare you?&lt;/p&gt;
&lt;p&gt;Many programmers are intimidated by &amp;ldquo;dynamic programming&amp;rdquo;, but I think it&amp;rsquo;s just a bad name for a simple technique. In this post, I&amp;rsquo;ll walk you through dynamic programming solutions to two problems. You may be surprised by how obvious the dynamic programming solutions are.&lt;/p&gt;





    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/09/dynamic-programming-is-easy/dp-featured.png&#34; data-lightbox=&#34;88f5b953fe468362c68f5462b5fb01c4&#34; &gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/09/dynamic-programming-is-easy/dp-featured_hu3bcb0140f1a1632b9a8fe3f944538bc1_100520_320x0_resize_lanczos_2.png&#34; width=&#34;320&#34; height=&#34;173&#34;&gt;
    
    &lt;/a&gt;
    
    
&lt;/figure&gt;

&lt;p&gt;I&amp;rsquo;ll start with the Fibonacci sequence because the code is easy to follow. If you&amp;rsquo;re more of a visual learner, feel free to jump down to the second example. It has animations!&lt;/p&gt;
&lt;h2 id=&#34;fibonacci&#34;&gt;Fibonacci&lt;/h2&gt;
&lt;p&gt;In case you haven&amp;rsquo;t seen it before, the Fibonacci sequence looks like this: &lt;code&gt;1, 1, 2, 3, 5, 8, 13, 21, ...&lt;/code&gt;. The sequence starts with two &lt;code&gt;1&lt;/code&gt;s, and each subsequent number is the sum of the two previous numbers.&lt;/p&gt;
&lt;h3 id=&#34;slow-fibonacci&#34;&gt;Slow Fibonacci&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s a Python function that calculates the &lt;em&gt;n&lt;sup&gt;th&lt;/sup&gt;&lt;/em&gt; number in the Fibonacci sequence using a simple recursive implementation:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;fib&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fib&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fib&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code works, but it&amp;rsquo;s very slow. Every call (except for &lt;code&gt;n = 1&lt;/code&gt; and &lt;code&gt;n = 2&lt;/code&gt;) results in two additional calls, so the result is exponential. By that, I mean that computing &lt;code&gt;fib(n+1)&lt;/code&gt; is about twice as hard as computing &lt;code&gt;fib(n)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In this next version of the code, I&amp;rsquo;m counting the calls:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;n&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;fib&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;global&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fib&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fib&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fib&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Running &lt;code&gt;fib(40)&lt;/code&gt; correctly computes the 40th fibonacci number (102,334,155), but it uses &lt;strong&gt;204,668,309&lt;/strong&gt; calls to &lt;code&gt;fib()&lt;/code&gt;. On my laptop, this takes about 30 seconds.&lt;/p&gt;
&lt;p&gt;So what&amp;rsquo;s wrong with this code? The issue is that it&amp;rsquo;s doing the same work over and over again. Below is the call graph for &lt;code&gt;fib(5)&lt;/code&gt;. Notice how much repetition there is. This inefficiency only increases when computing larger numbers:&lt;/p&gt;
&lt;p&gt;&lt;svg id=&#34;mermaid-1600873258828&#34; class=&#34;mx-auto&#34; width=&#34;538.0703125&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34; xmlns:xlink=&#34;http://www.w3.org/1999/xlink&#34; height=&#34;441&#34; viewBox=&#34;0 0 538.0703125 441&#34; style=&#34;max-width:100%&#34;&gt;&lt;style&gt;#mermaid-1600873258828{font-family:&amp;ldquo;trebuchet ms&amp;rdquo;,verdana,arial;font-size:16px;fill:#333;}#mermaid-1600873258828 .error-icon{fill:#552222;}#mermaid-1600873258828 .error-text{fill:#552222;stroke:#552222;}#mermaid-1600873258828 .edge-thickness-normal{stroke-width:2px;}#mermaid-1600873258828 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-1600873258828 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-1600873258828 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-1600873258828 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-1600873258828 .marker{fill:#333333;}#mermaid-1600873258828 .marker.cross{stroke:#333333;}#mermaid-1600873258828 svg{font-family:&amp;ldquo;trebuchet ms&amp;rdquo;,verdana,arial;font-size:16px;}#mermaid-1600873258828 .label{font-family:&amp;ldquo;trebuchet ms&amp;rdquo;,verdana,arial;color:#333;}#mermaid-1600873258828 .label text{fill:#333;}#mermaid-1600873258828 .node rect,#mermaid-1600873258828 .node circle,#mermaid-1600873258828 .node ellipse,#mermaid-1600873258828 .node polygon,#mermaid-1600873258828 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-1600873258828 .node .label{text-align:center;}#mermaid-1600873258828 .node.clickable{cursor:pointer;}#mermaid-1600873258828 .arrowheadPath{fill:#333333;}#mermaid-1600873258828 .edgePath .path{stroke:#333333;stroke-width:1.5px;}#mermaid-1600873258828 .flowchart-link{stroke:#333333;fill:none;}#mermaid-1600873258828 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-1600873258828 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-1600873258828 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-1600873258828 .cluster text{fill:#333;}#mermaid-1600873258828 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:&amp;ldquo;trebuchet ms&amp;rdquo;,verdana,arial;font-size:12px;background:hsl(80,100%,96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-1600873258828:root{&amp;ndash;mermaid-font-family:&amp;ldquo;trebuchet ms&amp;rdquo;,verdana,arial;}#mermaid-1600873258828 flowchart-v2{fill:apa;}&lt;/style&gt;&lt;g transform=&#34;translate(0, 0)&#34;&gt;&lt;marker id=&#34;flowchart-pointEnd&#34; class=&#34;marker flowchart&#34; viewBox=&#34;0 0 10 10&#34; refX=&#34;9&#34; refY=&#34;5&#34; markerUnits=&#34;userSpaceOnUse&#34; markerWidth=&#34;12&#34; markerHeight=&#34;12&#34; orient=&#34;auto&#34;&gt;&lt;path d=&#34;M 0 0 L 10 5 L 0 10 z&#34; class=&#34;arrowMarkerPath&#34; style=&#34;stroke-width: 1; stroke-dasharray: 1, 0;&#34;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;marker id=&#34;flowchart-pointStart&#34; class=&#34;marker flowchart&#34; viewBox=&#34;0 0 10 10&#34; refX=&#34;0&#34; refY=&#34;5&#34; markerUnits=&#34;userSpaceOnUse&#34; markerWidth=&#34;12&#34; markerHeight=&#34;12&#34; orient=&#34;auto&#34;&gt;&lt;path d=&#34;M 0 5 L 10 10 L 10 0 z&#34; class=&#34;arrowMarkerPath&#34; style=&#34;stroke-width: 1; stroke-dasharray: 1, 0;&#34;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;marker id=&#34;flowchart-circleEnd&#34; class=&#34;marker flowchart&#34; viewBox=&#34;0 0 10 10&#34; refX=&#34;11&#34; refY=&#34;5&#34; markerUnits=&#34;userSpaceOnUse&#34; markerWidth=&#34;11&#34; markerHeight=&#34;11&#34; orient=&#34;auto&#34;&gt;&lt;circle cx=&#34;5&#34; cy=&#34;5&#34; r=&#34;5&#34; class=&#34;arrowMarkerPath&#34; style=&#34;stroke-width: 1; stroke-dasharray: 1, 0;&#34;&gt;&lt;/circle&gt;&lt;/marker&gt;&lt;marker id=&#34;flowchart-circleStart&#34; class=&#34;marker flowchart&#34; viewBox=&#34;0 0 10 10&#34; refX=&#34;-1&#34; refY=&#34;5&#34; markerUnits=&#34;userSpaceOnUse&#34; markerWidth=&#34;11&#34; markerHeight=&#34;11&#34; orient=&#34;auto&#34;&gt;&lt;circle cx=&#34;5&#34; cy=&#34;5&#34; r=&#34;5&#34; class=&#34;arrowMarkerPath&#34; style=&#34;stroke-width: 1; stroke-dasharray: 1, 0;&#34;&gt;&lt;/circle&gt;&lt;/marker&gt;&lt;marker id=&#34;flowchart-crossEnd&#34; class=&#34;marker cross flowchart&#34; viewBox=&#34;0 0 11 11&#34; refX=&#34;12&#34; refY=&#34;5.2&#34; markerUnits=&#34;userSpaceOnUse&#34; markerWidth=&#34;11&#34; markerHeight=&#34;11&#34; orient=&#34;auto&#34;&gt;&lt;path d=&#34;M 1,1 l 9,9 M 10,1 l -9,9&#34; class=&#34;arrowMarkerPath&#34; style=&#34;stroke-width: 2; stroke-dasharray: 1, 0;&#34;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;marker id=&#34;flowchart-crossStart&#34; class=&#34;marker cross flowchart&#34; viewBox=&#34;0 0 11 11&#34; refX=&#34;-1&#34; refY=&#34;5.2&#34; markerUnits=&#34;userSpaceOnUse&#34; markerWidth=&#34;11&#34; markerHeight=&#34;11&#34; orient=&#34;auto&#34;&gt;&lt;path d=&#34;M 1,1 l 9,9 M 10,1 l -9,9&#34; class=&#34;arrowMarkerPath&#34; style=&#34;stroke-width: 2; stroke-dasharray: 1, 0;&#34;&gt;&lt;/path&gt;&lt;/marker&gt;&lt;g class=&#34;root&#34;&gt;&lt;g class=&#34;clusters&#34;&gt;&lt;/g&gt;&lt;g class=&#34;edgePaths&#34;&gt;&lt;path d=&#34;M236.828125,43.871031068624106L223.126953125,49.559192557186755C209.42578125,55.247354045749404,182.0234375,66.6236770228747,168.322265625,76.47850517810402C154.62109375,86.33333333333333,154.62109375,94.66666666666667,154.62109375,98.83333333333333L154.62109375,103&#34; id=&#34;L-A-B&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-A LE-B&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M301.2421875,39.41402071241607L324.4778645833333,45.84501726034673C347.7135416666667,52.276013808277376,394.1848958333333,65.13800690413869,417.4205729166667,75.73567011873601C440.65625,86.33333333333333,440.65625,94.66666666666667,440.65625,98.83333333333333L440.65625,103&#34; id=&#34;L-A-C&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-A LE-C&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M127.52302631578948,148L122.50486567982456,152.16666666666666C117.48670504385966,156.33333333333334,107.45038377192982,164.66666666666666,102.43222313596492,173C97.4140625,181.33333333333334,97.4140625,189.66666666666666,97.4140625,193.83333333333334L97.4140625,198&#34; id=&#34;L-B-D&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-B LE-D&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M186.828125,138.8710310686241L200.529296875,144.55919255718675C214.23046875,150.2473540457494,241.6328125,161.62367702287472,255.333984375,171.47850517810403C269.03515625,181.33333333333334,269.03515625,189.66666666666666,269.03515625,193.83333333333334L269.03515625,198&#34; id=&#34;L-B-E&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-B LE-E&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M70.31599506578948,243L65.29783442982456,247.16666666666666C60.27967379385965,251.33333333333334,50.24335252192983,259.6666666666667,45.22519188596491,268C40.20703125,276.3333333333333,40.20703125,284.6666666666667,40.20703125,288.8333333333333L40.20703125,293&#34; id=&#34;L-D-F&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-D LE-F&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M124.51212993421052,243L129.53029057017542,247.16666666666666C134.54845120614036,251.33333333333334,144.58477247807016,259.6666666666667,149.6029331140351,268C154.62109375,276.3333333333333,154.62109375,284.6666666666667,154.62109375,288.8333333333333L154.62109375,293&#34; id=&#34;L-D-G&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-D LE-G&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M269.03515625,243L269.03515625,247.16666666666666C269.03515625,251.33333333333334,269.03515625,259.6666666666667,269.03515625,268C269.03515625,276.3333333333333,269.03515625,284.6666666666667,269.03515625,288.8333333333333L269.03515625,293&#34; id=&#34;L-E-H&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-E LE-H&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M413.5581825657895,148L408.5400219298246,152.16666666666666C403.5218612938597,156.33333333333334,393.48554002192986,164.66666666666666,388.46737938596493,173C383.44921875,181.33333333333334,383.44921875,189.66666666666666,383.44921875,193.83333333333334L383.44921875,198&#34; id=&#34;L-C-I&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-C LE-I&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M383.44921875,243L383.44921875,247.16666666666666C383.44921875,251.33333333333334,383.44921875,259.6666666666667,383.44921875,268C383.44921875,276.3333333333333,383.44921875,284.6666666666667,383.44921875,288.8333333333333L383.44921875,293&#34; id=&#34;L-I-J&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-I LE-J&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M467.7543174342105,148L472.7724780701754,152.16666666666666C477.7906387061403,156.33333333333334,487.82695997807014,164.66666666666666,492.84512061403507,173C497.86328125,181.33333333333334,497.86328125,189.66666666666666,497.86328125,193.83333333333334L497.86328125,198&#34; id=&#34;L-C-K&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-C LE-K&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;path d=&#34;M40.20703125,338L40.20703125,342.1666666666667C40.20703125,346.3333333333333,40.20703125,354.6666666666667,40.20703125,363C40.20703125,371.3333333333333,40.20703125,379.6666666666667,40.20703125,383.8333333333333L40.20703125,388&#34; id=&#34;L-F-L&#34; class=&#34; edge-thickness-normal edge-pattern-solid flowchart-link LS-F LE-L&#34; marker-end=&#34;url(#flowchart-pointEnd)&#34;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabels&#34;&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;edgeLabel&#34;&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(0, 0)&#34;&gt;&lt;foreignObject width=&#34;0&#34; height=&#34;0&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;edgeLabel&#34;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;nodes&#34;&gt;&lt;g class=&#34;node undefined&#34; id=&#34;A&#34; transform=&#34;translate(269.03515625, 30.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(5)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;B&#34; transform=&#34;translate(154.62109375, 125.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(4)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;C&#34; transform=&#34;translate(440.65625, 125.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(3)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;D&#34; transform=&#34;translate(97.4140625, 220.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(3)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;E&#34; transform=&#34;translate(269.03515625, 220.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(2)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;F&#34; transform=&#34;translate(40.20703125, 315.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(2)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;G&#34; transform=&#34;translate(154.62109375, 315.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(1)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;H&#34; transform=&#34;translate(269.03515625, 315.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(1)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;I&#34; transform=&#34;translate(383.44921875, 220.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(2)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;J&#34; transform=&#34;translate(383.44921875, 315.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(1)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;K&#34; transform=&#34;translate(497.86328125, 220.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(1)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;g class=&#34;node undefined&#34; id=&#34;L&#34; transform=&#34;translate(40.20703125, 410.5)&#34;&gt;&lt;rect class=&#34;basic label-container&#34; rx=&#34;5&#34; ry=&#34;5&#34; x=&#34;-32.20703125&#34; y=&#34;-22.5&#34; width=&#34;64.4140625&#34; height=&#34;45&#34;&gt;&lt;/rect&gt;&lt;g class=&#34;label&#34; transform=&#34;translate(-24.70703125, -15)&#34;&gt;&lt;foreignObject width=&#34;49.4140625&#34; height=&#34;30&#34;&gt;&lt;div xmlns=&#34;http://www.w3.org/1999/xhtml&#34; style=&#34;display: inline-block; white-space: nowrap;&#34;&gt;&lt;span class=&#34;nodeLabel&#34;&gt;fib(1)&lt;/span&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/p&gt;
&lt;h3 id=&#34;memoization&#34;&gt;Memoization&lt;/h3&gt;
&lt;p&gt;Memoization to the rescue!&lt;/p&gt;





    





&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/09/dynamic-programming-is-easy/mawwiage.gif&#34; width=&#34;500&#34; height=&#34;250&#34;&gt;
    
    
    &lt;figcaption&gt;
        &lt;p&gt;I always imagine this guy when I hear &amp;ldquo;memoization&amp;rdquo;.&lt;/p&gt;
    &lt;/figcaption&gt;
    
&lt;/figure&gt;

&lt;p&gt;Memoization is a silly sounding word for &amp;ldquo;remembering results you&amp;rsquo;ve already found&amp;rdquo;. This next version of the code uses a Python &lt;code&gt;dict&lt;/code&gt; to remember previous results. It doesn&amp;rsquo;t bother recalculating something that&amp;rsquo;s already in the cache:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{}&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;fib2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;global&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fib2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fib2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fib2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;fib2(40)&lt;/code&gt; calculates the correct result in only 40 steps (compared to over 200 million earlier).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;We just did dynamic programming.&lt;/strong&gt; Well, maybe. I personally believe this meets the definition of dynamic programming, but others say top-down recursive implementations don&amp;rsquo;t count.&lt;/p&gt;
&lt;p&gt;In the next step, I&amp;rsquo;ll convert to a bottom-up approach that &lt;em&gt;everyone&lt;/em&gt; agrees is dynamic programming.&lt;/p&gt;
&lt;h3 id=&#34;flipping-the-problem-upside-down&#34;&gt;Flipping the problem upside down&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;fib2()&lt;/code&gt; still uses a top-down recursive approach. In most instances of dynamic programming, it&amp;rsquo;s possible to solve the problem bottom up by anticipating what subproblems will need to be solved. In the case of Fibonacci, this is quite easy. To calculate the 40th number in the sequence, you need to first calculate the first 39.&lt;/p&gt;
&lt;p&gt;This version of the code drops the recursive approach and uses a list instead of a &lt;code&gt;dict&lt;/code&gt; to remember the calculated results:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;fib3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;fibs&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[]&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;fibs&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;fibs&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fibs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fibs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fibs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fib3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It&amp;rsquo;s easy to see that &lt;code&gt;fib3(40)&lt;/code&gt; only does 40 iterations, so it&amp;rsquo;s as efficient as &lt;code&gt;fib2()&lt;/code&gt;. It&amp;rsquo;s actually a bit &lt;em&gt;better&lt;/em&gt; because it uses a more efficient data structure and dispenses with the deep call stack of the recursive implementation.&lt;/p&gt;
&lt;p&gt;Finally, notice that the algorithm never looks back more than two steps, so you can get rid of the list altogether and just use two variables:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;fib4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fib4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This again requires only 40 iterations to compute &lt;code&gt;fib4(40)&lt;/code&gt;, but it has the added advantage of using only extra constant space.&lt;/p&gt;
&lt;h2 id=&#34;what-is-dynamic-programming&#34;&gt;What is dynamic programming?&lt;/h2&gt;
&lt;p&gt;Hopefully, the Fibonacci sequence example was intuitive for you and not at all scary. Now it&amp;rsquo;s time to introduce a little terminology so you can sound smart when you talk about it.&lt;/p&gt;
&lt;p&gt;There are two necessary conditions for dynamic programming to work:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Your problem must have &lt;strong&gt;optimal substructure&lt;/strong&gt;. This means that you can compute the correct result (e.g. the 40th Fibonacci number) just by computing subproblems (e.g. the 38th and 39th Fibonacci numbers). This is basically another way of saying that a recursive approach will work.&lt;/li&gt;
&lt;li&gt;Your problem must have &lt;strong&gt;overlapping subproblems&lt;/strong&gt;. This means that a simple recursive approach will do duplicate work. Think back to the call graph for the original &lt;code&gt;fib()&lt;/code&gt; function. If the same subproblem shows up multiple times, you&amp;rsquo;ll benefit from using dynamic programming.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here are the steps to apply dynamic programming:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Formulate a recursive solution.&lt;/li&gt;
&lt;li&gt;Use memoization to avoid recalculating subproblems.&lt;/li&gt;
&lt;li&gt;Use a bottom-up approach to avoid recursion.&lt;/li&gt;
&lt;li&gt;Optimize space efficiency by forgetting results you no longer need.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You&amp;rsquo;re allowed to skip steps if you already know how to get there. For example, many of you probably thought of the simple iterative approach to Fibonacci immediately.&lt;/p&gt;
&lt;h2 id=&#34;seam-carving&#34;&gt;Seam carving&lt;/h2&gt;
&lt;p&gt;In the rest of this post, I&amp;rsquo;ll walk through another dynamic programming example. This algorithm is called &amp;ldquo;seam carving&amp;rdquo;, or sometimes &amp;ldquo;content aware image resizing&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;The problem is as follows. You&amp;rsquo;re given a grid, and each cell in the grid has an associated (nonnegative) &lt;em&gt;cost&lt;/em&gt;. Your job is to find a minimum cost path from any cell in the top row to any cell in the bottom row. Each step in the path has three choices: southwest, south, or southeast. The total cost of the path is the sum of the costs of each cell in the path.&lt;/p&gt;
&lt;p&gt;That description might be hard to follow, so I&amp;rsquo;ll show you a small example:&lt;/p&gt;
&lt;iframe class=&#34;mx-auto&#34; style=&#34;width:154px;height:102px&#34; src=&#34;https://smarx.com/demos/dp/example.html&#34;&gt;&lt;/iframe&gt;
&lt;p&gt;The task is to find a path from the top row to the bottom row that&amp;rsquo;s minimal cost:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Starting with the 1, there are two options of where to go: straight down or diagonally down and to the right. The cheapest path is the latter. Total cost: 1 + 5 = &lt;strong&gt;6&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Starting with the 2, all three options are available next. The cheapest is going diagonally down and to the right. Total cost: 2 + 3 = &lt;strong&gt;5&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Starting with the 4, the cheapest path is straight down. 4 + 3 = &lt;strong&gt;7&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For now, I don&amp;rsquo;t care about the full path, just its total cost. In this simple example, the cost of the best path is &lt;strong&gt;5&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Now I&amp;rsquo;ll build a dynamic programming solution, step by step.&lt;/p&gt;
&lt;h3 id=&#34;step-1-formulate-a-recursive-solution&#34;&gt;Step 1: Formulate a recursive solution&lt;/h3&gt;
&lt;p&gt;There&amp;rsquo;s a straightforward recursive solution to find the best path given a starting cell:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]]&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;rows&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;mincost&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;# Bottom row is the base case.&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;r&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rows&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;min&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
        &lt;span class=&#34;c1&#34;&gt;# Minimum of SW, S, and SE neighbors, but skip out-of-bounds ones.&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;mincost&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;overall_min&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;min&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mincost&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Below is that solution in action. Just press &amp;ldquo;Start&amp;rdquo; and watch the (slow) magic. There&amp;rsquo;s an artificial delay of 40ms for each function call so you can follow the progress:&lt;/p&gt;
&lt;iframe class=&#34;mx-auto example-frame&#34; style=&#34;width:100%;max-width:512px;height:325px&#34; src=&#34;https://smarx.com/demos/dp/index.html#0&#34;&gt;&lt;/iframe&gt;
&lt;p&gt;A little blue number in a cell represents the total cost of the best path starting at that cell. If you&amp;rsquo;re patient enough to watch the whole thing (about 40 seconds), you&amp;rsquo;ll see the best possible path cost highlighted in green.&lt;/p&gt;
&lt;p&gt;Notice how much duplicate work is happening. The &lt;strong&gt;overlapping subproblems&lt;/strong&gt; are computed again and again.&lt;/p&gt;
&lt;p&gt;As with the Fibonacci example, I&amp;rsquo;ve counted the function calls to see just how much dynamic programming can help. This simple top-down recursive implementation uses &lt;strong&gt;1,012&lt;/strong&gt; function calls to compute the best path in a 10×5 grid.&lt;/p&gt;
&lt;h3 id=&#34;step-2-use-memoization&#34;&gt;Step 2: Use memoization&lt;/h3&gt;
&lt;p&gt;This version of the code simply stores the minimum cost for each cell as it&amp;rsquo;s computed:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{}&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;mincost2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;r&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rows&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;min&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;mincost2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I only changed a couple lines, but watch the dramatic improvement. (There&amp;rsquo;s still that 40ms delay so you can see things happen.)&lt;/p&gt;
&lt;iframe class=&#34;mx-auto example-frame&#34; style=&#34;width:100%;max-width:512px;height:325px&#34; src=&#34;https://smarx.com/demos/dp/index.html#1&#34;&gt;&lt;/iframe&gt;
&lt;p&gt;There are 50 cells in the grid, and this solution uses exactly 50 function calls. (Compare to 1,012 for the non-memoized version.)&lt;/p&gt;
&lt;p&gt;Another exponential function has turned linear with memoization!&lt;/p&gt;
&lt;h3 id=&#34;step-3-use-a-bottom-up-approach&#34;&gt;Step 3: Use a bottom-up approach&lt;/h3&gt;
&lt;p&gt;If you watch the previous example, you&amp;rsquo;ll see that the grid is filled in diagonally, revealing that the top-down approach is still in effect. In this next iteration, I&amp;rsquo;ll flip the problem upside down and compute costs from the bottom up instead.&lt;/p&gt;
&lt;p&gt;The following code computes the minimum cost for each cell in order, starting at the bottom left and ending at the top right:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Start with an empty grid.&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[[&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;None&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rows&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)]&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;# Populate the bottom row.&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][:]&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;# For each remaining row, working our way up:&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;r&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rows&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;# Find the minimum using the row below.&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;min&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;overall_min&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;min&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;iframe class=&#34;mx-auto example-frame&#34; style=&#34;width:100%;max-width:512px;height:325px&#34; src=&#34;https://smarx.com/demos/dp/index.html#2&#34;&gt;&lt;/iframe&gt;
&lt;h3 id=&#34;step-4-optimize-space-efficiency&#34;&gt;Step 4: Optimize space efficiency&lt;/h3&gt;
&lt;p&gt;Once a row has been filled with minimum costs, there&amp;rsquo;s no reason to keep any of the rows below it. In this version, just one row of values is stored, so &lt;code&gt;mcs&lt;/code&gt; becomes a 1-dimension array instead of 2-dimensional:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Start with bottom row costs.&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rows&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][:]&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;# For subsequent rows, working our way up:&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;r&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rows&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;# Find the minimum using the row below.&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;grid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;r&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;min&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cp&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cols&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;overall_min&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;min&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mcs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;iframe class=&#34;mx-auto example-frame&#34; style=&#34;width:100%;max-width:512px;height:325px&#34; src=&#34;https://smarx.com/demos/dp/index.html#3&#34;&gt;&lt;/iframe&gt;
&lt;h2 id=&#34;reading-the-whole-path&#34;&gt;Reading the whole path&lt;/h2&gt;
&lt;p&gt;I said earlier that this example is called &amp;ldquo;seam carving&amp;rdquo;. &lt;a href=&#34;https://en.wikipedia.org/wiki/Seam_carving&#34;&gt;Seam carving&lt;/a&gt; is a way to reduce the dimensions of an image in a way that keeps as much information as possible.&lt;/p&gt;
&lt;p&gt;To do this, seam carving finds &lt;strong&gt;and removes&lt;/strong&gt; a minimum cost path from top to bottom (or side to side) of an image, where the cost of each pixel is something like its gradient magnitude. Edges in the image have the highest gradient magnitude, so they will be preserved as much as possible.&lt;/p&gt;
&lt;p&gt;Removing the pixels along a vertical path reduces the width of the image by one pixel. This process is then repeated to remove more columns (or rows, using horizontal paths).&lt;/p&gt;
&lt;p&gt;Up to this point, I&amp;rsquo;ve only cared about the &lt;em&gt;cost&lt;/em&gt; of the minimum path, but now I actually need the path itself. To get that, I&amp;rsquo;ll go back to keeping the full grid around. Once the starting cell is known, it&amp;rsquo;s easy to find the full path. At each step, just look down one row and see which neighbor has the lowest minimum cost. That&amp;rsquo;s the next step in the path.&lt;/p&gt;
&lt;p&gt;This final example puts it all together and computes the minimum path efficiently:&lt;/p&gt;
&lt;iframe class=&#34;mx-auto example-frame&#34; style=&#34;width:100%;max-width:512px;height:325px&#34; src=&#34;https://smarx.com/demos/dp/index.html#4&#34;&gt;&lt;/iframe&gt;
&lt;p&gt;Trade those random numbers in for pixel gradients, and you have seam carving! Seam carving is pretty neat, so I&amp;rsquo;ll leave you with this video that shows it in action:&lt;/p&gt;

&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
  &lt;iframe src=&#34;https://www.youtube-nocookie.com/embed/6NcIJXTlugc&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; allowfullscreen title=&#34;YouTube Video&#34;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;script&gt;
    (function() {
        const iframes = document.getElementsByClassName(&#34;example-frame&#34;);
        for (let i = 0; i &lt; iframes.length; i++) {
            const iframe = iframes[i];
            iframe.onload = function () {
                iframe.style.height = iframe.contentWindow.document.body.scrollHeight + &#34;px&#34;;
            }
        }
    })();
&lt;/script&gt;
</description>
    </item>
    
  </channel>
</rss>
