Unit-1-Ppt Ada
Unit-1-Ppt Ada
By
RUPALI CHAURE
Assistant Professor
CSE, SIRT
INTRODUCTION
2.OUTPUT: At least one quantity is produced. For each input the algorithm
produced value from specific task.
4.FINITENESS: If we trace out the instructions of an algorithm, then for all cases,
the algorithm terminates after a finite number of steps.
programming language
3.Person should have 3.Programmer
Domain knowledge
4.Analyze 4.Testing
ALGORITHM SPECIFICATION
Algorithm can be described (Represent) in four ways.
3.An identifier begins with a letter. The data types of variables are not
explicitly declared.
node= record
{
data type 1 data 1;
data type n data n;
node *link;
}
4. There are two Boolean values TRUE and FALSE.
Logical Operators
AND, OR, NOT
Relational Operators
<, <=,>,>=, =, !=
5. Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;
Here link is a pointer to the record type node. Individual data items of
a record can be accessed with and period.
Contd…
7. The following looping statements are employed.
For, while and repeat-until While Loop:
While < condition > do
{
<statement-1>
..
..
<statement-n>
}
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>
Case
{
: <condition-1> : <statement-1>
.
.
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}
9. Input and output are done using the instructions read & write. No
format is used to specify the size of input or output quantities
Contd…
10. There is only one type of procedure: Algorithm, the heading takes the
form,
Algorithm Name (Parameter lists)
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for i:= 2 to n do
6. if A[i] > Result then
7. Result :=A[i];
8. return Result;
9. }
Issue in the study of algorithm
1. How to create an algorithm.
2. How to validate an algorithm.
3. How to analyses an algorithm
4. How to test a program.
b) Linear(variable)space complexity
1.Constant space complexity: A fixed amount
of space for all the input values.
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for i=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
KINDS OF ANALYSIS
1.Worst-case: (usually)
• T(n) = maximum time of algorithm on any input of size n.
2.Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs of
size n.
• Need assumption of statistical distribution of inputs.
3.Best-case:
• T(n) = minimum time of algorithm on any input of size n.
COMPLEXITY:
Complexity refers to the rate at which the storage time grows as a
function of the problem size
Analysis of an Algorithm
The goal of analysis of an algorithm is to compare
algorithm in running time and also Memory
management.
Running time of an algorithm depends on how
long it takes a computer to run the lines of code of
the algorithm.
Running time of an algorithm depends on
1.Speed of computer
2.Programming language
3.Compiler and translator
Examples: binary search, linear search
ASYMPTOTIC ANALYSIS:
Expressing the complexity in term of its relationship
to know function. This type analysis is called
asymptotic analysis.
The main idea of Asymptotic analysis is to have a
measure of efficiency of an algorithm , that doesn’t
depends on
1.Machine constants.
2.Doesn’t require algorithm to be implemented.
3.Time taken by program to be prepare.
ASYMPTOTIC NOTATION
1.Big oh (O)notation
2.Big omega (Ω) notation
3.Theta(Θ) notation
4.Little oh notation
5.Little omega(Ω) notation
1.Big oh (O)notation
1.Big oh (O)notation : Asymptotic “less than”(slower rate).This
notation mainly represent upper bound of algorithm run time.
Big oh (O)notation is useful to calculate maximum amount of time of
execution.
By using Big-oh notation we have to calculate worst case time
complexity.
Formula : f(n)<=c g(n) n>=n0 , c>0 ,n0
>=1
Problem:-Find upper bond ,lower bond & tight bond range for
functions: f(n)= 2n+5
Solution:-Let us given that f(n)= 2n+5 , now g(n)= n
lower bond=2n, upper bond =3n, tight bond=2n
For Big –oh notation(O):- according to definition
f(n)<=cg(n) for Big oh we use upper bond so
f(n)=2n+5, g(n)=n and c=3 according to definition
2n+5<=3n
Put n=1 7<=3 false Put n=2 9<=6 false Put n=3
14<=9 false Put n=4 13<=12 false Put n=5
15<=15 true
now for all value of n>=5 above condition is satisfied. C=3
n>=5
2. Big - omega notation :- f(n)>=c.g(n) we know that
this
Notation is lower bond notation so c=2
Let f(n)=2n+5 & g(n)=2.n
Now 2n+5>=c.g(n);
2n+5>=2n put n=1
We get 7>=2 true for all value of n>=1,c=2 condition is
satisfied.
3. Theta notation :- according to definition
c1.g(n)<=f(n)<=c2.g
DIVIDE AND CONQUER
Given a function to compute on ‘n’ inputs the divide-and-conquer
strategy suggests splitting the inputs into ‘k’ distinct subsets, 1<k<=n,
yielding ‘k’ sub problems.
These sub problems must be solved, and then a method must be found
to combine sub solutions into a solution of the whole.
If the sub problems are still relatively large, then the divide-and-
conquer strategy can possibly be reapplied.
If the problem p and the size is n , sub problems are n1, n2 ….nk,
respectively, then the computing time of D And C is described by the
recurrence relation.
T(n)= { g(n) n small
T(n1)+T(n2)+……………+T(nk)+f(n);
otherwise.
g(n) is the time of compute the answer directly for small I/p s. f(n) is the
time for dividing P & combining the solution to sub problems.
DIVIDE AND CONQUER :GENERAL METHOD
Consider the case in which a=2 and b=2. Let T(1)=2 & f(n)=n. We have,
T(n) = 2T(n/2)+n
2[2T(n/2/2)+n/2]+n
[4T(n/4)+n]+n
4T(n/4)+2n
4[2T(n/4/2)+n/4]+2n
4[2T(n/8)+n/4]+2n
8T(n/8)+n+2n
8T(n/8)+3n
23T(n/23)+3n
By using substitution method
Let n=2k
K=logn
2
K=3
2kT(n/n)+3n
nT(1)+3N
2n+kn
2n+nlogn
Time complexity is O(nlogn)
APPLICATIONS
3.Merge Sort is also a sorting algorithm. The algorithm divides the array in
two halves, recursively sorts them and finally merges the two sorted halves.
BINARY SEARCH
1. Algorithm Bin search(a,n,x)
2. // Given an array a[1:n] of elements in non-decreasing
3. //order, n>=0,determine whether x is present and
4. // if so, return j such that x=a[j]; else return 0.
5. {
6. low:=1; high:=n;
7. while (low<=high) do
8. {
9. mid:=[(low+high)/2];
10. if (x<a[mid]) then high;
11. else if(x>a[mid]) then
12. low=mid+1;
13. else return mid;
14. }
15. return 0; }
EXAMPLE
Only the variables low, high & mid need to be traced as we simulate the
algorithm.
Each set is individually sorted, and the resulting sorted sequences are
merged to produce a single sorted sequence of n elements.
Algorithm MergeSort(A, B, m, n)
{
i=1; j=1; k=1;
while (i<=m && j<=n)
{
if (A[i] < B[j] )
C[k++] = A[i++]
else
C[k++] = B[j++]
}
for ( ; i<=m ; i++)
C[k++] = A[i]
for ( ; j <= n ; j++)
C[k++] = B[j]
}
ALGORITHM FOR MERGE SORT using Recursion
RECURRENCE RELATION
1 n=1
T(n) =
2T(n/2) + n n>1
EXAMPLE
Consider the array of 10 elements a[1:10] =(310, 285, 179, 652, 351, 423,
861, 254, 450, 520)
Algorithm Mergesort begins by splitting a[] into 2 sub arrays each of size
five (a[1:5] and a[6:10]).
The elements in a[1:5] are then split into 2 sub arrays of size 3 (a[1:3] ) and
2(a[4:5])
Then the items in a [1:3] are split into sub arrays of size 2 a[1:2] &
one(a[3:3])
The 2 values in a[1:2] are split to find time into one-element sub arrays and
now the merging begins.
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn
cn/4 cn/4
…
…
(1) #leaves = n (n)
Total(n lg n)
MERGE SORT ANALYSIS
ADVANTAGES:-
LARGE SIZE LIST
LINKED LIST
EXTERNAL SORTING
STABLE
DISADVANTAGES
EXTRA SPACE REQUIRED
NO SMALL PROBLEM
In Quick sort, the division into 2 sub arrays is made so that the sorted sub
a[i]<=a[j] for all i between 1 & n and all j between (m+1) & n for some m,
1<=m<=n.
Thus the elements in a[1:m] & a[m+1:n] can be independently sorted.
TEXT BOOKS