Lecture Module3 Prof Saroj Kaushik, IIT Delhisaroj/IITJ/Lect3.pdfProf Saroj Kaushik, IIT Delhi 3 `...

36
Lecture_Module3 Prof Saroj Kaushik, IIT Delhi

Transcript of Lecture Module3 Prof Saroj Kaushik, IIT Delhisaroj/IITJ/Lect3.pdfProf Saroj Kaushik, IIT Delhi 3 `...

  • Lecture_Module3Prof Saroj Kaushik, IIT Delhi

  • Problem definitionMethod (how to solve it)◦ Algorithm◦ Data structure◦ Verification for correctness◦ Analysis for efficiencyCoding in given “programming language”Understanding of computer “architecture”“Compilation”, “testing”, “de-bugging”Documentation

    2Prof Saroj Kaushik, IIT Delhi

  • Most difficultRequires interaction between “programmer” and userSpecs include:◦ Input data

    Type, accuracy, units, range, format, location, sequence◦ Special symbols to signal end of data◦ Output data (results)

    type, accuracy, units, range, format, location, “headings”◦ How is output related to input◦ Any special constraintExample: “find the phone no. of a person”Problems get revised often

    3Prof Saroj Kaushik, IIT Delhi

  • It is a finite set of instructions which, iffollowed accomplish a particular task.

    It is basically used to describe a problemsolving method suitable forimplementation as a computer program.

    Algorithm is independent of the machineand language used for implementation.

    4Prof Saroj Kaushik, IIT Delhi

  • Input◦ Zero or more quantities are supplied externallyOutput◦ At least one quantity is producedDefiniteness◦ Each instruction is clear & unambiguousFiniteness◦ It terminates after finite stepsEffectiveness◦ Each instruction is simple to be carried out manually.

    5Prof Saroj Kaushik, IIT Delhi

  • An “unambiguous specification of a method”Is characterized by:◦ Ordered sequence of well-defined, effective

    operations that, when executed, will produce a result after “terminating” within a finite no of steps

    6Prof Saroj Kaushik, IIT Delhi

  • Well-defined and effective◦ No ambiguity, a method must exist◦ Good Examples:

    Add 1 to xcompute largest prime no. < 100compute square root of x to 4 decimal places

    ◦ Bad examples:divide 10 by xcompute largest primecompute square root of x

    7Prof Saroj Kaushik, IIT Delhi

  • Always terminate, and be sure about itProduce correct results◦ this may require some hard thinking◦ testing helps, but is not adequate

    8Prof Saroj Kaushik, IIT Delhi

  • Basic differences are:

    ◦ Program is written in programminglanguage whereas algorithm is inEnglish like pseudo language.

    ◦ Program may be non terminating (OS)whereas algorithm should terminate infinite steps.

    9Prof Saroj Kaushik, IIT Delhi

  • Study of algorithm can be classifiedin four distinct areas namely how to◦ devise◦ express◦ validate◦ analyze algorithms

    10Prof Saroj Kaushik, IIT Delhi

  • Devising good algorithm:◦ Requires study of various design techniques◦ Top down, bottom up and object oriented

    approachesExpressing an algorithm:◦ Good algorithms are expressed using principle of

    structured programmingValidation of algorithm:◦ It should be validated for correctness of all possible

    legal inputs.(correct, incorrect, exceptions)

    ◦ Note that algorithm need not yet be expressed as acomputer program.

    11Prof Saroj Kaushik, IIT Delhi

  • Analysis of an algorithm:◦ Study of behavior pattern or performance

    profile.◦ It can be calculated in terms of computing

    time and space requirement in the machine.Time Complexity: Running time of the programas a function of the size of input.Space Complexity: Amount of computer memoryrequired during the program execution.

    12Prof Saroj Kaushik, IIT Delhi

  • In top-down model, an overview of thesystem is formulated, without goinginto detail for any part of it.

    Each part of the system is then refinedin more details.

    Each new part may then be refinedagain, defining it in yet more detailsuntil the entire specification is detailedenough to validate the model.

    13Prof Saroj Kaushik, IIT Delhi

  • This design model can also be appliedwhile developing algorithm.

    It basically refers to successiverefinement of the problem (task) intosub problems (subtasks).

    Refinement is applied until we reach tothe stage where the subtasks can bedirectly carried out.

    14Prof Saroj Kaushik, IIT Delhi

  • subtask1 subtask2 subtask3

    Main Task

    15Prof Saroj Kaushik, IIT Delhi

  • In bottom-up design individual parts ofthe system are specified in details.

    The parts are then linked together toform larger components, which are inturn linked until a complete system isformed.

    Object-oriented languages such as C++or JAVA use bottom-up approach whereeach object is identified first.

    16Prof Saroj Kaushik, IIT Delhi

  • 17Prof Saroj Kaushik, IIT Delhi

  • It is a technique using which one can write algorithms (programs) in top down fashion.

    This technique should be used with every level of refinement.

    In SP, one entry and one exit principle is adopted in all the constructs.

    Basically there are three structures in SP

    18Prof Saroj Kaushik, IIT Delhi

  • Sequential (sequence)entry

    T1

    T2

    exit

    19Prof Saroj Kaushik, IIT Delhi

  • If cond then task1Y

    Cond task1N

    If cond then task1 else task2

    task2 N Cond Y task1

    20Prof Saroj Kaushik, IIT Delhi

  • WhileWhile (cond) do{

    }

    RepeatRepeat

    Until (cond)

    21Prof Saroj Kaushik, IIT Delhi

  • for ordered sequenceT1, while C then T2,T3

    T1

    T2

    T3

    CN

    22Prof Saroj Kaushik, IIT Delhi

    Y

  • for ordered sequenceT1, Repeat T2 until C,T3

    T1

    T2

    T3

    CN

    23Prof Saroj Kaushik, IIT Delhi

    Y

  • What is the difference between:

    S1

    S2

    S3

    C

    S1

    S2

    S3

    CN

    Y

    24Prof Saroj Kaushik, IIT Delhi

    S1, Repeat S2 until C, S3 S1, While C { S2 }, S3

    N

    Y

  • Computation of income taxGiven a tax table as below, compute the tax, T, on an income, X.

    Here is an algorithm:

    Step 1: Input INC;Step 2: Compute tax, T;Step 3: Output T

    INCOME TAX0

  • Step 1: Input INC;

    Step 2a: if INC > 300000 then T ← 30000 + 0.30*(INC‐300000);

    Step 2b: if INC > 200000 and INC ≤ 300000then T ← 10000 + 0.20*(INC‐200000);

    Step 2c: if INC > 100000 and INC ≤ 200000then T ← 0 + 0.10*(INC‐100000);

    Step 2d: if INC ≤ 100000 then T ← 0;

    Step 3: Output T

    26Prof Saroj Kaushik, IIT Delhi

  • Table look-up: Consider: ◦ special key K,◦ length of list, 5 or N, more generally◦ unsorted L = [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5)],

    where xi is key and yi is corresponding output valueProblem is to search whether key K is in the list and output relevant pair if it existHere is an algorithm:

    Step 1: Input all data, K, and list L;Step 2: Search for K in L;Step 3: Output results

    27Prof Saroj Kaushik, IIT Delhi

  • Here is refined version of the algorithm:

    Input K;Input (x1, y1);Input (x2, y2);Input (x3, y3);Input (x4, y4);Input (x5, y5);If K = x1 then output (x1, y1);If K = x2 then output (x2, y2);If K = x3 then output (x3, y3);If K = x4 then output (x4, y4);If K = x5 then output (x5, y5)

    28Prof Saroj Kaushik, IIT Delhi

  • This algorithm does the same thing, except that it is compact:

    Input K;Repeat these operations 5 times:

    { Input (x, y);If K = x then output (x, y)

    }

    29Prof Saroj Kaushik, IIT Delhi

  • An even better algorithm:

    Input n;If n > 0 then

    { Input K;Repeat these operations n times:{ Input (x, y);

    If K = x then output (x, y)}

    }

    30Prof Saroj Kaushik, IIT Delhi

  • Formula: x1 = [(-b) + √(b2 – 4ac)]/2a x2 = [(-b) - √(b2 – 4ac)]/2a Algorithm: input a, b, c; d = b*b – 4*a*c; if (d ≥ 0) then { e = sqrt(d); r1 = (-b + e) / (2*a); r2 = (-b - e) / (2*a); i1=0; i2 =0 } else

    { e = sqrt(-d); r1 = (-b) / (2*a); r2 = r1; i1= e / (2*a); i2 = -e/(2*a); } output r1,r2,i1,i2

  • Formula:fact = n * n-1 * …* 2 * 1

    Algorithm:input n;i = 0; initializationfact = 1;

    loopingwhile (i < n) { i = i + 1;

    fact = i * fact;}output fact

    Prof Saroj Kaushik, IIT Delhi 32

    ExecutionLet n = 4;i = 0; fact = 1;while loop0 < 4; i = i +1=1; fact = 1 * 1;1 < 4; i = i +1=2; fact = 1 * 2;2 < 4; i = i +1=3; fact = 2 * 3;3 < 4; i = i +1=4; fact = 6 * 44 < 4 is false so exit the while loop; output fact = 24

  • Algorithm:input num;rev = 0;while (num > 0)

    { rev = rev*10 + num mod 10;

    num = num div 10;}

    output rev

    Executionnum = 245;rev = 0;while looprev = 0 * 10 + 5 = 5;num = 24;rev = 5 * 10 + 4 = 54;num = 2;rev = 54 * 10 + 2 =

    542;num = 0;exit whileoutput rev as 542

    Prof Saroj Kaushik, IIT Delhi 33

  • Algorithm: Here number is read in the loop. Atthe end of loop, read elements are notavailable.

    max = 0;i = 0;while (i < 100){ i = i+1;

    input num;if (num > max) then max = num;

    }output max

    34Prof Saroj Kaushik, IIT Delhi

  • Algorithm:input num(i), i = 1,100;max = 0;i = 0;while (i < 100){ i = i+1;

    if (num(i) > max) then max = num(i);}output max, num(i),

    i = 1,100

    ExecutionLet numbers are: 4, 2,7,1max = 0; i = 0;while loopi = 1; 4 > max so max = 4;i = 2; 2 < max so no

    changei = 3; 7 > max so max = 7;i = 4; 1 < max so no

    changeexit of whileoutput max as 7 and

    numbers as 4, 2, 7, 1

    Prof Saroj Kaushik, IIT Delhi 35

  • Series: f = 0! + 1! + 2! + …+ n! (n ≥ 0)Algorithm: Here we are given the value of n. Series

    sum is calculated by finding sum after addingprevious sum with a term at each step.

    input n;f = 0; term = 1; i = 0;while (i < n){ f = f + term;

    i = i + 1;term = term * i;

    }output f

    Problem Solving using ComputerProblem solvingProblem definition AlgorithmCharacteristics of an AlgorithmAlgorithms(1) Algorithms (2)Algorithms (3)Algorithm verses Program Study of AlgorithmContd…Analysis of AlgorithmTop Down Design ModelTop Down Concept in Problem SolvingTop Down DesignBottom-up DesignBottom up DesignStructured Programming (SP)Three types of control flow in SPSelection (test) RepetitionFlow diagram Ordered SequencesFlow diagramRepeat and While ordered sequencesAlgorithms: an exampleAlgorithms: (refined)Algorithms: another exampleTable look-up algorithms (contd.)Table look-up algorithms (contd.)Table look-up algorithms (contd.)Problem 1: Find the roots of a quadratic equation of the form a*x2 + b*x + c = 0Problem 2: Write algorithm to find factorial of nProblem 3: Reversing integer digits (3542 2453)�Problem4: Find maximum out of 100 numbers (+ve integers)�Input all 100 numbers in an array (vector). Then compute maximum out of 100 numbers. At the end of loop we have all the numbers.Problem5: Design an algorithm to find the sum of first n terms of the series