lecture module3 prof saroj kaushik, iit delhi - ernetsaroj/iitj/lect3.pdf · prof saroj kaushik,...

36
Lecture_Module3 Prof Saroj Kaushik, IIT Delhi

Upload: vanquynh

Post on 20-May-2018

261 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

Lecture_Module3Prof Saroj Kaushik, IIT Delhi

Page 2: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 3: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 4: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 5: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 6: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 7: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 8: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 9: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 10: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

10Prof Saroj Kaushik, IIT Delhi

Page 11: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 12: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 13: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 14: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 15: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

subtask1 subtask2 subtask3

Main Task

15Prof Saroj Kaushik, IIT Delhi

Page 16: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 17: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

17Prof Saroj Kaushik, IIT Delhi

Page 18: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 19: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

Sequential (sequence)entry

T1

T2

exit

19Prof Saroj Kaushik, IIT Delhi

Page 20: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

If cond then task1Y

Cond task1N

If cond then task1 else task2

task2 N Cond Y task1

20Prof Saroj Kaushik, IIT Delhi

Page 21: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

WhileWhile (cond) do{

}

RepeatRepeat

Until (cond)

21Prof Saroj Kaushik, IIT Delhi

Page 22: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

for ordered sequenceT1, while C then T2,T3

T1

T2

T3

CN

22Prof Saroj Kaushik, IIT Delhi

Y

Page 23: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

for ordered sequenceT1, Repeat T2 until C,T3

T1

T2

T3

CN

23Prof Saroj Kaushik, IIT Delhi

Y

Page 24: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 25: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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 <= INC <=100000 0100001 <= INC <=200000 0 + 0.10*(INC-100000)200001 <= INC <=300000 10000 + 0.20*(INC-200000)300001 <= INC 30000 + 0.30*(INC-300000)

25Prof Saroj Kaushik, IIT Delhi

Page 26: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 27: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 28: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 29: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 30: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 31: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 32: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 33: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 34: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 35: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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

Page 36: Lecture Module3 Prof Saroj Kaushik, IIT Delhi - ERNETsaroj/IITJ/Lect3.pdf · Prof Saroj Kaushik, IIT Delhi. 11 `Analysis of an algorithm: Study of behavior pattern or performance

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