mgu data structures midule 1 ppt

90
 INTRODUCTION TO ALGORITHMS An Algorithm is any well-defned computational procedure that takes zero or more values as Input and produces a set o values or some value as output. Thus algorithm is a sequence o computational steps that transorms the i/p into the o/p.

Upload: robinpt

Post on 04-Nov-2015

232 views

Category:

Documents


0 download

DESCRIPTION

this is a refference ppt for dsa on module 1.

TRANSCRIPT

PowerPoint Presentation

INTRODUCTION TO ALGORITHMSAn Algorithm is any well-defined computational procedure that takes zero or more values as Input and produces a set of values or some value as output. Thus algorithm is a sequence of computational steps that transforms the i/p into the o/p.

Formal Definition & Properties of an Algorithm

An Algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all algorithms should satisfy the following criteria.

Properties of an Algorithm :-

INPUT Zero or more quantities are externally supplied.OUTPUT At least one quantity is produced.DEFINITENESS Each instruction is clear and unambiguous.FINITENESS If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps.EFFECTIVENESS Every instruction must very basic so that it can be carried out, in principle, by a person using only pencil & paper.

3. Definiteness :- each operation must be clear i.e what should be done. Eg:- add 6 or 7 , compute 5/02Development of an AlgorithmDesigning AlgorithmsValidation of algorithmProgram proving/pgm verificationAnalysing the Algorithm/Performance AnalysisTesting the programDebuggingProfilingValidation is to assure that the algm will work correctly.. Debugging :- process of executing algm on sample data to determine faulty result occur and to correct them3Algorithm SpecificationCan describe an algorithm using

Natural language, like EnglishGraphic representation, called flow chartWork well on small and simple algorithmPseudo-code MethodPseudo-code conventionsTo describe algorithms, here we are using pseudo-code that resembles CHow each instructions can be described using pseudo-code?Comments:-Comments begins with // and continue until the end of lineBlocks are indicated with matching braces{ and } and statements are delimited by ; (semicolon)An identifier begin with letter. The data types of variables are not explicitly declaredAssignment of values to variables is done by using the assignment statementGeneral form:- = In order to produce true/false values the logical operators and relational operators can be used.Elements of multidimensional array are accessed using [ and ]Eg:- A is a 2-D array, the ( i, j )th element is denoted as A[i,j]The following looping statements are employed:-for , while , repeatuntilGeneral form:-for variable= value1 to value2 step step do{Statement 1..Statement n}

while do{Statement 1..Statement n}

repeatStatement 1..Statement nuntil A conditional statement has the following formif then if then else Input and output done using the instruction read and writeEg:- write ( The result,x)Algorithm consist of a heading and a body Form of heading:-Algorithm Name( )

Algorithm to find the largest element in an arrayAlgorithm Max(A ,n)// A is an array of size n{Result := A[i];for i:= 2 to n do if A[i] > Result then Result :=A[i]; return Result;}

Algorithm to find the largest element n an arrayAlgorithm Max(A ,n)// A is an array of size n{Result := A[i];For i:= 2 to n do if A[i] > Result then Result :=A[i]; return Result;}

Algorithm to find the largest element n an arrayAlgorithm Max(A ,n)// A is an array of size n{Result := A[i];for i:= 2 to n do if A[i] > Result then Result :=A[i]; return Result;}

Algorithm to find the largest element n an arrayAlgorithm Max(A ,n)// A is an array of size n{Result := A[i];For i:= 2 to n do if A[i] > Result then Result :=A[i]; return Result;}

Algorithm to find the largest element n an arrayAlgorithm Max(A ,n)// A is an array of size n{Result := A[i];for i:= 2 to n do if A[i] > Result then Result :=A[i]; return Result;}

Algorithm to find the largest element n an arrayAlgorithm Max(A ,n)// A is an array of size n{Result := A[i];for i:= 2 to n do if A[i] > Result then Result :=A[i]; return Result;}

Algorithm to find the largest element n an arrayAlgorithm Max(A ,n)// A is an array of size n{Result := A[i];for i:= 2 to n do if A[i] > Result then Result :=A[i]; return Result;}

Recursive AlgorithmAn algorithm is said to be recursive if the same algorithm is invoked in the body.Recursive algorithms can be eitherDirect recursiveIndirect recursiveAn algorithm that calls itself is Direct Recursive.Algorithm A is said to be Indirect Recursive if it calls another algorithm which in turns calls A.

The Towers of Hanoi problem show how to develop a recursive algorithms. Problem:-Three vertical towers labeled A , B ,C are fixed erect on a platformOn tower A, there are n (here n=3) disks and the disk were of decreasing sizeThe problem is to move all the n disks on tower A to B maintaining the same order of size.The can be moved only one at a time and at no time can a disk be on the top of a smaller disk

Tower A Tower BTower CA B Tower A Tower BTower CA C

Tower A Tower BTower CB C

Tower A Tower BTower CA B

Tower A Tower BTower CC A

Tower A Tower BTower CC B

Tower A Tower BTower CA B

Tower A Tower BTower CAlgorithm TowersofHanoi(n,x,y,z)//Move the top n disks from tower x to tower y.{ if(n1) then { TowersofHanoi(n-1,x,z,y); Write(move top disk from tower X ,to top of tower ,Y); TowersofHanoi(n-1,z,y,x); }}TowersofHanoi (n, x, y, z)TowersofHanoi (n-1, z, y, x)TowersofHanoi (n-1, x, z, y)TowersofHanoi (n-1, x, z, y)TowersofHanoi (n-1, z, y, x)TowersofHanoi (n-1, x, z, y)TowersofHanoi (n-1, z, y, x)TowersofHanoi (n-1, x, z, y)TowersofHanoi (n-1, z, y, x)TowersofHanoi (n-1, x, z, y)TowersofHanoi (n-1, z, y, x)TowersofHanoi (n-1, x, z, y)TowersofHanoi (n-1, z, y, x)TowersofHanoi (n-1, x, z, y)TowersofHanoi (n-1, z, y, x)PERFORMANCE ANALYSISSpace Complexity:The space complexity of an algorithm is the amount of memory it needs to run to completion.Time Complexity:The time complexity of an algorithm is the amount of computer time it needs to run to completion.

Space Complexity:The Space needed by each of the algorithm is seen to be the sum of the following component.A fixed part that is independent of the characteristics (eg: number, size)of the inputs and outputs (instance characteristics). The part typically includes the instruction space (ie. Space for the code)space for simple variablefixed-size component variables (also called aggregate) space for constants, and so on.A variable part that consists of the space needed by component variables whose size is dependent on the particular problem instance being solved, the space needed by referenced variables and the recursion stack space.

The space requirement S(P) of any algorithm P may therefore be written as,S(P) = c+ Sp(Instance characteristics) where c is a constant.

Example-1 :-Algorithm abc(a,b,c){return a+b+b*c+(a+b-c)/(a+b) +4.0;}The space needed by this algorithm is independent of the instance characteristics, so Sabc = 0 and one word is adequate to store the values of each of a, b, c. S(abc) = c+ Sabc

Example-2 :-Algorithm sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}

The problem instances for this algorithm are characterized by n, the number of elements to be summed. The space needed by n is one word, since it is of type integer.

The space needed by a is the space needed by variables of type array of floating point numbers. This is atleast n words, since a must be large enough to hold the n elements to be summed.So, we obtain Ssum(n) (3+n)n for a[],one each for n, i & s

Example-3 :- (Recursive Algorithm)Algorithm Rsum(a, n){ if (n 0) then return 0.0; else return Rsum(a, n-1)+ a[n];}

The recursion stack space includes space for the formal parameters, the local variables and the return address.

Each call of Rsum requires atleast 3 wordsSince the depth of recursion is n + 1, the recursion stack space needed is 3(n+1)Time Complexity:Two types of times are associated with the execution of every programcompile timerun (or execution) timeThe compile time does not depend on the instance characteristics. The run time of a program depend on the instance characteristics. This run time is denoted by tpThe time T(p) taken by a program P is the sum of the compile time and the run timeImportance is given for the run time part only, the reason is that that a compiled program will be run several times without recompilationThe execution time can be calculated by counting the no: of program steps.A program step is defined as syntactically and semantically meaningful segment of a program that has an execution time that is independent of the instance characteristics.The number of steps any problem statement is assigned depends on the kind of statement.

For example, For comment line 0 steps Assignment statements 1 stepsIn iterative statement such as for, while & repeat-until Control part of the statement. The step count of the control part is one.

Determining the no:of stepsThere are two ways to determine the no: of steps needed by a program.

Introduce a variable, count into the program statement with initial value 0. Each time the a statement in the original program is executed, count is incremented by the step count of that statementFind the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

Find the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

Find the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

Find the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

Find the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

Find the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

Find the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

FALSE CASEFind the time complexity of algorithm Sum.For finding, here a count variable is introduced.Algorithm Sum(a, n){ s=0.0; for i= 1 to n do s= s+a[i]; return s;}Algorithm Sum(a , n){s= 0.0;count = count+1;for i=1 to n do { count =count+1; s=s+a[i]; count=count+1; }count=count+1;count=count+1;return s;}

FOR THE RETURN STMTFind the time complexity of Recursive algorithm For finding, here a count variable is introduced.Algorithm Rsum(a, n){ if (n 0) then return 0.0;else return Rsum(a, n-1)+ a[n];}Algorithm Rsum(a, n){ count = count+1; if (n 0) then { count =count+1; return 0.0; } else { count=count+1; return Rsum(a, n-1)+ a[n]; }}

FOR THE IF CONDITIONFind the time complexity of Recursive algorithm For finding, here a count variable is introduced.Algorithm Rsum(a, n){ if (n 0) then return 0.0;else return Rsum(a, n-1)+ a[n];}Algorithm Rsum(a, n){ count = count+1; if (n 0) then { count =count+1; return 0.0; } else { count=count+1; return Rsum(a, n-1)+ a[n]; }}

FOR THE RETURNFind the time complexity of Recursive algorithm For finding, here a count variable is introduced.Algorithm Rsum(a, n){ if (n 0) then return 0.0;else return Rsum(a, n-1)+ a[n];}Algorithm Rsum(a, n){ count = count+1; if (n 0) then { count =count+1; return 0.0; } else { count=count+1; return Rsum(a, n-1)+ a[n]; }}

FOR THE ADDITION, FUNCTION INVOCATION & RETURNWhen analyzing a recursive program for its step count, obtains a recursive formula for the step count,

tRSum (n) = 2if n=02+ tRSum (n -1) if n>0These recursive formulas are referred to as recurrence relation2. The second method to determine the step count of an algorithm is to build a table in which we list the total number of steps contributes by each statement.

First determine the number of steps per execution (s/e) of the statement and the total number of times (ie., frequency) each statement is executed.

By combining these two quantities, the total contribution of all statements, the step count for the entire algorithm is obtained.

StatementS/eFrequencyTotal1. Algorithm Sum(a,n)2.{3. S=0.0;4. for i=1 to n do5. s=s+a[i];6. return s;7. }0011110--1n+1n1-001n+1n10Total2n+3StatementS/eFrequencyn=0 n>0Totaln=0 n>0Algorithm Rsum(a, n){ if (n 0) then return 0.0;else return Rsum(a, n-1)+ a[n];}0

11

1 + x0 - - 1 1 1 0

0 1 - -0 0

1 1 0

0 1+x0 0Total2 2+xx = tRSum(n-1)Asymptotic analysis:Expressing the complexity in term of its relationship to know function. This type analysis is called asymptotic analysis.Asymptotic notations are used in analyzing algorithms.Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Asymptotic Upper Bound

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg1:- 3n+2

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg1:- 3n+2 f(n)=3n+2

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg1:- 3n+2 f(n)=3n+2 4n for all n2

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg1:- 3n+2 f(n)=3n+2 4n for all n2 4*n

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg1:- 3n+2 f(n)=3n+2 4n for all n2 4*n O(n)

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg2:- 10n2 +4n+2

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg2:- 10n2 +4n+2 f(n)= 10n2 +4n+2 11n2 for all n5

Asymptotic notation:Big oh: The function f(n)=O(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n , n n0.

Eg2:- 10n2 +4n+2 f(n)= 10n2 +4n+2 11n2 for all n5 = O(n2)

NotationComputing TimeO(1)O(n) O(n2) O(n3) O(2n)O(log n) Constant LinearQuadraticCubicExponential LogarithmicAsymptotic notation:Omega: The function f(n)=(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n, n n0.Asymptotic lower boundEg1:- 3n+2

Asymptotic notation:Omega: The function f(n)=(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n, n n0.

Eg1:- 3n+2 f(n)=3n+2 3n for all n1

Asymptotic notation:Omega: The function f(n)=(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n, n n0.

Eg1:- 3n+2 f(n)=3n+2 3n for all n1 3*n

Asymptotic notation:Omega: The function f(n)=(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n, n n0.

Eg1:- 3n+2 f(n)=3n+2 3n for all n1 3*n (n)

Asymptotic notation:Omega: The function f(n)=(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n, n n0.

Eg2:- 10n2 +4n+2

Asymptotic notation:Omega: The function f(n)=(g(n)) iff there exist positive constants c and n0 such that f(n) c*g(n) for all n, n n0.

Eg2:- 10n2 +4n+2 f(n)= 10n2 +4n+2 n2 for all n1 = (n2)

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.Asymptotic tight bound

Eg 1:- 3n+2

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 1:- 3n+2 f(n)=3n+2

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 1:- 3n+2 f(n)=3n+2 3n+2 4n for all n2

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 1:- 3n+2 f(n)=3n+2 3n+2 4n for all n2 3n+2 3n for all n2

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 1:- 3n+2 f(n)=3n+2 3n+2 4n for all n2 3n+2 3n for all n2 3n 3n+2 4n

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 1:- 3n+2 f(n)=3n+2 3n+2 4n for all n2 3n+2 3n for all n2 3n 3n+2 4n = (n)

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 2:- 10n2 +4n+2

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 2:- 10n2 +4n+2 f(n)= 10n2 +4n+2

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 2:- 10n2 +4n+2 f(n)= 10n2 +4n+2 10n2 +4n+2 n2 for all n5

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 2:- 10n2 +4n+2 f(n)= 10n2 +4n+2 10n2 +4n+2 n2 for all n5 10n2 +4n+2 11n2 for all n5

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 2:- 10n2 +4n+2 f(n)= 10n2 +4n+2 10n2 +4n+2 n2 for all n5 10n2 +4n+2 11n2 for all n5 n2 10n2 +4n+2 11n2

Asymptotic notation:Theta: The function f(n)=(g(n)) iff there exist positive constants c1,c2 and n0 such that c1g(n) f(n) c2 g(n) for all n, n no.

Eg 2:- 10n2 +4n+2 f(n)= 10n2 +4n+2 10n2 +4n+2 n2 for all n5 10n2 +4n+2 11n2 for all n5 n2 10n2 +4n+2 11n2 = (n2)

O-notation ---Less than equal to ()-notation --- Greater than equal to()-notation ---Equal to (=)

Asymptotic notations are used to formalize that an algorithm has running time or storage requirements that are ``less than,'' ``always greater than,' or ``exactly'' some amountLittle oh : The function f(n) = o(g(n)) iff

Eg:- 3n+2 = o(n2)

Little omega : The function f(n) = w(g(n)) iff

Recurrence RelationWhen an algorithm contains a recursive call to itself, its running time can often be described by a recurrenceA recurrence is an equation or inequality that describes a function in terms of its value on smaller inputsFor eg:- The worst case running time T(n) of the merge-sort is described by the recurrence

T(n) = (1) if n=12T(n/2) +(n) if n>1

Iterative method for solving recurrence relationThe basic idea is to expand the recurrence and express it as a summation of terms dependent only on n and initial condition.Solve the recurrence eqn: using iterative methodT(n) = 3T(n/4) + n .(1) Initial condition T(1) = (1) Expand the term T(n/4) using eqn (1) T(n/4)= 3T(n/42) + n/4 .substitute in eqn(1) Eqn (1)=> T(n) = 3{3T(n/42) + n/4 }+ n =9T(n/42) + 3n/4 + n Expand T(n/42)

T(n) = 3{3T(n/42) + n/4 }+ n =32T(n/42) + 3n/4 + n =32{3T(n/43) + n/42} + 3n/4 + n = 33T(n/43) + 32n/42 + 3n/4 + n

n + 3n/4 + 32n /42 + 33T(n/43) n + 3n/4 + 32n /42 +..+ 3iT(n/4i) n + 3n/4 + 32n /42 +..+ 3 log4n T(1)

Expand T(n/42) = 3T(n/43) + n /42T(n) = 2T(n/2)+3n2