Comments

Notes

MCS 211

18 topics

Basic of an Algorithm and its properties

Updated Jan 28, 20255 min read

Introduction:

The word algorithm is derived from the mathematician of the ninth century Abdullah Jafar Muhammad ibn Musa Al-khowarizmi. The word ‘alkhowarizmi’ is ‘Algorismus’in Latin which became Algorithm after his name.

Definition:

  • An algorithm is a set of rules for carrying out calculation either by hand or on a machine.  

  • An algorithm is a well-defined computational procedure that takes input and produces output.  

  • An algorithm is a finite sequence of instructions or steps (i.e. inputs) to achieve some particular output.

Characteristics Of Algorithm:

  • Input: There must be a finite number of inputs for the algorithm. 

  • Output: There must be some output produced as a result of execution of the algorithm. 

  • Definiteness: There must be a definite sequence of operations for transformation of input into output. 

  •  Effectiveness: Every step of the algorithm should be basic and essential. 

  • Finiteness: The transformation of input to output must be achieved in finite steps, i.e. the algorithm must stop, eventually! Stopping may mean that it should produce the expected output or a response that no solution is possible.

BASICS BUILDING BLOCKS OF ALGORITHMS:

Block Name
Common Name
Sequencing
Step By Step Actions
Selection
Decision
Iteration
Repetition Or Loop
Procedure

Recursion



Sequencing: A problem can be solved by performing some actions in a sequence [called algorithm], and the order of execution of those actions is important to ensure the correctness of an algorithm.

If the order of steps of the algorithm changes and does not follow the steps as specified, it will not produce the correct output as expected.

Selection: As an algorithm has to be generalized to solve many cases, there may be situations where the sequence of execution of actions may depend on some condition. That is, some of the instructions will only be executed if a given condition satisfies.

Iteration: While solving a problem certain actions may be required to execute a certain number of times or until a certain condition is met.


Let us consider Algorithm 1 again to understand these concepts:

Finding GCD of (a, b) with Euclidian Method. Algorithm


GCD-Euclid (a, b)

Step1: begin [start of Algorithm] 

Step2: { 

Step3: while b != 0 do 

Step4: { 

Step5: r < a mod b; 

Step6: a < b; 

Step7: b < r; 

Step8: } [end of while loop] 

Step9: return (b) 

Step10: } [end of algorithm]


In the above algorithm Step5, Step6 and Step7 are examples of sequencing, as these statements are always executed in sequence as written in the text. 

Step3 is the selection step as it decides which next step is to be executed among Step4 and Step9 according to the condition of the loop.

Step3 is also acts as iteration or the looping statements. Based on the while loop condition, the Step4 to Step8 are executed in a repeated manner.

Procedure: A procedure is a set of instructions encapsulated as a reusable block. It can take input parameters, perform specific tasks, and optionally return output. Procedures are widely used to modularize and structure code.

Under this mechanism, the sequence of instructions expected to be repeatedly used in one or more algorithms, is written only once and outside and independent of the algorithms of which the sequence could have been a part otherwise. There may be many such sequences and hence, there is need for an identification of each of such sequences. For this purpose, each sequence is prefaced by statements in the following format:

PROCEDURE ProcedureName(parameters)
    BEGIN
        // Step-by-step instructions
    END PROCEDURE


Example of a Procedure in Algorithm

Problem: Create a procedure to calculate the factorial of a number.

PROCEDURE CalculateFactorial(n: Integer)
    BEGIN
        factorial ? 1
        FOR i ? 1 TO n DO
            factorial ? factorial * i
        END FOR
        PRINT "The factorial of ", n, " is ", factorial
    END PROCEDURE


Explanation of the Example

  1. Procedure Name:

    • CalculateFactorial is the procedure's name.

  2. Parameter:

    • n is the input to the procedure, representing the number for which the factorial will be calculated.

  3. Logic:

    • A loop multiplies numbers from 1 to n to compute the factorial.

  4. Output:

    • The procedure prints the factorial.


Recursion: A procedure, which can call itself, is said to be recursive procedure/algorithm. 

For successful implementation of the concept of recursive procedure, the following conditions should be satisfied.

  1.  There must be an in-built mechanism in the computer system that supports the calling of a procedure by itself, e.g, there may be in-built stack operations on a set of stack registers. 

  2. There must be conditions within the definition of a recursive procedure under which, after a finite number of calls, the procedure is terminated. 

  3. The arguments in successive calls should be simpler in the sense that each succeeding argument takes us towards the conditions mentioned in (2).

 Recursion is an important construct which will be used extensively to solve sorting algorithms, searching algorithm, matrix multiplications, etc.


Key Components of Recursion

  1. Base Case:

    • The condition under which the recursion stops.

    • Prevents infinite recursion.

  2. Recursive Case:

    • The part of the algorithm where the function calls itself with a smaller or simpler input.

General Structure of a Recursive Algorithm?


PROCEDURE RecursiveProcedure(parameters)
    BEGIN
        IF base_case_condition THEN
            // Handle the base case
        ELSE
            // Recursive call with modified parameters
            RecursiveProcedure(modified_parameters)
        END IF
    END PROCEDURE



Problem: Calculate the factorial of a number n.


PROCEDURE Factorial(n: Integer): Integer
    BEGIN
        IF n = 0 THEN
            RETURN 1
        ELSE
            RETURN n * Factorial(n - 1)
        END IF
    END PROCEDURE





Share