Basics of an Algorithm and Different Algorithmic Approaches
Published: 2021-01-19 09:12:37

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.cse.buffalo.edu%2F~rapaport%2F510%2Falg-dilbert.gif&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center">The information in this blog contains,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Page 1: What do you mean by an Algorithm, Time complexity, Space complexity, Orders of growth, Asymptotic Notations, Approach to solve recursive and non recursive algorithms.</li><li>Page 2: Divide and Conquer, Applications of Divide and Conquer, Greedy Method, Applications of Greedy Method, Dynamic Method, Principle of Optimality and Applications of Dynamic Method.</li><li>Page 3: Backtracking Method, Applications of Backtracking, Branch and Bound, Applications of Branch and bound, NP Complete and NP Hard problems, GitHub repository with the code .</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center">Introduction:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>An algorithm is a set of instructions that produces an output or a result.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It tells the system what to do in order to achieve the desired result. It may not know what the result is beforehand, but it knows that it wants one.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3028,"width":348,"height":193,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-18.png?w=468" alt="" class="wp-image-3028" width="348" height="193"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>An example for an algorithm, will be cooking a cake. We have our desired output which is a cake, now we have to follow certain set of instructions to make a cake.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="http://programmerprodigycode.files.wordpress.com/2020/12/e28e4-iu.gif" alt=""/></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4><strong>There are two kinds of efficiency:</strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>Time Efficiency/Complexity: Indicates how fast an algorithm in question runs.</li><li>Space Efficiency/Complexity: Refers to the amount of memory units required by the algorithm in addition to the space needed for its input and output.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Since we are after a measure of an algorithm’s efficiency, we would like to have a metric that does not depend on these extraneous factors.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>We focus on Basic operation, The operation contributing the most to the total running time, and compute the number of times the basic operation is executed.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Algorithm’s time efficiency suggests measuring it by counting the number of times the algorithm’s basic operation is executed on inputs of size n. Running time T (n) of a program implementing this algorithm on that computer by the formula</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3031,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-19.png?w=234" alt="" class="wp-image-3031"/></figure></div>
<!-- /wp:image -->

<!-- wp:list {"ordered":true} -->
<ol><li>cop be the execution time of an algorithm’s basic operation on a particular computer, </li><li>C(n) be the number of times this operation needs to be executed for this algorithm.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Orders of Growth:</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p> When comparing two algorithms that solve the same problem, if the input size is very less–</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>It is difficult to distinguish the time complexities</li><li>Not possible to choose efficient algorithm</li></ol>
<!-- /wp:list -->

<!-- wp:image {"id":3034,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-20.png?w=742" alt="" class="wp-image-3034"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>2^n and the factorial function n! Both these functions grow so fast that their values become astronomically large even for rather small values of n.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The efficiencies of some algorithms may differ significantly for inputs of the same size. For such algorithms, we need to distinguish between the worst-case, average-case, and best-case efficiencies.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>let us consider linear search example</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">SequentialSearch(A[],K)
//Searches for a given value in a given array by linear search
//Input: An array A[] and a search key K
//Output: The index of the first element in A that matches k or -1 if there are no matching elements

int i=0;
while i&lt; n and A[i] !=k do
 i=i+1;
if i&lt;n return i
else return -1</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="http://programmerprodigycode.files.wordpress.com/2020/12/29860-iu.jpg" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>1.Worst case: No match</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Analyze the algorithm to see what kind of inputs yield the largest value of the basic operation’s count C(n) among all possible inputs of size n and then compute this worst-case value.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3041,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-21.png?w=203" alt="" class="wp-image-3041"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>2.Best case: Very first match</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For the best-case input of size n, which is an input (or inputs) of size n for which the algorithm runs the fastest among all possible inputs of that size.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3042,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-22.png?w=168" alt="" class="wp-image-3042"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>Worst-case efficiency is very important than Best-case efficiency</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>3.Average case: For average case efficiency assumptions are needed</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The standard assumptions for linear search are that</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The probability of a successful search is equal to p (0 ≤ p ≤ 1) and</li><li>The probability of the first match occurring in the ith position of the list is the same for every i. </li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>In the case of a successful search, the probability of the first match occurring in the ith position of the list is p/n for every i. In the case of an unsuccessful search, the number of comparisons will be n with the probability of such a search being (1 -p)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>1.For example, if p = 1 (the search must be successful)</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The average number of key comparisons made by sequential search is (n + 1)/2; that is, the algorithm will inspect, on average, about half of the list’s elements.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>2.If p = 0 (the search must be unsuccessful)</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The average number of key comparisons will be n because the algorithm will inspect all n elements on all such inputs.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>The framework’s primary interest lies in the order of growth of the algorithm’s running time(extra memory units consumed)as its is input size goes to infinity.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Asymptotic Notations:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>we are interested in, t(n) will be an algorithm’s running time( usually indicated by its basic operation countC(n), and g(n) will be some simple function to compare the count with.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>1.O(big oh) Notation:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A function t(n) is said to be in O(g(n)), denoted t(n) ∈O(g(n)), if t(n) is bounded above by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3048,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-23.png?w=378" alt="" class="wp-image-3048"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>big-O notation for asymptotic upper bounds</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>2.Ω(big omega) Notation:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈Ω(g(n)), if t(n) is bounded below by some positive constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3050,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-24.png?w=302" alt="" class="wp-image-3050"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>big-Ω notation for asymptotic lower bound</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>3.θ(big theta) Notation:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A function t(n) is said to be in θ(g(n)), denoted t(n)∈θ(g(n)), if t(n) is bounded both above and below by some positive constant multiples of g(n) for all large n, i.e.,if there exist some positive constants c1 and c2 and some non negative integer n0 such that,</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3053,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-25.png?w=406" alt="" class="wp-image-3053"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>big-Θ notation, an asymptotically tight bound on the running time</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.dotnetlovers.com%2Fimages%2Fcoolnikhilj2256c883d1-b9fc-46e9-b225-588ac5063c3d.png%3F1%2F24%2F2016%25202%3A56%3A15%2520AM&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3><strong>Non-recursive and recursive algorithms</strong>:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>1.<strong>General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Decide on a parameter (or parameters) indicating an input’s size.</li><li>Identify the algorithm’s basic operation. (As a rule, it is located in the innermost loop.)</li><li>Check whether the number of times the basic operation is executed depends only on the size of an input. If it also depends on some additional property, the worst-case, average-case, and, if necessary, best-case efficiencies have to be investigated separately.</li><li>Set up a sum expressing the number of times the algorithm’s basic operation is executed.</li><li>Using standard formulas and rules of sum manipulation, either find a closed form formula for the count or, at the very least, establish its order of growth.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>For example, finding Largest element in a list</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">MaxElement(A[])
//Determines the value of the largest element in a given array
// Input: An array A[] of real numbers
// Output: The value of the largest element in A
maxal =A[0]
for i &lt;- 1 to n-1 do
 if A[i} >maxval
  maxval=A[i]
return maxval
</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:list {"ordered":true} -->
<ol><li>C(n) the number of times this comparison is executed </li><li>The algorithm makes one comparison on each execution of the loop, which is repeated for each value of the loop’s variable i within the bounds 1 and n -1, inclusive </li><li>Therefore, we get the following sum for C(n)</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","id":3061,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-28.png?w=262" alt="" class="wp-image-3061"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>2.<strong>General Plan for Analyzing the Time Efficiency of Recursive Algorithms</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Decide on a parameter (or parameters) indicating an input’s size.</li><li>Identify the algorithm’s basic operation. </li><li>Check whether the number of times the basic operation is executed can vary on different inputs of the same size; if it can, the worst-case, average-case, and best-case efficiencies must be investigated separately.</li><li>Set up a recurrence relation, with an appropriate initial condition, for the number of times the basic operation is executed.</li><li>Solve the recurrence or, at least, ascertain the order of growth of its solution.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>For example: Factorial function</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">Algorithm Factorial(n)
//Computes n! recursively
//Input: a non negative integer n
//Output: The value of n!

if n=0 return 1
else return F(n-1) *n</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:image {"id":3072,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-32.png?w=446" alt="" class="wp-image-3072"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Compute the factorial function F (n) = n! for an arbitrary nonnegative integer n.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Another example of Recursive algorithm would be Towers of Hanoi.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","width":506,"height":379,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fi.pinimg.com%2F736x%2Fc4%2F7c%2Fc4%2Fc47cc4bcf9577f6f361f22b92a2fa111.jpg&amp;f=1&amp;nofb=1" alt="" width="506" height="379"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>One should be careful with recursive algorithms because their succinctness may mask their inefficiency.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:nextpage -->
<!--nextpage-->
<!-- /wp:nextpage -->

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center">Divide and Conquer:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Divide-and-conquer algorithms work according to the following general plan: </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>A problem is divided into several sub problems of the same type, ideally of about equal size.</li><li>The sub problems are solved (typically recursively, though sometimes a different algorithm is employed, especially when sub problems become small enough).</li><li>If necessary, the solutions to the sub problems are combined to get a solution to the original problem.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","width":517,"height":337,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="http://programmerprodigycode.files.wordpress.com/2020/12/27c1b-iu.png" alt="" width="517" height="337"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Recurrence relation for the general divide and conquer recurrence:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>In the most typical case of divide-and-conquer a problem’s instance of size n is divided into two instances of size n/2. </li><li>More generally, an instance of size n can be divided into b instances of size n/b, with a of them needing to be solved. (Here, a and b are constants; a ≥ 1 and b &gt; 1.)</li><li>Assuming that size n is a power of b to simplify our analysis, we get the following recurrence for the running time T (n):</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","id":3066,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-29.png?w=233" alt="" class="wp-image-3066"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>The time complexity of recurrence relations is found by master theorem,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":3069,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-31.png?w=793" alt="" class="wp-image-3069"/><figcaption>d is power of n in f(n).</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Some examples of master theorem solutions,</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="http://programmerprodigycode.files.wordpress.com/2020/12/e4f12-iu.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>For more information,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a href="https://courses.missouristate.edu/anthonyclark/325/lectures/07-master-theorem.pdf" target="_blank" rel="noreferrer noopener">https://courses.missouristate.edu/anthonyclark/325/lectures/07-master-theorem.pdf</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Time complexity of below algorithms is also calculated by master theorem</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a href="https://algotree.org/algorithms/sorting/mergesort/" target="_blank" rel="noreferrer noopener">Merge Sort</a></li><li><a href="https://algotree.org/algorithms/sorting/quicksort/" target="_blank" rel="noreferrer noopener">Quick Sort</a></li><li><a href="https://algotree.org/algorithms/binary_search/" target="_blank" rel="noreferrer noopener">Binary search</a></li><li><a href="https://www.cs.princeton.edu/~wayne/teaching/multiply.pdf" target="_blank" rel="noreferrer noopener">Multiplication of large integers</a></li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center">Greedy Method:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A greedy algorithm is an algorithm that constructs an object X one step at a time, at each step choosing the locally best option. In some cases, greedy algorithms construct the globally best object by repeatedly choosing the locally best option.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Greedy algorithms aim for global optimal by iteratively making a locally optimal decision. Solves problems in a Top-Down approach</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Greedy algorithms have several advantages over other algorithmic approaches:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Simplicity</strong>: Greedy algorithms are often easier to describe and code up than other algorithms.</li><li><strong>Efficiency</strong>: Greedy algorithms can often be implemented more efficiently than other algorithms.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Greedy algorithms have several drawbacks:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Hard to design</strong>: Once you have found the right greedy approach, designing greedy algorithms can be easy. However, finding the right approach can be hard.</li><li><strong>Hard to verify</strong>: Showing a greedy algorithm is correct often requires a nuanced argument.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For example, If we have to travel from V1 to V7</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/10/image-7.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Then according to Greedy algorithm, we will consider local optimum path which will be. </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>V1 to V6 = 4 </li><li>V6 toV5 = 15</li><li>V5 to V4 =10 </li><li>V4 to V3 =6 </li><li>V3 to V2 =8</li><li>V2 to V7 =7</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4><strong>Assemble an optimal solution to a problem</strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>Making locally optimal (or greedy) choices</li><li>At each step, we make the choice that looks best in the current problem</li><li>We don’t consider results from different sub problems</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3 id="when-do-we-use-greedy-algorithms">When do we use Greedy Algorithms?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>All the greedy problems share a common property that a local optima can eventually lead to a global minima without reconsidering the set of choices already considered.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>The only problem with them is that you might come up with the correct solution but you might not be able to verify if its the correct one.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Applications of Greedy method</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":5} -->
<h5>An application of of Greedy Algorithm is the Coin Change problem</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Given currency denominations: 1, 5, 10, 25, 100, devise a method to pay amount to customer using fewest number of coins.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Our task is to use these coins to form a sum of money using the minimum (or optimal) number of coins. Also, we can assume that a particular denomination has an infinite number of coins</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3097,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-33.png?w=392" alt="" class="wp-image-3097"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>At each iteration, add coin of the largest value that does not take us past the amount to be paid.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">Coin_Change(x,Coins[])
// Sort n coin denominations so that  c1&lt; c2 ...
S = []
    # Iterate through coins
    for coin in denomination_list:
        # Add current coin as long as not exceeding ampoiunt
        while amount:
            if coin &lt;= amount:
                change.append(coin)
                amount -= coin
            else:
                break
    return change</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>Problem reduces to coin-changing x–ck cents, which, by induction,is optimally solved by cashier's algorithm.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3099,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-34.png?w=533" alt="" class="wp-image-3099"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>It may not even lead to a feasible solution every time, as if c1 &gt; 1: 7, 8, 9.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Coin change's algorithm: 15¢ = 9 + ???.</li><li>Optimal: 15¢ = 7 + 8.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":5} -->
<h5>An application of of Greedy Algorithm is the knapsack problem</h5>
<!-- /wp:heading -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fimage.slidesharecdn.com%2Fknapsackproblem-achchuthan-140506120412-phpapp02%2F95%2Fgreedy-knapsack-problem-by-y-achchuthan-2-638.jpg%3Fcb%3D1399378079&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>For solving the Fractional knapsack problem, we calculate the value of an item/ weight of an item ratio. Then we simply pick the best value to weight ratio items.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Take as much of the article with the highest value per pound (<em>vi/wi</em>) as possible</li><li>If the article with the highest value is over, take the next article with the highest value (<em>vi/wi</em>) </li><li>Continue until the sack is full</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","width":584,"height":438,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fimage.slidesharecdn.com%2Fgreedy-171108085921%2F95%2Fgreedy-method-4-638.jpg%3Fcb%3D1510131739&amp;f=1&amp;nofb=1" alt="" width="584" height="438"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Greedy algorithms is applied in the following algorithms,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-design-and-analysis-of-algorithms-spring-2015/lecture-notes/MIT6_046JS15_lec12.pdf" target="_blank">Prim’s algorithm</a></li><li><a rel="noreferrer noopener" href="https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-design-and-analysis-of-algorithms-spring-2015/lecture-notes/MIT6_046JS15_lec12.pdf" target="_blank">Kruskal’s algorithm</a></li><li><a rel="noreferrer noopener" href="https://algotree.org/algorithms/single_source_shortest_path/" target="_blank">Dijkstra’s algorithm</a></li><li>Huffman trees</li><li>Job sequencing with deadlines/activity selection problem</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For more information,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="https://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/13/Small13.pdf" target="_blank">https://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/13/Small13.pdf</a></li><li><a rel="noreferrer noopener" href="https://www2.seas.gwu.edu/~ayoussef/cs6212/greedy.html" target="_blank">https://www2.seas.gwu.edu/~ayoussef/cs6212/greedy.html</a></li><li><a href="https://www.cs.csustan.edu/~john/Classes/CS4440/Notes/04_GreedyAlgs/04GreedyAlgorithmsI.pdf" target="_blank" rel="noreferrer noopener">https://www.cs.csustan.edu/~john/Classes/CS4440/Notes/04_GreedyAlgs/04GreedyAlgorithmsI.pdf</a></li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center">Dynamic Programming:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Dynamic Programming is a general approach to solving problems, much like “divide-and-conquer” is a general method, except that unlike divide-and-conquer, the sub problems will typically overlap and will not effect each other overlap.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Commonly used in optimization. </li><li>When solving sub problems, try them all, but choose the best one.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center","level":4} -->
<h4 class="has-text-align-center"><strong>Basic Idea:</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What we want to do is take our problem and somehow break it down in to a reasonable number of sub problems in such a way that we can use optimal solutions to the smaller sub problems to give us optimal solutions to the larger ones. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Unlike divide-and-conquer  it is OK if our sub problems overlap, so long as there are not too many of them.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>Dynamic Programming results in an efficient algorithm, if the following conditions hold:</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The optimal solution can be produced by combining optimal solutions of subproblems;</li><li>The optimal solution of each subproblem can be produced by combining optimal solutions of sub-subproblems, etc;</li><li>The total number of subproblems arising recursively is polynomial.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4><strong><em>Steps for Solving DP Problems</em></strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>Define subproblems</li><li>Write down the recurrence that relates sub problems</li><li>Recognize and solve the base cases</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Usually works when the obvious Divide&amp;Conquer algorithm results in an exponential running time</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4><em><strong>Solutions for Dynamic Programming:</strong></em></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>Find the recursion in the problem.</li><li>Top-down: store the answer for each subproblem in a table to avoid having to recompute them.</li><li>Bottom-up: Find the right order to evaluate the results so that partial results are available when needed.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>Implementation Trick:</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Remember (memoize) previously solved “subproblems”; e.g., in Fibonacci, we memo-ized the solutions to the subproblemsF0,F1,···,Fn−1, while unraveling the recursion.</li><li>if we encounter a subproblem that has already been solved, re-use solution.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong><em>Runtime≈Number of subproblems * time/subproblem</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let us take a few examples,</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>1.Factorial function:</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's take the simple example of the Fibonacci numbers: finding the n<sup> th</sup> Fibonacci number defined by</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub> and F<sub>0</sub> = 0, F<sub>1</sub> = 1</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--2E7cm862--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_auto%252Cw_880%2Fhttp%3A%2F%2Fankursheel.com%2Fwp-content%2Fuploads%2F2017%2F03%2FAlgorithms-F6CallTree.png&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>1.Recursion:</strong> The obvious way to do this is recursive, so for every time we find f(3) value three different points,</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">def fibonacci(n):
    if n == 0:
        return 0
    if n == 1:
        return 1

    return fibonacci(n - 1) + fibonacci(n - 2)
</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p><strong>2.Top Down -Memoization:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The recursion does a lot of unnecessary calculations because a given Fibonacci number will be calculated multiple times. So, instead of calculating value of f(3)  value three different times, we store value of f(3) in cache. So, we can easily retrieve it, </p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">cache = {}

def fibonacci(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    if n in cache:
        return cache[n]

    cache[n] = fibonacci(n - 1) + fibonacci(n - 2)

    return cache[n]
</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>The  memoization does take extra space to store the cache values. So we compensate space complexity with time complexity.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>3.Bottom-up - Iterative:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A better way to do this is to get rid of the recursion all-together by using iterative procedure, this way we don't have to store extra values or take time to find the value. </p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">cache = {}

def fibonacci(n):
    cache[0] = 0
    cache[1] = 1

    for i in range(2, n + 1):
        cache[i] = cache[i - 1] +  cache[i - 2]

    return cache[n]</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>So in terms of complexity Iterative &gt; Memoization &gt; Recurstion</strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Principle of Optimality:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A key idea is that optimization over time can often be regarded as ‘optimization in stages’. We trade off our desire to obtain the lowest possible cost at the present stage against the implication this would have for costs at future stages.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The best action minimizes the sum of the cost incurred at the current stage and the least total cost that can be incurred from all subsequent stages, consequent on this decision.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>For example, If we have to travel from V1 to V7</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/10/image-7.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Then according to Principle of optimality, we will consider global optimum path instead of local optimum path,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>V1 to V2</li><li>V2 to V7</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Applications of Dynamic method</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":5} -->
<h5><strong>An application of Dynamic Algorithm is the Coin change</strong></h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To make change for n cents, we are going to figure out how to make change for every value x &lt; n first. We then buildup the solution out of the solution for smaller values</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3097,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-33.png?w=392" alt="" class="wp-image-3097"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>We will only concentrate on computing the number of coins. </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Let C[p] be the minimum number of coins needed to make change for p cents.</li><li>Let x be the value of the first coin used in the optimal solution.</li><li>Then C[p] = 1 +C[p−x]</li></ol>
<!-- /wp:list -->

<!-- wp:image {"id":3119,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-35.png?w=580" alt="" class="wp-image-3119"/></figure>
<!-- /wp:image -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">DP-Change(n)
 C[&lt;0] =∞
 C[0] = 0
 for p= 2 t o n 
  do min =∞
   for i= 1 to k 
    do if(p≥di)
     then if(C[p−di]) + 1&lt;min)
      thenmin=C[p−di] + 1 
      coin=i
  C[p] =min
  S[p] =coin</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>Instead of going for local largest coin to return at every iteration,  We rather focus on minimizing the number of coins returned.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>It may not leads to a feasible solution every time, as if c1 &gt; 1: 7, 8, 9.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Dynamic Coin change's algorithm: 15¢ = 7 + 8</li><li>Greedy Coin change's algorithm:15¢ = 9 + ?.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":5} -->
<h5>An application  of Dynamic Algorithm is the knapsack problem</h5>
<!-- /wp:heading -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fimage.slidesharecdn.com%2Fknapsackproblem-achchuthan-140506120412-phpapp02%2F95%2Fgreedy-knapsack-problem-by-y-achchuthan-2-638.jpg%3Fcb%3D1399378079&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>For solving the 0/1 knapsack problem, we calculate the value of an item/ weight of an item ratio. Then we simply check for the optimum  solution from all the combination of itmes.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Take as much of the article with the highest value per pound (<em>vi/wi</em>) as possible</li><li>Check the maximum value/weight for all the combination of items. </li><li>Continue until the sack is full</li></ol>
<!-- /wp:list -->

<!-- wp:image {"id":3129,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-36.png?w=1024" alt="" class="wp-image-3129"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"textAlign":"center","level":4} -->
<h4 class="has-text-align-center">Difference between Greedy algorithm and Dynamic algorithm</h4>
<!-- /wp:heading -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fpediaa.com%2Fwp-content%2Fuploads%2F2019%2F01%2FDifference-Between-Greedy-Method-and-Dynamic-Programming-Comparison-Summary.jpg&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>More applications of Dynamic programming are,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="https://prezi.com/8cbnfn6cybrq/csc210-25-dynamic-programming-warshalls-and-floyds-algorithms/" target="_blank">Warshal and Floyd Algorithm</a></li><li><a rel="noreferrer noopener" href="https://www.radford.edu/~nokie/classes/360/dp-opt-bst.html" target="_blank">Optimal Binary Search Trees</a></li><li><a href="https://cse442-17f.github.io/Traveling-Salesman-Algorithms/" target="_blank" rel="noreferrer noopener">Traveling salesperson problem</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For more information,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="https://cs.lmu.edu/~ray/notes/dynamicprogramming/" target="_blank">https://cs.lmu.edu/~ray/notes/dynamicprogramming/</a></li><li><a rel="noreferrer noopener" href="https://www.youtube.com/channel/UCxX9wt5FWQUAAz4UrysqK9A" target="_blank">https://www.youtube.com/channel/UCxX9wt5FWQUAAz4UrysqK9A</a></li><li><a rel="noreferrer noopener" href="https://www.cs.cmu.edu/~avrim/451f09/lectures/lect1001.pdf" target="_blank">https://www.cs.cmu.edu/~avrim/451f09/lectures/lect1001.pdf</a></li></ol>
<!-- /wp:list -->

<!-- wp:nextpage -->
<!--nextpage-->
<!-- /wp:nextpage -->

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center">Backtracking</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Backtracking</strong> is an approach to solving <strong>constraint-satisfaction problems</strong> without trying all possibilities.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Backtracking consists of doing a DFS of the state space tree, checking whether each node is promising and if the node is non promising backtracking to the node’s parent.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","width":443,"height":305,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fhe-s3.s3.amazonaws.com%2Fmedia%2Fuploads%2F4b2bba0.jpg&amp;f=1&amp;nofb=1" alt="" width="443" height="305"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>Suppose you have to make a series of decisions,among various choices, where</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>You don’t have enough information to know what to choose</li><li>Each decision leads to a new set of choices</li><li>Some sequence of choices (possibly more than one) may be a solution to your problem</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Back tracking is a methodical way of trying out various sequences of decisions, until you find one that “works”</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Backtracking is easily implemented with recursion because:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The run-time stack takes care of keeping track of the choices that got us to a given point.</li><li>Upon failure we can get to the previous choice simply by returning a failure code from the recursive call</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>An real life example of this would be Solving a maze,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Given a maze, find a path from start to finish</li><li> At each intersection, you have to decide between three or fewer choices:<ol><li>Go straight</li><li>Go left</li><li>Go right</li></ol></li><li>You don’t have enough information to choose correctly</li><li>Each choice leads to another set of choices</li><li>One or more sequences of choices may (or may not)lead to a solution</li><li>Many types of maze problem can be solved with backtracking</li></ol>
<!-- /wp:list -->

<!-- wp:image {"id":3137,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-37.png?w=620" alt="" class="wp-image-3137"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Each of the problems above are in the some sense <em>the same problem</em>. They all work by calling some initialization function, then for each possible slot, you try each possible value in turn, stopping when a solution is found, or backtracking when we can’t fill the current slot.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Iterative Implementations</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The solutions above used recursion to implement backtracking. This is great because if we ever have to backtrack, we return right in the middle of a <code>for</code>-loop so we know the next value to try. This is actually awesome.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Can we do things iteratively?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Well, yeah, but we have to some incrementing an decrementing manually, and some ugly loop controlling.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>The backtracking algorithm</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Backtracking is really quite simple--we“explore” each node, as follows:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To “explore” node N:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>If N is a goal node, return “success”</li><li>If N is a leaf node, return “failure”</li><li>For each child C of N,<ol><li>Explore C<ol><li>If C was successful, return “success”</li></ol></li></ol></li><li>Return “failure”</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>It is convenient to implement this kind of processing by constructing a tree of choices being made called the "State Space Tree". Its root represents an initial state before the search for the solution begins. The nodes of the first level in the tree represent the choices for the first component of a solution and the nodes of a second level represent the choices for the second component and so on.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A node in the state space tree is promising if it corresponds to the partials constructed solution that may lead to the complete solution otherwise the nodes are called non-promising. Leaves of the tree represent either the non- promising dead end or complete solution found by the algorithm.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Application of Backtracking method:</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>An application of Back tracking Algorithm is the N Queen Problem:</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Given: n-queens and an nxn chess board</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Find: A way to place all n queens on the board s.t. no queens are attacking another queen</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let us consider 4 queens as of now, in a chess board. A queen can move,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Horizontal</li><li>Vertical</li><li>Diagonal</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","id":3143,"width":399,"height":312,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-38.png?w=462" alt="" class="wp-image-3143" width="399" height="312"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Algorithm for N queens problem:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Now we can place the first queen with out any problems</li><li>Before placing the second queen. we will have to check if it does not violate any one condition, i.e not present in the same row, column or diagonal as the first queen.</li><li>If we get stuck at a point, after which there is no solution. Then we backtrack to one step before and take an another approach.</li><li>We repeat this until we get a solution.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fimage3.slideserve.com%2F6861001%2Fstate-space-tree15-l.jpg&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>An application  of Back tracking Algorithm is the knapsack problem</h5>
<!-- /wp:heading -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fimage.slidesharecdn.com%2Fknapsackproblem-achchuthan-140506120412-phpapp02%2F95%2Fgreedy-knapsack-problem-by-y-achchuthan-2-638.jpg%3Fcb%3D1399378079&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>The <em>profit density</em> of each item (weight / profit) is first calculated, and the knapsack is filled in order of decreasing profit density. A bounding function is used to determine the upper bound on the profit achievable with the current knapsack so as to reduce the search space.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The <em>profit density</em> of each item (weight / profit) is calculated</li><li>Filled in order of decreasing profit density. </li><li>A bounding function is used to determine the upper bound on the profit achievable with the current knapsack so as to reduce the search space.</li><li>Continue until the sack is full</li></ol>
<!-- /wp:list -->

<!-- wp:image {"id":3150,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-39.png?w=759" alt="" class="wp-image-3150"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Bounding function is needed to help kill some live nodes without actually expanding them.<br><strong>A good bounding function for this problem is obtained by using an upper bound on the value of the best feasible solution obtainable by expanding the given live node and any of its descendants. If this upper bound is not higher than the value of the best solution determined so far then that live node may be killed.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":3151,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-40.png?w=812" alt="" class="wp-image-3151"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>More applications of Dynamic programming are,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Subset Sum</li><li>Graph coloring</li><li>Hamiltonian Cycle</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For more information,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a href="http://www.perisastry.com/daa/BackTrkg-BandB.pdf0.pdf"></a><a href="https://jeffe.cs.illinois.edu/teaching/algorithms/book/02-backtracking.pdf">https://jeffe.cs.illinois.edu/teaching/algorithms/book/02-backtracking.pdf</a></li><li><a rel="noreferrer noopener" href="http://www.perisastry.com/daa/BackTrkg-BandB.pdf0.pdf" target="_blank">http://www.perisastry.com/daa/BackTrkg-BandB.pdf0.pdf</a></li><li><a href="https://cs.lmu.edu/~ray/notes/backtracking/" target="_blank" rel="noreferrer noopener">https://cs.lmu.edu/~ray/notes/backtracking/</a></li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center">Branch and bound</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Branch and Bound is a state space search method in which all the children of a node are generated before expanding any of its children.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>It is similar to backtracking technique but uses BFS like search.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fimage.slidesharecdn.com%2Funiformedtreesearching-131220150928-phpapp01%2F95%2Funiformed-tree-searching-12-638.jpg%3Fcb%3D1387552768&amp;f=1&amp;nofb=1" alt=""/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The different types (FIFO, LIFO, LC) define different 'strategies' to explore the search space and generate branches.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>1.FIFO</strong> (first in, first out): always the oldest node in the queue is used to extend the branch. This leads to a <strong>breadth-first</strong> search, where all nodes at depth d are visited first, before any nodes at depth d+1 are visited.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>2.<strong>LIFO</strong> (last in, first out): always the youngest node in the queue is used to extend the branch. This leads to a <strong>depth-first</strong> search, where the branch is extended through every 1st child discovered at a certain depth, until a leaf node is reached.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>3.<strong>LC</strong> (lowest cost): the branch is extended by the node which adds the lowest additional costs, according to a given cost function. The strategy of traversing the search space is therefore defined by the cost function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Branch and bound keeps a list of live nodes. Four strategies are used to manage the list:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Depth first search: As soon as child of current E-node is generated, the child becomes the new E-node<ol><li>Parent becomes E-node only after child’s subtree is explored</li></ol></li><li>In the other 3 strategies, the E-node remains the E-node until it is dead. Its children are managed by:<ol><li>Breadth first search: Children are put in queue</li><li>D-search: Children are put on stack</li><li>Least cost search: Children are put on heap</li></ol></li><li>We use bounding functions (upper and lower bounds) to kill live nodes without generating all their children.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Both BFS and DFS generalize to branch-and-bound strategies</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>BFS is an FIFO search in terms of live node<ol><li>List of live nodes is a queue</li></ol></li><li>DFS is an LIFO search in terms of live nodes<ol><li>List of live nodes is a stack</li></ol></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>Just like backtracking, we will use bounding functions to avoid generating subtrees that do not contain an answer node</strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>An application  of Back tracking Algorithm is the knapsack problem</h5>
<!-- /wp:heading -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fimage.slidesharecdn.com%2Fknapsackproblem-achchuthan-140506120412-phpapp02%2F95%2Fgreedy-knapsack-problem-by-y-achchuthan-2-638.jpg%3Fcb%3D1399378079&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:list {"ordered":true} -->
<ol><li>Numbers inside a node are profit and weight at that node, based on decisions from root to that node</li><li> Nodes without numbers inside have same values as their parent </li><li>Numbers outside the node are upper bound calculated by greedy algorithm<ol><li>Upper bound for every feasible left child (xi=1) is same as its parent’s bound </li><li>Chain of left children in tree is same as greedy solution at that point in the tree </li><li>We only recompute the upper bound when we can’t move to a feasible left child </li></ol></li><li>Final profit and final weight (lower bound) are updated at each leaf node reached by algorithm <ol><li>Solution improves at each leaf node reached </li><li>No further leaf nodes reached after, because lower bound (optimal value) is sufficient to prune all other tree branches before leaf is reached </li></ol></li><li>By using floor of upper bound at nodes, we avoid generating the tree below either node <ol><li>Since optimal solution must be integer, we can truncate upper bounds </li><li>By truncating bounds at Nodes we avoid exploring Nodes.</li></ol></li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.cheggcdn.com%2Fmedia%2Fe28%2Fe28cfa91-21fa-476b-b447-c43f5bcfcbb4%2FphpdbbosA.png&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":5} -->
<h5>An application  of Back tracking Algorithm is the Traveling salesman</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Definition: Find a tour of minimum cost starting from a node S going through other nodes only once and returning to the starting point S.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Definitions: </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>A row(column) is said to be reduced iff it contains at least one zero and all remaining entries are non-negative. </li><li>A matrix is reduced iff every row and column is reduced.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Branching: </p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Each node splits the remaining solutions into two groups: those that include a particular edge and those that exclude that edge</li><li>Each node has a lower bound.,</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Bounding: How to compute the cost of each node?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Subtract of a constant from any row and any column does not change the optimal solution (The path).</li><li>The cost of the path changes but not the path itself.</li><li>Let A be the cost matrix of a G=(V,E).</li><li>The cost of each node in the search tree is computed as follows:<ol><li>Let R be a node in the tree and A(R) its reduced matrix</li><li>The cost of the child (R), S: <ol><li>Set row i and column j to infinity</li><li>Set A(j,1) to infinity </li><li>Reduced S and let RCL be the reduced cost. </li><li>C(S) = C(R) + RCL+A(i,j)</li></ol></li></ol></li><li>Get the reduced matrix A' of A and let L be the value subtracted from A.</li><li>L: represents the lower bound of the path solution</li><li>The cost of the path is exactly reduced by L</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>What to determine the branching edge? </strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The rule favors a solution through left subtree rather than right subtree, i.e., the matrix is reduced by a dimension. </li><li>Note that the right subtree only sets the branching edge to infinity. </li><li>Pick the edge that causes the greatest increase in the lower bound of the right subtree, i.e., the lower bound of the root of the right subtree is greater.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"id":3198,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-45.png?w=866" alt="" class="wp-image-3198"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>For more information,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="http://www.cs.umsl.edu/~sanjiv/classes/cs5130/lectures/bb.pdf" target="_blank">http://www.cs.umsl.edu/~sanjiv/classes/cs5130/lectures/bb.pdf</a></li><li><a rel="noreferrer noopener" href="https://www2.seas.gwu.edu/~bell/csci212/Branch_and_Bound.pdf" target="_blank">https://www2.seas.gwu.edu/~bell/csci212/Branch_and_Bound.pdf</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>Difference between Backtracking and Branch and Bound</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fpediaa.com%2Fwp-content%2Fuploads%2F2019%2F01%2FDifferences-Between-Backtracking-and-Branch-and-Bound-Comparison-Summary.jpg&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>NP - Hard and NP Complete Problems:</h2>
<!-- /wp:heading -->

<!-- wp:image {"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fi2.wp.com%2Fwww.mlopt.com%2Fwp-content%2Fuploads%2F2018%2F02%2Fpnp.png%3Fresize%3D655%252C384&amp;f=1&amp;nofb=1" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>1.Polynomial Algorithms(P)</strong>:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>P is the class of decision problems which can be solved in polynomial time by a deterministic Turing machine. If a problem belongs to P, then there exists at least one algorithm that can solve it from scratch in polynomial time.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Many algorithms complete in polynomial time:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>All basic mathematical operations; addition, subtraction, division, multiplication</li><li>Linear and Binary Search Algorithms for a given set of number</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>2.Nondeterministic Polynomial(NP) :</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>NP is the class of decision problems which can be solved in polynomial time by a non-deterministic Turing machine. Equivalently, it is the class of problems which can be <em>verified</em> in polynomial time by a deterministic Turing machine.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>They cannot be solved in polynomial time. However, they can be verified in polynomial time.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>3.Nondeterministic Polynomial Hard(NP-Hard) :</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>NP-hard is the class of decision problems to which all problems in NP can be reduced to in polynomial time by a deterministic Turing machine.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>They are not only hard to solve but are hard to verify as well. In fact, some of these problems aren’t even decidable. Some of them are,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Traveling Salesman Problem, and</li><li>Graph Coloring</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>4.<strong>Nondeterministic Polynomial Complete(NP-Complete) :</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>NP-Complete is the intersection of NP-hard and NP. Equivalently, NP-complete is the class of decision problems in NP to which all other problems in NP can be reduced to in polynomial time by a deterministic Turing machine.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For any <img src="https://www.baeldung.com/cs/wp-content/ql-cache/quicklatex.com-2315dba7a7db4ddb59a93d12f90cbffc_l3.svg" alt="\mathcal{NP}" width="31" height="16"> problem that’s complete, there exists a polynomial-time algorithm that can transform the problem into any other <img src="https://www.baeldung.com/cs/wp-content/ql-cache/quicklatex.com-2315dba7a7db4ddb59a93d12f90cbffc_l3.svg" alt="\mathcal{NP}" width="31" height="16">-complete problem. This transformation requirement is also called&nbsp;<em>reduction</em>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As stated already, there are numerous <img src="https://www.baeldung.com/cs/wp-content/ql-cache/quicklatex.com-2315dba7a7db4ddb59a93d12f90cbffc_l3.svg" alt="\mathcal{NP}" width="31" height="16"> problems proven to be complete. Among them are:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Traveling Salesman</li><li>Knapsack,</li><li>Graph Coloring</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Curiously, what they have in common, aside from being in <img src="https://www.baeldung.com/cs/wp-content/ql-cache/quicklatex.com-2315dba7a7db4ddb59a93d12f90cbffc_l3.svg" alt="\mathcal{NP}" width="31" height="16">, is that each can be reduced into the other in polynomial time. These facts together place them in <img src="https://www.baeldung.com/cs/wp-content/ql-cache/quicklatex.com-03b00f8955cd0eb5e7472a38d2098a4f_l3.svg" alt="\mathcal{NP}\text{-}Complete" width="113" height="18">.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="http://programmerprodigycode.files.wordpress.com/2020/12/591a7-iu.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>For more information,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="https://cs.lmu.edu/~ray/notes/np/" target="_blank">https://cs.lmu.edu/~ray/notes/np/</a></li><li><a rel="noreferrer noopener" href="https://www.baeldung.com/cs/p-np-np-complete-np-hard" target="_blank">https://www.baeldung.com/cs/p-np-np-complete-np-hard</a></li><li><a rel="noreferrer noopener" href="https://courses.cs.washington.edu/courses/cse373/18au/files/slides/lecture28.pdf" target="_blank">https://courses.cs.washington.edu/courses/cse373/18au/files/slides/lecture28.pdf</a></li><li><a href="https://www.cs.princeton.edu/courses/archive/spr11/cos423/Lectures/NP-completeness.pdf" target="_blank" rel="noreferrer noopener">https://www.cs.princeton.edu/courses/archive/spr11/cos423/Lectures/NP-completeness.pdf</a></li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>The GitHub repository for the code are,</h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li><a rel="noreferrer noopener" href="https://github.com/kakabisht/Algorithms_lab_Sem5" target="_blank">https://github.com/kakabisht/Algorithms_lab_Sem5</a></li><li><a rel="noreferrer noopener" href="https://github.com/kakabisht/Data-structures" target="_blank">https://github.com/kakabisht/Data-structures</a></li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4><strong>If you want to understand basics of graphs, then </strong></h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li><a href="https://programmerprodigy.code.blog/2020/10/22/basics-of-graphs/https://programmerprodigy.code.blog/2020/10/22/basics-of-graphs/" target="_blank" rel="noreferrer noopener">https://programmerprodigy.code.blog/2020/10/22/basics-of-graphs/https://programmerprodigy.code.blog/2020/10/22/basics-of-graphs/</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->