Daa Unit I
Daa Unit I
Shumama Ansa
Formal Definition
• An Algorithm is a finite set of instructions that, if followed,
accomplishes a particular task.
Case
{ :<condition-1>:<statement-1>
.
.
:<condition-n>:<statement-n>
:else:<statement-n+1>
}
10.Input and output are done using the
instructions read &write.
11.There is only one type of procedure:
Algorithm, the heading takes the form,
Algorithm Name (<Parameter list>)
As an example, the following algorithm fields & 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;
}
In this algorithm (named Max), A & n are procedure
parameters. Result & I are Local variables.
Performance Analysis:
• Performance of an algorithm is a process of making evaluative
judgment about algorithms.
• Performance of an algorithm means predicting the resources
which are required to an algorithm to perform its task.
• That means when we have multiple algorithms to solve a
problem, we need to select a suitable algorithm to solve that
problem.
• We compare all algorithms with each other which are solving
same problem, to select best algorithm.
• To compare algorithms, we use a set of parameters or set of
elements like memory required by that algorithm, execution
speed of that algorithm, easy to understand, easy to
implement, etc.
• Generally, the performance of an algorithm depends
on the following elements...
• SP(I)=0
• Hence s(p)= constant
• Algorithm 2: Iterative function for sum a
list of numbers
Algorithm sum( list[ ], n)
{
tempsum = 0;
for i = 0 ton do
tempsum += list [i];
return tempsum;
}
• T(P)=C+TP(I)
• It is combination of
-Compile time (C) independent of instance characteristics
-Run (execution) time TP dependent of instance characteristics
{ 0 - 0
sum := 0; 1 1 1
for i := 0 to n do 1 n+1 n+1
Algorithm add(a,b) 0 - 0
{ 0 - 0
for i:=1 to m do 1 m+1 m+1
for j:=1 to n do 1 m(n+1) mn+m
c[i,j]:=a[i,j]+b[i,j]; 1 mn mn
} 0 - 0
Total 2mn+2m+1
Example : Matrix multiplication
Statement s/e Frequency Total steps
Algorithm mul(a,b,n) 0
0
{
for i:=1 to m do 1 (n+1) (n+1)
forj:=1 to n do
1 n(n+1) (n^2+n)
c[i][j]=0; 1 n*n n^2
for k:=1 to n do
1 n*n*(n+1) n^3+n^2
c[i,j]+:=a[i,k]*b[k,j]; 1 n*n*n n^3
}
Total 2n^3+3n^22
n+1=
O(n^3)
• The worst-case complexity of the algorithm is the
function defined by the maximum number of steps
taken on any instance of size n. It represents the curve
passing through the highest point of each column.
O(√n)
Examples
for(i=1;i<n;i=i*2) i
1
{
2
Stmt; 4
} 8
Assume 16
i>n k
2^k > n
K=log n
2
O(log n)
Examples
for(i=n;i>=1;i=i/2) i
n
{
n/2
Stmt; n/(2^2)
} n/(2^3)
Assume n/(2^4)
i<1
n/(2^k)
n/(2^k)<1
n= (2^k)
K=log2 n
O(log n)
Types of time functions
• O(1) constant
• O(log n) logarithmic
• O(n) linear
• O(n^2) quadratic
• O(n^3) cubic
• O(2^n) exponential
• 1<log n<√n<n<nlog n< n^2< n^3
<……<2^n<3^n…..<n^n
Asymptotic Notations
Asymptotic Notations
ΟNotation
Ω Notation
θ Notation
Big oh notation: O
Definition
The function f(n)=O(g(n)) (read as “f of n is big oh of g of n”) iff
there exist positive constants c and n0 such that
f(n)≤C*g(n) for all n,n≥0
This notation gives tight upper bound of the given f(n).
The value g(n)is the upper bound value of f(n).
Example:
Consider the following f(n) and g(n)...
f(n) = 3n + 2 and g(n) = n
3n + 2 = O(n)
3n+2=O(n) as
3n+2 ≤4n for alln≥2
Algorithm
Omega notation: Ω
Definition
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≥0
This notation gives tight lower bound of the given f(n).
The value g(n) is the lower bound value of f(n).
Example:
3n+2=Ω (n) as 3n+2 ≥3n for all n≥1
Consider the following f(n) and g(n)...
f(n) = 3n + 2 and g(n) = n
If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n)
>= C g(n) for all values of C > 0 and n0>= 1 f(n) >= C g(n)
⇒3n + 2 >= C n
Above condition is always TRUE for all values of C = 1 and n >=
1.
By using Big - Omega notation we can represent the time
complexity as follows...
3n+2 =Ω(n)
Theta notation: θ
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
C1*g(n) ≤f(n)≤C2*g(n) for all n, n≥0
Example
Example:
3n+2=θ (n) as
3n+2 ≥3n for all n≥2
3n+2 ≤3n for all n≥2
Here c1=3 and c2=4 and n0=2
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy
C1 g(n)<= f(n) >= C2 g(n) for all values of C1, C2 > 0 and n0>= 1
C1 g(n) <= f(n) >= C2 g(n)
C1 n <= 3n + 2 >= C2 n
Above condition is always TRUE for all values of C1 = 1, C2 = 4
and n >= 1.
By using Big - Theta notation we can represent the time
complexity as follows...
3n + 2 = Θ(n)
Little oh: o
Little oh: o
The function f(n)=o(g(n)) iff
Lim f(n)/g(n)=0 for all n,n≥0
n∞
Little omega: ω
•If the sub problems are still relatively large, then the divide-
and- conquer strategy can possibly be reapplied. Often the sub
problems resulting from a divide-and-conquer design are of the
same type as the original problem.
DIVIDE AND CONQUER
• DAndC(Algorithm) is initially invoked as DandC(P), where ‘p’
is the problem to be solved.
Binary search
Quick sort
Merge sort
Strassen’s matrix multiplication.
BINARY SEARCH
• Given a list of n elements arranged in increasing order.
-15,-6,0,7,9,23,54,82,101,112,125,131,142,151.
X=151
Foun
d
Example
Array Elements
-15,-6,0,7,9,23,54,82,101,112,125,131,142,151.
x=-14
-15,-6,0,7,9,23,54,82,101,112,125,131,142,151.
x=9
Found
Time Complexity of binary search
The complexity of binary search is successful
searches is
Algorithm INTERCHANGE(a, i, j)
{
p:= a[i]; a[i]:=a[j; a[j]:=p;
}
Example
40 20 10 80 60 50 7 30 100
Pick Pivot Element
There are a number of ways to pick the pivot element. In
this example, we will use the first element in the array:
40 20 10 80 60 50 7 30 100
Partitioning Array
Given a pivot, partition the elements of the array such that
the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
1. While a[ i] <= a[pivot]
++ i
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
1. While a[ i] <= a[pivot]
++ i
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
1. While a[ i ]<= a[pivot]
++ i
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
1. While a[ i ]<= a[pivot]
++ i
2. While a[ j ] > a[pivot]
-- j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
1. While a[ i ]<= a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
1. While a[ i ]<= a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i< j
swap a[ i ]and a[ j ]
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
1. While a[ i ]<= a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i< j
swap a[ i ]and a[ j ]
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
1. While a[ i ]<= a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i< j
swap a[ i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
1. While a[ i ]<=
a[pivot]
++ i
2. While-- a[j j ]> a[pivot]
3. If i< j
swap a[ i ]and a[ j ]
4. While j i, go to
> 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While a[ i ]<=
a[pivot]
++ i
2. While -- a[j j ]> a[pivot]
3. If i< j
swap a[ i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While a[ i ]<=
a[pivot]
++ i
2. While -- a[j j ]> a[pivot]
3. If i< j
swap a[ i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While a[ i ]<=
a[pivot]
++ i
2. While -- a[j j ]> a[pivot]
3. If i< j
swap a[ i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While a[ i ]<=
a[pivot]
++ i
2. While -- a[j j ]> a[pivot]
3. If i< j
swap a[ i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If i < a[
swap j i ]and a[ j ]
4. While j > i, go to
1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If swap
i < a[j i ]and a[ j ]
4. While j > i, go to
5. Swap a[ j1.]and
a[pivot_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
1. While i ]<=
a[ a[pivot]
++ i
2. While a[ j ]> a[pivot]
-- j
3. If swap
i < a[j i ]and a[ j ]
4. While j > i, go to
5. Swap a[ j1.]and
a[pivot_index]
pivot_index = 4 7 20 10 30 40 50 60 80 100
i j
Partition Result
7 20 10 30 40 50 60 80 100
T (n – 1) = T (n – 2) + C (n –1)
T (n – 2) = T (n – 3) + C (n –2)
- - - - - - --
T (2) = T (1) + C(2)
Adding up all these equations yields
T (n) =O(n2) - (3)
Analysis of Quick Sort
Best Case Analysis
Finally,
Which
yields,
}
MERGE SORT
Algorithm MERGE (low, mid,high)
{
h :=low; i := low; j:= mid + 1;
while((h <mid) and (J <high))do
{
if (a[h] <a[j])then
{
b[i] :=a[h]; h:=h+1;
}
else
{
b[i] :=a[j]; j := j +1;
}
i := i +1;
}
if (h > mid) then
for k := j to high do
{
b[i] := a[k];
i := i +1
}
for k := h to mid do
{
b[i]:= a[K];
i := i +1;
}
for k := low to high do
a[k] :=b[k];
} //end MERGE
Example 1
Analysis of Merge Sort
Analysis of Merge Sort
•For n = 1, the time to merge sort is constant, which we will
be denoted by 1.
• Otherwise, the time to merge sort ‘n’ numbers is equal to the
time to do two recursive merge sorts of size n/2, plus the
time to merge, which is linear.
T(n) = 2T(n/2) + n
= 4T(n/4) + n + n
= 8T(n/8) + n + n + n
= ……
= 2k T(n/2K) + kn
= n.T(1) + nlogn
= n + nlogn
O(nlogn)
}
T(n) = 1 if n<=2
8T(n/2)+n2 n>2
P = (A11+ A22)(B11+B22)
Q = (A21 + A22) * B11 C11 = P + S - T + V
R = A11 * (B12 - B22) C12 = R + T
S = A22 * (B21 - B11) C21 = Q + S
T = (A11 + A12) * B22 C22 = P + R - Q + U
U = (A21 - A11) * (B11 + B12)
V = (A12 - A22) * (B21 + B22)
Time Analysis
T(n) = 1 if n<=2
7T(n/2)+n2 n>2