{"id":419,"date":"2021-03-11T07:43:12","date_gmt":"2021-03-11T07:43:12","guid":{"rendered":"https:\/\/drssridhar.com\/?p=419"},"modified":"2021-03-11T07:43:12","modified_gmt":"2021-03-11T07:43:12","slug":"module-3-basics-of-algorithm-writing","status":"publish","type":"post","link":"https:\/\/www.drssridhar.com\/?p=419","title":{"rendered":"Module 3: Basics of Algorithm Writing"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>This module focuses on the basics of the algorithm writing. The learning objectives of this module are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To introduce pseudocode notations for writing algorithms<\/li><li>To practice writing elementary algorithms<\/li><li>To introduce iterative and recursive algorithms &nbsp;<\/li><\/ul>\n\n\n\n<p>Algorithms as discussed earlier are step by step procedure for solving a given problem. As said earlier, algorithms can be written using natural language or pseudocode. There is no standard way of writing algorithms in pseudocode. So there is a need for basic guidelines for writing algorithms. The problem solving starts with stepwise refinement. The idea of stepwise refinement is to take a problem and try to divide it into many subproblems. The subproblems can further be divided more subproblems. The subdivision will be carried out till the problem can\u2019t further be divided. Hence, the idea of stepwise refinement is to evolve structures that can be directly implemented in a programming language.<\/p>\n\n\n\n<p>The kinds of structures thus evolved are sequence, decision and Iteration.&nbsp; These are called control structures and are described below:<\/p>\n\n\n\n<p>1. Sequence<\/p>\n\n\n\n<p>Sequence is a structure whereby the computer system executes tasks one by one. This is given as follows:<\/p>\n\n\n\n<p>Task P<\/p>\n\n\n\n<p>Task Q<\/p>\n\n\n\n<p>Task R<\/p>\n\n\n\n<p>Here, the task P is executed first, followed by tasks Q and R.<\/p>\n\n\n\n<p>2. Decision or Conditional Branching<\/p>\n\n\n\n<p>This is a control structure where the condition is evaluated first and based on its condition the course of action is decided.&nbsp; The control structure for decision is given as follows:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF (Condition C) Then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Perform Task A<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Perform Task B<\/p>\n\n\n\n<p>It can be observed that the condition C is evaluated first. Then based on the results of the condition, task P is performed if the condition is true or task Q is performed if the condition is false.<\/p>\n\n\n\n<p>3.<em> Repetition<\/em><\/p>\n\n\n\n<p>Computers are known for their effectiveness in solving repetitive tasks. Repetition control structure is given as follows:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; While (condition C) do<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; R<\/p>\n\n\n\n<p>For example, informally we say often in English language \u201cPerform the task 100 times\u201d. This is a kind of repetition.<\/p>\n\n\n\n<p>&nbsp;There are two types of iteration. Iteration like saying \u201cPerform task A exactly 500 times\u201d is called a bounded iteration. Programming languages do provide a \u2018For \u2013 Statement\u201d that implements a bounded iteration. On the other hand, statements like performing a task for a specific condition are called unbound iteration. Good examples of unbounded iteration are statements like \u201cWhile\u2026End while\u201d and \u201crepeat \u2026. until\u201d.<\/p>\n\n\n\n<p>Once the control structures are evolved, then it has to be written as an algorithm. There are only three ways of writing algorithms:<\/p>\n\n\n\n<p>1. Using a natural language like English.<\/p>\n\n\n\n<p>2. Using a programming languages, say C++ or Java.<\/p>\n\n\n\n<p>3. Pseudocode<\/p>\n\n\n\n<p>English, or any natural language, is obvious choice for writing algorithms. But the problem is most of the natural languages are ambiguous as a wrong interpretation of a word may lead to imprecise implementation. Hence, natural languages are not considered suitable for algorithm writing. Similarly, the usage of a programming language makes algorithm dependent on a particular language. Hence, pseudocode is preferable for writing algorithms. One can recollect from module 1 that pseudocode is a mix of natural language like English and mathematical constructs.<\/p>\n\n\n\n<p>Let us evolve a pseudocode conventions so that the algorithm can be written. The pseudocode conventions of the algorithms are specified below:<\/p>\n\n\n\n<p>1. Assignment Statement<\/p>\n\n\n\n<p>&nbsp;Assignment statement is a statement for assigning a value or expression to a variable. For example, the following assignment statements are valid.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <em>x <\/em>= 20<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; z =&nbsp; r + k<\/p>\n\n\n\n<p>2. Input\/output Statements<\/p>\n\n\n\n<p>Input statement is used by the user to give values to the variables of the algorithm. For example, the following statement is right.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Input x, y<\/p>\n\n\n\n<p>Similarly, the print or write statement is used to print the values of the variables. For example, the following statement is used to print the value of the variable x and y.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Print x,y or Write x,y<\/p>\n\n\n\n<p><strong>3. Conditional Statements<\/strong><\/p>\n\n\n\n<p>Algorithm can have conditional statement. The syntax of the conditional statement can be shown as:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<em>If &nbsp;<\/em>(condition) then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Statement (s)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End if<\/p>\n\n\n\n<p>Here, the condition (True or false type) is evaluated first. If the condition is true, then statement(s) are executed. \u201cIf- Endif\u201d serves as brackets for the conditional statement. &nbsp;Conditional statement can have else part also as shown below:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>&nbsp;If<\/em> (condition) then&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Statement A ;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>&nbsp;else<\/em><\/p>\n\n\n\n<p><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/em>&nbsp;Statement B &nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>End if<\/em><\/p>\n\n\n\n<p>Here, If the condition is true, then statement A (This can be a set of statements also) are executed. Otherwise, if the condition is false, then statement B (This also can be a single or multiple statements) is executed.<\/p>\n\n\n\n<p><strong>4. Repetition Statement<\/strong><\/p>\n\n\n\n<p>Algorithms can have repetitive statements. Three repetitive statements are supported by most of the programming languages.<\/p>\n\n\n\n<p>Unconditional repetitive statement is \u2018For\u2019 statement. This is an example of bounded iteration. The syntax of this statement is given as follows<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For variable = value1 to value2 do<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Statement(s)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End for<\/p>\n\n\n\n<p>Computer system executes this statement like this: First the variable is to value1. Value1 and value2 can be a value or an expression. Then the statement(s) is executed till the variable values reaches value2.&nbsp;<\/p>\n\n\n\n<p>Conditional Loop:<\/p>\n\n\n\n<p>One useful repetitive statement is \u201cWhile\u201d statement that provides a conditional loop. The syntax of the statement is given below:<\/p>\n\n\n\n<p>While (Condition) do begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Statement(s);<\/p>\n\n\n\n<p>End while.<\/p>\n\n\n\n<p>Repeat\u2026Until also provides repetition control structure. The difference between this statement and While statement is that \u201crepeat \u2013 until\u201d statement first executes the task and then checks condition of the statement. The syntax of repeat statement is given below:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repeat&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Statement(s)<\/p>\n\n\n\n<p>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;until (condition)<\/p>\n\n\n\n<p>Using these statements, some elementary algorithms can be designed.<\/p>\n\n\n\n<p>let us practice some elementary algorithms and let us consider the problem of converting Fahrenheit to Celsius. The problem can be directly solved using a simple formula. &nbsp;The formula for conversion is given as<\/p>\n\n\n\n\n\n<p>The algorithm can be written informally as follows:<\/p>\n\n\n\n<p>1. Input Fahrenheit temperature<\/p>\n\n\n\n<p>2. Apply the formula for temperature conversion<\/p>\n\n\n\n<p>3. Display the results.<\/p>\n\n\n\n<p>The algorithm can be given formally as follows:<\/p>\n\n\n\n<p><strong>Algorithm FtoC(F)<\/strong><\/p>\n\n\n\n<p>Input : Fahrenheit F<\/p>\n\n\n\n<p>Output: Celsius<\/p>\n\n\n\n<p>Begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Celsius = (5\/9)* (F-32)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return Celsius;<\/p>\n\n\n\n<p>End.<\/p>\n\n\n\n<p>Let us practice one more algorithm for finding the count and sum of even\/odd numbers of an array. The algorithm can be given informally as follows;<\/p>\n\n\n\n<p>1. Initialize oddcount, evencount, oddsum and evensum<\/p>\n\n\n\n<p>2. Read the sumber<\/p>\n\n\n\n<p>3. If it is odd or even, then increment the appropriate counters<\/p>\n\n\n\n<p>4. Display the results<\/p>\n\n\n\n<p>The algorithm is formally given as follows:<\/p>\n\n\n\n<p>Algorithm sumoddeven (A[1..n])<\/p>\n\n\n\n<p>Input: An array A[1..n]<\/p>\n\n\n\n<p>Output: sum on odd and even number count<\/p>\n\n\n\n<p>oddcount = 0<\/p>\n\n\n\n<p>evencount = 0<\/p>\n\n\n\n<p>oddsum = 0<\/p>\n\n\n\n<p>evensum = 0<\/p>\n\n\n\n<p>for i = 1 to n<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reminder = A[i] mod 2<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (reminder = 0) then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; evensum = evensum + A[i]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; evencount = evencount + 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; oddsum = oddsum + A[i]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; oddcount = oddcount + 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End if<\/p>\n\n\n\n<p>End for<\/p>\n\n\n\n<p>Return evensum, evencount, oddsum, oddcount<\/p>\n\n\n\n<p>End<\/p>\n\n\n\n<p>Another popular algorithm is linear search. Here, a target is given and the objective of the linear search is to display whether the target is present in the given array, if so where? And failure message is the target is not present in the array.<\/p>\n\n\n\n<p>The informal algorithm is given as follows:<\/p>\n\n\n\n<p>1. Read the value of the target and array A<\/p>\n\n\n\n<p>2. Index = 1, found = false<\/p>\n\n\n\n<p>3. Repeat until found = true of index &gt; n<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if the value at index = target then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return the index and set found = true<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else index = index + 1<\/p>\n\n\n\n<p>4. If (not found) then output message that target is not found<\/p>\n\n\n\n<p>5. Exit<\/p>\n\n\n\n<p>It can be seen that initially index and flag found is initialized to 1 and \u2018false\u2019 respectively. Every value guided by index pointer is checked and if the target is found, then the flag is set true and the corresponding index is sent. Otherwise, the failure message to find target is printed.<\/p>\n\n\n\n<p>Formally this can be written as follows:<\/p>\n\n\n\n<p>Algorithm Search(list, n , target)<\/p>\n\n\n\n<p>Begin<\/p>\n\n\n\n<p>index = 1<\/p>\n\n\n\n<p>found = false<\/p>\n\n\n\n<p>repeat until found = true of index &gt; n<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (list<sub>index<\/sub> = target ) then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \u201ctarget found at\u201d, index<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; found = true<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else index = index + 1<\/p>\n\n\n\n<p>if (not found) then print \u2018Target is not found\u2019<\/p>\n\n\n\n<p>End<\/p>\n\n\n\n<p>Thus one can conclude that after stepwise refinement, the control structures are evolved and can be written suitable as a pseudocode. Now let us discuss about recursive algorithms.<\/p>\n\n\n\n<p><strong>Basics of Recursion&nbsp;<\/strong><\/p>\n\n\n\n<p>Recursion is a way of defining an object using the concept of self-reference. The statements like \u201cA length of the linked list of size N is 1 plus the length of the remaining linked list of size N-1\u201d are examples of recursive definition.<\/p>\n\n\n\n<p>Let us start with recursive definitions. The basic components of a recursive algorithm definition are given as follows:<\/p>\n\n\n\n<p>1.&nbsp; Base case \u2013 This is also known as initial values. This is the non-recursive part.<\/p>\n\n\n\n<p>2.&nbsp; Recursive step &#8211; It is the recursive part of the algorithm. Often this is a rule used for calculating a function in terms of the previous values of the function.<\/p>\n\n\n\n<p>3. Step for progress towards base case: This is a condition that ensures the algorithm eventually converges by bringing the recursive step towards the base case.<\/p>\n\n\n\n<p>Let us discuss some recursive algorithms now. Let us consider the problem of finding summation problem given as <img loading=\"lazy\" decoding=\"async\" width=\"32\" height=\"45\" src=\"\">. The recursive definition of summation can be given compactly as follows<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"21\" height=\"98\" src=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if n = 0<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Sigma(n) =<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n+ Sigma(n-1)&nbsp;&nbsp;&nbsp;&nbsp; if&nbsp; n &gt; 1<\/p>\n\n\n\n<p>A simple recursive algorithm for implementing the above recursive formula is given as follows:<\/p>\n\n\n\n<p><strong>Algorithm Sigma (N)<\/strong><\/p>\n\n\n\n<p><em>Program: Compute sum recursively<\/em><\/p>\n\n\n\n<p><em>Input: N<\/em><\/p>\n\n\n\n<p><em>Output: Sum of N numbers<\/em><\/p>\n\n\n\n<p>Begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; if (N == 0)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; return 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; else<\/p>\n\n\n\n<p>&nbsp;&nbsp; &nbsp;return N + Sigma(N &#8211; 1);<\/p>\n\n\n\n<p>&nbsp;&nbsp; End if<\/p>\n\n\n\n<p>End<\/p>\n\n\n\n<p>The above algorithm can also be written as an iterative algorithm. The iterative algorithm is given as follows:<\/p>\n\n\n\n<p><strong>Algorithm iterative-sigma(N)<\/strong><\/p>\n\n\n\n<p><em>Program: Compute sum recursively<\/em><\/p>\n\n\n\n<p><em>Input: N<\/em><\/p>\n\n\n\n<p><em>Output: Sum of N numbers<\/em><\/p>\n\n\n\n<p>Begin<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; sum = 0;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i = 1 to N do<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sum = sum + I<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp; end for<\/p>\n\n\n\n<p>End<\/p>\n\n\n\n<p>It can be observed that both versions give identical answers. It can be observed that recursive algorithms are compact and it is easier to formulate the algorithms. But , the disadvantage is that recursive algorithms take extra space and require more memory.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"21\" height=\"98\" src=\"\">Let us discuss about one more recursive algorithm for finding factorial of a number. The recursive function for finding the factorial of a number is given as follows:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if n = 0<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Factorial(n)&nbsp;&nbsp;&nbsp;&nbsp; =&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n<img loading=\"lazy\" decoding=\"async\" width=\"12\" height=\"13\" src=\"\">factorial(n-1)&nbsp; if <img loading=\"lazy\" decoding=\"async\" width=\"33\" height=\"19\" src=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>The pseudocode for finding factorial of a number is given as follows:<\/p>\n\n\n\n<p>Algorithm MFactorial(m)<\/p>\n\n\n\n<p>Begin<\/p>\n\n\n\n<p>If m &lt; 0 then<\/p>\n\n\n\n<p>print \u201cfactorial for negative numbers is not possible\u201d<\/p>\n\n\n\n<p>End if<\/p>\n\n\n\n<p>If ((m=0) OR (m = 1) then<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return 1;<\/p>\n\n\n\n<p>Else<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return m * MFactorial(m-1);<\/p>\n\n\n\n<p>End If<\/p>\n\n\n\n<p>End.<\/p>\n\n\n\n<p>Another important problem is towers of Hanoi.&nbsp; There are three pegs \u2013 source, intermediate and destination. The objective of this problem is to move the disks from source peg to the destination peg using the intermediate peg with the following rules:<\/p>\n\n\n\n<p>1. At any point of time, only one disk should be moved<\/p>\n\n\n\n<p>2. A larger disk cannot be placed on the smaller disk at any point of time<\/p>\n\n\n\n<p>The logic for solving this problem is that, for N disks, N-1 disks are moved to the intermediate tower and then the larger disk is moved to the destination. Then the disks are moved from the intermediate tower to the destination.<\/p>\n\n\n\n<p>The formal algorithm is given as follows:<\/p>\n\n\n\n<p><strong>Algorithm towersofhanoi(A,B,C,n)<\/strong><\/p>\n\n\n\n<p><em>Input: Source Peg A, intermediate Peg B and Destination Peg C, &nbsp;n disks<\/em><\/p>\n\n\n\n<p><em>Output: All the disks in the tower C<\/em><\/p>\n\n\n\n<p>Begin<\/p>\n\n\n\n<p>if n = 1 the move disk from A to C<\/p>\n\n\n\n<p>lse<\/p>\n\n\n\n<p>towersofhanoi (A,C,B,n-1);<\/p>\n\n\n\n<p>move disk from A to C &nbsp;<\/p>\n\n\n\n<p>towersofhanoi (B,A,C,n-1);<\/p>\n\n\n\n<p>End if<\/p>\n\n\n\n<p>End.<\/p>\n\n\n\n<p>Thus, one can conclude this module as follows:<\/p>\n\n\n\n<p>1. Stepwise refinement leads to a better algorithm<\/p>\n\n\n\n<p>2. Control structures are sequence, Decision or conditional branching and repetition<\/p>\n\n\n\n<p>3. Pseudocode is better way of writing algorithms.<\/p>\n\n\n\n<p>References<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li><em>S.Sridhar \u2013 Design and Analysis of Algorithms, Oxford University Press, 2014.<\/em><\/li><li>Cormen, T.H., C.E. Leiserson, and R.L. Rivest, Introduction to Algorithms, MIT Press, Cambridge, MA 1992.<\/li><\/ol>\n ","protected":false},"excerpt":{"rendered":"<p>This module focuses on the basics of the algorithm writing. The learning objectives of this module are as follows: To introduce pseudocode notations for writing algorithms To practice writing elementary algorithms To introduce iterative and recursive algorithms &nbsp; Algorithms as discussed earlier are step by&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[],"tags":[5,6],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","tag-algorithm-writing","tag-psudocode"],"_links":{"self":[{"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=\/wp\/v2\/posts\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=419"}],"version-history":[{"count":1,"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=\/wp\/v2\/posts\/419\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.drssridhar.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}