×
Stop thinking about WHAT WILL HAPPEN and start thinking about WHAT YOU CAN DO.

# Note for Analysis and Design of Algorithms - DAA by Ashutosh Jaiswal

• Analysis and Design of Algorithms - DAA
• Note
• 9 Topics
• 389 Views
Ashutosh Jaiswal
0 User(s)

#### Text from page-1

Design and Analysis of Algorithm ( www.jntuworld.com ) UNIT – I Analysis of Algorithm: INTRODUCTION C A S E A N A LY S I S -S O LV I N G R E C U R R E N C E S . – A N A LY Z I N G C O N T R O L S T R U C T U R E S - AV E R A G E ALGORITHM Informal Definition: An Algorithm is any well-defined computational procedure that takes some value or set of 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: An Algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all algorithms should satisfy the following criteria. 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. 5. EFFECTIVENESS  Every instruction must very basic so that it can be carried out, in principle, by a person using only pencil & paper. 1. 2. 3. 4. Issues or study of Algorithm:      How to device or design an algorithm  creating and algorithm. How to express an algorithm  definiteness. How to analysis an algorithm  time and space complexity. How to validate an algorithm  fitness. Testing the algorithm  checking for error. Algorithm Specification: Algorithm can be described in three ways. 1. Natural language like English: When this way is choused care should be taken, we should ensure that each & every statement is definite. 2. Graphic representation called flowchart: This method will work well when the algorithm is small& simple. 3. Pseudo-code Method: -1-

#### Text from page-2

Design and Analysis of Algorithm ( www.jntuworld.com ) In this method, we should typically describe algorithms as program, which resembles language like Pascal & algol. Pseudo-Code Conventions: 1. Comments begin with // and continue until the end of line. 2. Blocks are indicated with matching braces {and}. 3. An identifier begins with a letter. The data types of variables are not explicitly declared. 4. Compound data types can be formed with records. Here is an example, Node. Record { data type – 1 data-1; . . . data type – n data – n; node * link; } Here link is a pointer to the record type node. Individual data items of a record can be accessed with  and period. 5. Assignment of values to variables is done using the assignment statement. <Variable>:= <expression>; 6. There are two Boolean values TRUE and FALSE.  Logical Operators AND, OR, NOT Relational Operators <, <=,>,>=, =, != 7. The following looping statements are employed. For, while and repeat-until While Loop: While < condition > do { <statement-1> . . . <statement-n> } -2-

#### Text from page-3

Design and Analysis of Algorithm ( www.jntuworld.com ) For Loop: For variable: = value-1 to value-2 step step do { <statement-1> . . . <statement-n> } repeat-until: repeat <statement-1> . . . <statement-n> until<condition> 8. A conditional statement has the following forms.  If <condition> then <statement>  If <condition> then <statement-1> Else <statement-1> Case statement: Case { : <condition-1> : <statement-1> . . . : <condition-n> : <statement-n> : else : <statement-n+1> } 9. Input and output are done using the instructions read & write. 10. There is only one type of procedure: Algorithm, the heading takes the form, Algorithm Name (Parameter lists) -3-

#### Text from page-4

Design and Analysis of Algorithm ( www.jntuworld.com )  As an example, the following algorithm fields & returns the maximum of ‘n’ given numbers: 1. 2. 3. 4. 5. 6. 7. 8. 9. algorithm Max(A,n) // A is an array of size n { Result := A[1]; for I:= 2 to n do if A[I] > Result then Result :=A[I]; return Result; } In this algorithm (named Max), A & n are procedure parameters. Result & I are Local variables.  Next we present 2 examples to illustrate the process of translation problem into an algorithm. Selection Sort:  Suppose we Must devise an algorithm that sorts a collection of n>=1 elements of arbitrary type.  A Simple solution given by the following.  ( From those elements that are currently unsorted ,find the smallest & place it next in the sorted list.) Algorithm: 1. For i:= 1 to n do 2. { 3. Examine a[I] to a[n] and suppose the smallest element is at a[j]; 4. Interchange a[I] and a[j]; 5. }  Finding the smallest element (sat a[j]) and interchanging it with a[ i ]  We can solve the latter problem using the code, t := a[i]; a[i]:=a[j]; a[j]:=t; -4-