{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/dynamicProgramming","result":{"data":{"blog":{"frontmatter":{"title":"DYNAMIC PROGRAMMING","thumbnail":"blog69","date":"June 4, 2021","dsaCppCodeFile":null},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Dynamic programming is a technique for solving problems with\n                  overlapping sub problems. Typically, these sub problems arise\n                  from a recurrence relating a given problem’s solution to\n                  solutions of its smaller sub problems.\n                </p>\n                <p>\n                  Rather than solving overlapping sub problems again and again,\n                  dynamic programming suggests solving each of the smaller\n                  sub problems only once and recording the results in a table\n                  from which a solution to the original problem can then be\n                  obtained.\n                </p>\n                <p>\n                  The difference between Dynamic Programming and straightforward\n                  recursion is in memoization of recursive calls. If the sub\n                  problems are independent and there is no repetition then\n                  memoization does not help.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>EXAMPLE: Fibonacci Series</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Using Recursion: Fib(n)</strong>\n                  <ul class=\"pl-4\">\n                    <li>If n==0, return 0.</li>\n                    <li>If n==1, return 1.</li>\n                    <li>return fib(n-1) + fib(n-2).</li>\n                  </ul>\n                  <p>\n                    In this method, the recursive call is made only if n is\n                    neither 1 not 0. So there occurs a situation where recursive\n                    calls has to be made for same value of n but at different\n                    instance.\n                    <br />\n                    Suppose we calculate Fib(4): <strong>(i)</strong> The\n                    recursive calls made in root function are - Fib(3) and\n                    Fib(2). <strong>(ii)</strong> The recursive calls made in\n                    Fib(3) are - Fib(2) and Fib(1). <strong>(iii)</strong> The\n                    recursive calls made in Fib(2) are - Fib(1) and Fib(0).\n                    <br />\n                    Notice that the the recursive call for n=2 is made twice\n                    here and this situation occurs for other values of n also.\n                    The simple solution is to memoize the newly calculated value\n                    for n as shown in next step.\n                  </p>\n                </li>\n                <li>\n                  <strong>Using DP</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      <strong>Top-Down DP: Fib(n)</strong>\n                      <ul class=\"pl-4\">\n                        <span class=\"text-muted\"\n                          >Create a global integer array memo[n].</span\n                        >\n                        <li>if memo[n]!=NULL, return memo[n].</li>\n                        <li>If n==0, return 0.</li>\n                        <li>If n==1, return 1.</li>\n                        <li>memo[n] = fib(n-1) + fib(n-2).</li>\n                        <li>return memo[n].</li>\n                      </ul>\n                      <p>\n                        Here, we see that if memo[n] is not null then it returns\n                        the value stored in it which prevents from making a new\n                        recursive call.\n                        <br />\n                        It is top-down dp because the problem is broken into sub\n                        problems, each of these sub problems is solved, and the\n                        solutions are remembered in case they need to be solved\n                        again.\n                      </p>\n                    </li>\n                    <li>\n                      <strong>Bottom-Up DP: Fib(n)</strong>\n                      <ul class=\"pl-4\">\n                        <li>Let memo[n+1] be an integer array.</li>\n                        <li>Initially, memo[0] = 0 and memo[1] = 1.</li>\n                        <li>Loop from i=2 to n.</li>\n                        <li>memo[i] = memo[i-1] + memo[i-2].</li>\n                        <li>return memo[n].</li>\n                      </ul>\n                      <p>\n                        Here, we see that if memo[n] is calculated just by\n                        adding the previous two integers stored in that array.\n                        <br />\n                        It is bottom-up dp because these methods start with\n                        lower values of input and keep building the solutions\n                        for higher values.\n                      </p>\n                    </li>\n                  </ol>\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/warshallsAlgorithm\">Warshall's Algorithm</a> |\n              <a href=\"/blogs/floydsAlgorithm\">Floyd's Algorithm</a> |\n              <a href=\"/blogs/knapsackProblem\">Knapsack problem</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Dynamic programming is a technique for solving problems with\n                  overlapping sub problems. Typically, these sub problems arise\n                  from a recurrence relating a given problem’s solution to\n                  solutions of its smaller sub problems.\n                </p>\n                <p>\n                  Rather than solving overlapping sub problems again and again,\n                  dynamic programming suggests solving each of the smaller\n                  sub problems only once and recording the results in a table\n                  from which a solution to the original problem can then be\n                  obtained.\n                </p>\n                <p>\n                  The difference between Dynamic Programming and straightforward\n                  recursion is in memoization of recursive calls. If the sub\n                  problems are independent and there is no repetition then\n                  memoization does not help.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>EXAMPLE: Fibonacci Series</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Using Recursion: Fib(n)</strong>\n                  <ul class=\"pl-4\">\n                    <li>If n==0, return 0.</li>\n                    <li>If n==1, return 1.</li>\n                    <li>return fib(n-1) + fib(n-2).</li>\n                  </ul>\n                  <p>\n                    In this method, the recursive call is made only if n is\n                    neither 1 not 0. So there occurs a situation where recursive\n                    calls has to be made for same value of n but at different\n                    instance.\n                    <br />\n                    Suppose we calculate Fib(4): <strong>(i)</strong> The\n                    recursive calls made in root function are - Fib(3) and\n                    Fib(2). <strong>(ii)</strong> The recursive calls made in\n                    Fib(3) are - Fib(2) and Fib(1). <strong>(iii)</strong> The\n                    recursive calls made in Fib(2) are - Fib(1) and Fib(0).\n                    <br />\n                    Notice that the the recursive call for n=2 is made twice\n                    here and this situation occurs for other values of n also.\n                    The simple solution is to memoize the newly calculated value\n                    for n as shown in next step.\n                  </p>\n                </li>\n                <li>\n                  <strong>Using DP</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      <strong>Top-Down DP: Fib(n)</strong>\n                      <ul class=\"pl-4\">\n                        <span class=\"text-muted\"\n                          >Create a global integer array memo[n].</span\n                        >\n                        <li>if memo[n]!=NULL, return memo[n].</li>\n                        <li>If n==0, return 0.</li>\n                        <li>If n==1, return 1.</li>\n                        <li>memo[n] = fib(n-1) + fib(n-2).</li>\n                        <li>return memo[n].</li>\n                      </ul>\n                      <p>\n                        Here, we see that if memo[n] is not null then it returns\n                        the value stored in it which prevents from making a new\n                        recursive call.\n                        <br />\n                        It is top-down dp because the problem is broken into sub\n                        problems, each of these sub problems is solved, and the\n                        solutions are remembered in case they need to be solved\n                        again.\n                      </p>\n                    </li>\n                    <li>\n                      <strong>Bottom-Up DP: Fib(n)</strong>\n                      <ul class=\"pl-4\">\n                        <li>Let memo[n+1] be an integer array.</li>\n                        <li>Initially, memo[0] = 0 and memo[1] = 1.</li>\n                        <li>Loop from i=2 to n.</li>\n                        <li>memo[i] = memo[i-1] + memo[i-2].</li>\n                        <li>return memo[n].</li>\n                      </ul>\n                      <p>\n                        Here, we see that if memo[n] is calculated just by\n                        adding the previous two integers stored in that array.\n                        <br />\n                        It is bottom-up dp because these methods start with\n                        lower values of input and keep building the solutions\n                        for higher values.\n                      </p>\n                    </li>\n                  </ol>\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/warshallsAlgorithm\">Warshall's Algorithm</a> |\n              <a href=\"/blogs/floydsAlgorithm\">Floyd's Algorithm</a> |\n              <a href=\"/blogs/knapsackProblem\">Knapsack problem</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M94%2095l-1%207v6l-1-6c-1-8-3-9-6-3l-1%204-1-4c-1-2-2-4-4-4s-3%203-3%2011v6l-1-5c-2-11-3-12-6-12-2%201-5%206-7%2014-1%203%202%204%205%202%202-3%203-2%203%200%201%202%201%202%205%202l5-1%201-2h1l2%201%201-1h1l1%202h4c4%201%205%200%205-3v-2l2%202c1%203%204%204%207%203%203-2%205-5%203-6h-2l-3%202v-9c2-1%202-1%202%201l2%202c3%200%203-2%202-5-2-4-11-2-11%203l-1-2c0-3-4-5-4-3m55%200c-4%203-5%2012-3%2015s7%203%209%200c6-7%200-19-6-15m83%200l-2%208-1%208v-8c0-7%200-8-2-8l-3%203-2%203-2-3c-2-5-5-4-5%202v6l-1-5c-1-7-4-8-6-2l-2%204-1-4c0-3-1-4-3-4h-3v8c0%208-1%208-1%205-2-10-3-12-6-13-2%200-3%200-5%206-4%2010-4%2011-1%2011l3-1c1-3%204-2%204%200%201%202%202%202%209%201l1-2h5c1-2%201-2%201%200s1%202%205%202%205%200%205-2h1c0%202%202%201%204-1v1l1%202h8l7%201v-8l1%203c2%205%207%206%207%201v-2l1%202c2%205%209%204%2011-1%201-4%200-5-3-5s-3%200-3%203v2-7c0-2%202-2%202%200%200%201%204%201%204-1%201-2-1-5-4-5s-7%203-7%207c-1%201-1%200-1-2%200-4%200-5-2-5-1%200-2%201-2%203l-1%203-1-3c-2-4-6-4-6%200-1%203-1%203-1%200%200-4-1-5-3-3M24%2096l-1%209v8l3-1c10%200%2012-17%202-17l-4%201m12%200l1%206%202%207%201%203c1%201%203%200%203-2l3-7c3-7%203-8%200-8l-3%203-2%203-1-3c-1-3-3-4-4-2m14%200l-1%209c0%207%200%208%202%208%201%200%202-1%202-4l1-4%201%203c1%203%202%204%204%204s2%200%202-6l1-9c0-2%200-2-2-2-3%200-3%200-3%203v3l-1-3c-2-2-5-4-6-2m70%200l-1%209c0%207%200%208%202%208%201%200%202-1%202-3s1-3%203-4c7-2%206-11-1-11l-5%201m13%200l-1%209c-1%207%200%207%202%207l2-2%201-3%202%203c1%202%202%202%204%201V96h-10m27%202c-3%204-3%2012%201%2014%203%201%206%200%208-3l1-2v2c0%202%200%203%202%203s3-1%203-3l1-2%201%202c1%203%203%204%205%202%201-1%200-2-1-3-1-2-1-3%201-4%203-5%200-9-6-9l-4%201-1%205-1%204c-1-2-6-3-6%200v2l-1%201c-2%200-1-5%200-8h2c0%203%204%202%205%200%201-5-6-7-10-2m139%208l-1%208v7h9c8%200%209%201%209%203s0%202-2%202c-5-1-16%2013-21%2026l-3%2020%206-7c4-5%206-7%2010-8%206-3%206-2%206%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%205-17-1-20l-3-2-1-1-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%207v7h22v-3l1-3v-1l-1-3v-3h-22v6m-31%2028l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/f257fa54762d480400eba7ac7fefd6ab/ee604/blog69.png","srcSet":"/static/f257fa54762d480400eba7ac7fefd6ab/69585/blog69.png 200w,\n/static/f257fa54762d480400eba7ac7fefd6ab/497c6/blog69.png 400w,\n/static/f257fa54762d480400eba7ac7fefd6ab/ee604/blog69.png 800w,\n/static/f257fa54762d480400eba7ac7fefd6ab/f3583/blog69.png 1200w,\n/static/f257fa54762d480400eba7ac7fefd6ab/e4d72/blog69.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"dynamicProgramming","thumbnail":"thumbnails/blog69.png"}},"staticQueryHashes":["2987289216","63159454"]}