My under eye areas maybe dark but my future will be bright and that is more important.
--Your friends at LectureNotes

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

  • Analysis and Design of Algorithms - DAA
  • Note
  • 9 Topics
  • 6 Offline Downloads
  • Uploaded 4 months ago
Ashutosh Jaiswal
Ashutosh Jaiswal
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

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-

Text from page-5

Design and Analysis of Algorithm ( www.jntuworld.com )  The first subtask can be solved by assuming the minimum is a[ I ];checking a[I] with a[I+1],a[I+2]…….,and whenever a smaller element is found, regarding it as the new minimum. a[n] is compared with the current minimum.  Putting all these observations together, we get the algorithm Selection sort. Theorem: Algorithm selection sort(a,n) correctly sorts a set of n>=1 elements .The result remains is a a[1:n] such that a[1] <= a[2] ….<=a[n]. Selection Sort: Selection Sort begins by finding the least element in the list. This element is moved to the front. Then the least element among the remaining element is found out and put into second position. This procedure is repeated till the entire list has been studied. Example: LIST L = 3,5,4,1,2 1 is selected ,  1,5,4,3,2 2 is selected, 1,2,4,3,5 3 is selected, 1,2,3,4,5 4 is selected, 1,2,3,4,5 Proof:    We first note that any I, say I=q, following the execution of lines 6 to 9,it is the case that a[q] Þ a[r],q<r<=n. Also observe that when ‘i’ becomes greater than q, a[1:q] is unchanged. Hence, following the last execution of these lines (i.e. I=n).We have a[1] <= a[2] <=……a[n]. We observe this point that the upper limit of the for loop in the line 4 can be changed to n-1 without damaging the correctness of the algorithm. Algorithm: 1. Algorithm selection sort (a,n) 2. // Sort the array a[1:n] into non-decreasing order. 3.{ 4. for I:=1 to n do 5. { 6. j:=I; 7. for k:=i+1 to n do 8. if (a[k]<a[j]) 9. t:=a[I]; 10. a[I]:=a[j]; 11. a[j]:=t; -5-

Lecture Notes