Daa R20 Unit 1
Daa R20 Unit 1
Sum of n no’s
Abstract solution:
Sum of n elements by adding up elements one at a time.
Abstract solution:
Find the Largest number among a list of elements by considering one element at a
time.
Little more detailed solution:
1. Treat first element as largest element
2. Examine a[2] to a[n] and suppose the largest element is at a [ i ];
3. Assign a[i] to largest;
4. Return largest;
Linear search
Abstract solution:
From the given list of elements compare one by one from first element to last
element for the required element
Abstract solution:
From those elements that are currently unsorted, find the smallest and place it
next in the sorted list.
Bubble Sort
Abstract solution:
Comparing adjacent elements of an array and exchange them if they are not in order. In the
process, the largest element bubbles up to the last position of the array first and then the
second largest element bubbles up to the second last position and so on.
for i := 1 to n do
{
Compare a[1] to a [n-i] pair-wise (each element with its next)
and interchange a[j] and a[j+1] whenever a[j] is greater than a[j+1]
}
Insertion sort
Abstract solution:
Insertion sort works by sorting the first two elements and then inserting the third element in its
proper place so that all three are sorted. Repeating this process, k+1 st element is inserted in its
proper place with respect to the first k elements (which are already sorted) to make all k+1
elements sorted. Finally, 'n' th element is inserted in its proper place with respect to the first n-1
elements so all n elements are sorted.
(variable) :=(expression);
5. There are two Boolean values true and false. In order to produce these values,
the logical operators and, or, and not and the relational operators
<, ≤, =, ≠ , ≥ , and > are provided.
6. Elements of multidimensional arrays are accessed using [ and ]. For example, if A
is a two dimensional array, the (i,j) the element of the array is denoted as A[i,j].
Array indices start at zero.
7. The following looping statements are employed: for, while, and repeat- until.
The while loop takes the following form:
While (condition) do
{
(statement 1)
::::::::::::::
(statement n)
}
As long as (condition) is true, the statements get executed. When (condition)
becomes false, the value of (condition) is evaluated at the top of loop.
The general form of a for loop is
for variable := value 1 to value 2 step step do
{
(statement 1)
::::::::::::::
(statement n)
}
Here value1, value2, and step are arithmetic expressions. A variable of type
integer or real or a numerical constant is a simple form of an arithmetic
expression. The clause “step step” is optional and taken as +1 if it does not occur.
Step could either be positive or negative variable is tested for termination at the
start of each iteration. The for loop can be implemented as a while loop as
follows:
Variable := value1;
fin :=value2;
incr:=step;
while ((variable –fin) * step ≤,0) do
{
(statement 1)
:::::::::::::::
(statement n)
}
A repeat-until statement is constructed as follows :
repeat
(statement 1)
:::::::::::::::
(statement n)
Until (condition)
The statements are executed as long as (condition) is false. The value of
(condition) is computed after executing the statements.
The instruction break; can be used within any of the above looping instructions to
force exit. In case of nested loops, break; results in the exit of the innermost loop
that it is a part of. A return statement within any of the above also will result in
exiting the loops. A return statement results in the exit of the function itself.
8. A conditional statement has the following forms:
if (condition) then (statement)
if (condition) then (statement 1) else (statement 2)
Here (condition) is a boolean expression and (statement),(statement 1),
and (statement 2) are arbitrary statements (simple or compound).
We also employ the following case statement:
Case
{
:(condition 1 ) : (statement 1)
:::::::::::::::
:(condition n ) : (statement 1)
:else : (statement n+1)
}
Here (statement 1) and (statement 2), etc. could be either simple statements or
compound statements. A case statement is interpreted as follows. If (condition 1)
is true, (statement 1) gets executed and the case statements is exited. If
(statement 1) is false,(condition 2) is evaluated. If (condition 2) is true,
(statement 2) gets executed and the case statement exited, and so on. If none of
the conditions (condition 1),…,(condition n) are true, (statement n+1) is executed
and the case statement is exited. The else clause is optional.
9. Input and output are done using the instructions read and write, No format is
used to specify the size of input or output quantities.
10. There is only one type of procedure: Algorithm. An algorithm consists of a
heading and a body. The heading takes the form.
Algorithm Name ((parameter list))
Where Name is the name of the procedure and ((parameter list)) is a listing of the
procedure parameters. The body has one or more (simple or compound)
statements enclosed within braces { and }. An algorithm may or may not return
any values. Simple variables to procedures are passed by value. Arrays and
records are passed by reference. An array name or a record name is treated as a
pointer to the respective data type.
As an example. The following algorithm finds and returns the maximum of n
given numbers:
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;
}
RECURSION
Definition: It is the process of repeating items in a self similar way.
Example 1:
DEFINITION(Computer Science): This is the method of defining a function in which the function
being defined is applied within its own definition.
In simple terms if a function makes a call to itself then it is known as Recursion.
Example3: Observe the recursive definition for a function Suml(n) to compute
Sum of first n natural numbers.
If n=1 then
return 1
else
return n + Sum(n-1)
Steps to be followed to write a Recursive function
2.RECURSIVE STEP: Contains recursive definition for all other cases(other than base case)to reduce
them towards the base case
Note: if any one of the two or both, wrongly defined makes the program infinitely Recursive.
Algorithm RecursiveSum(a,k)
{
if (k<=0) then
Return 0.0;
else
Return (RecursiveSum(a,k-1) + a[k]);
}
Algorithm RecursiveInsertionSort(a,k)
{
if (k > n)
Return; // Do nothing
else
{
RecursiveInsertionSort(a,k-1);
//Insert 'k'th element into k-1 sorted list
value := a[k]; j := k - 1;
done := false;
repeat
if a[j] > value
{
a[j + 1] := a[j]; j := j - 1;
if (j < 0) done := true;
}
else done := true;
until done;
a[j + 1] := value;
}
}
Algorithm bin(int n)
{
if (n=1 or n=0)
{
write n;
return;
}
else
bin=n/2;
write n mod 2;
}
Rules:
(1) Can only move one disk at a time.
(2) A larger disk can never be placed on top of a smaller disk.
(3) May use third post for temporary storage.
Performance Analysis
Space Complexity:
Algorithm abc (a,b,c)
{
return a+b++*c+(a+b-c)/(a+b) +4.0;
}
The Space needed by each of these algorithms is seen to be the sum of the following
component.
1. A fixed part that is independent of the characteristics (eg: number, size)of the inputs and
outputs. The part typically includes the instruction space (ie. Space for the code), space for
simple variable and fixed-size component variables (also called aggregate) space for constants,
and so on.
2. 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 (to the extent that is depends on instance characteristics), 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 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 d by ‘n’ is one word, since it is of type integer.
The space needed by ‘a’ a is the space needed by variables of type array of floating point
numbers.
This is at least ‘n’ words, since ‘a’ must be large enough to hold the ‘n’ elements to be
summed.
So, we obtain Sum(n)>=(n+s)
[ n for a[ ],one each for n, I a& s]
Alogorithm RSum(a,n)
{
If(n<= 0) then
return 0.0;
Else
return RSum(a,n-1)+a[n];
}
Recursive function for Sum
Time Complexity:
A Program step is loosely defined as a syntactically or semantically meaningful segment of a
program that has an execution time that is independent of the instance characteristics.
For Ex: Return a+b+b*c+(a+b-c)/(a+b)+4.0;
Algorithm RSum(a,n)
{
Count :=count+1; // For the if conditional
if (n < 0)then
{
count:=count+1//For the return
return 0.0;
}
else
{
count :=count+1 // For the addition, function invocation and return
return RSum(a,n-1)+a[n];
}
}
Algorithm Recursive sum with count statements added
Algorithm Add(a,b,c,m,n)
{
for i:=1 to m do
{
count :=count +1; // For ‘for i’
for j :=1 to n do
{
count :=count+1; // For ‘for j’
c[i, j] := a[i, j]+b[i, j];
count :=count +1; //For the assignment
}
count := count +1 ;// For loop initialization and last time of ‘for j’
}
count := count+1 ; // For loop initialization and last time of ‘for i’
}
Algorithm for Matrix addition with counting statements
x = tRSum (n-1)
– Mathematical equations
– Determination of the problem size
– Order of magnitude of any algorithm
Asymptotic notations(O,Ω,Ө)
Step count is to compare time complexity of two programs that compute same function and also to
predict the growth in run time as instance characteristics changes. Determining exact step count is
difficult and not necessary also. Since the values are not exact quantities we need only comparative
statements like c1 n2 ≤ tp(n) ≤ c2n2.
For ex: consider two programs with complexities c1n2 + c2 n and c3 n respectively. For small values
of n, complexity depend upon values of c1, c2 and c3. But there will also be an n beyond which
complexity of c3 n is better than that of c1 n2 + c2 n.This value of n is called break-even point. If this
point is zero, c3n is always faster (or at least as fast).
Definition [Big ‘oh’] The function f(n)=O(g(n)) iff there exist positive constants c and no such that
f(n)≤c*g(n) for all n, n ≥ no.
Ex1: f(n) = 2n + 8, and g(n) = n2. Can we find a constant c, so that 2n + 8 <= n2? The number 4 works
here, giving us 16 <= 16.
For any number c greater than 4, this will still work. Since we're trying to generalize this for large
values of n, and small values (1, 2, 3) aren't that important, we can say that f(n) is generally faster than
g(n); that is, f(n) is bound by g(n), and will always be less than it.
Ex7:3n+2≠O(1) as3n+2 not less than or equal to c for any constant c and all n≥n0
Ex 8: 10n2+4n+2≠O(n)
Definition[Omega] The function f(n) =Ώ (g(n) (read as “f of n is omega of g of n'') iff there exist
positive constants c and n0 such that f(n) ≥c *g(n) for all n, n≥ no.
Ex1: The function 3n+2=Ω(n) as 3n+2≥3n for n≥1
(the inequality holds for n≥0,but the definition of Ω requires an n0>0).
3n+2=Ω(n) as 3n+2≥3n for n≥1.
Definition [Theta] The function f(n) = Θ (g(n) ) (read as “f of n is theta of g of n'') iff there exist
positive constants C1,C2, and n0 such that c1g(n)≤ f(n) ≤ c2g(n) for all n, n≥n0
Pb 1: 6n+4= Θ(___) as 6n+4≥________for all n ≥_______and 6n+4 ≤ _______ for all n ≥_______
Ex 2 : 3n + 3 = Ө(n)
Ex 3 : 10n2 + 4n +2 = Ө(n2)
Ex 4: 6* 2n + n2 = Θ (2n)
Ex5:10*log n+4= Θ (log n)
Ex6:3n+2≠ Θ(1)
Ex7:3n + 3 = Ө(n)
Ex 8: 10n2 +4n+2 ≠ Θ (n)
Definition [Little ‘oh’] The Function f (n) =o(g(n)) (read as ‘f of n is little oh of g of n’)iff
lim f(n) =0
n→∞ g(n)
Example:
Definition [Little omega] The function f(n)= ω(g(n)) (read as “ f of n is little omega of g of n”) iff
lim g(n) = 0
n→∞ f(n)