Professional Documents
Culture Documents
DAA Introduction WM
DAA Introduction WM
INTRODUCTION
Contents:
Algorithm
- Definition
- Criterion (Properties of algorithm)
- Different study areas of algorithm
Analysis of algorithms.
Asymptotic notations.
Recurrence Relations
- Substitution method
- Recursion tree
- Masters Theorem
General rules to find complexity and examples.
Practical complexities.
Prerequisites & Objectives
Prerequisites: Before beginning this subject, you should be able to
Read and create algorithms.
Read and create recursive algorithms.
Identify comparison and arithmetic operations.
Use basic algebra (Geometric series & Progression, Recurrence relation).
Definition:
An algorithm is a finite set of instructions that, if followed, accomplishes
a particular task.
OR
An algorithm is a sequence of unambiguous instructions for solving a
problem, i.e., for obtaining a required output for any legitimate input in a
finite amount of time.
OR
An algorithm is a finite set of instructions that accomplishes a particular
task.
OR
A clearly specified set of instructions to solve a problem.
Algorithm 2/3
Power consumption
Correctness ignored in this course.
Analysis of algorithms
How to Analyse an Algorithm:
Example : 1
Time Space
Algorithm
Complexity Complexity
Algorithm Swap(a, b) a- 1
{ b- 1
temp := a; 1 temp - 1
a := b; 1
b := temp; 1
}
f(n) = 3
S(n) = 3 word
Time complexity : f(n) = 3 O (1) constant
Space complexity: S(n) = 3 word O (1) constant
Analysis of algorithms
Frequency count method:
Example : 1 Time Space
Algorithm
Complexity Complexity
Algorithm Sum(A, n) s- 1
{ A- n
s := 0; 1 n- 1
for (i:=0; i<n; i++) n+1 i- 1
s:= s + A[i]; n
return s; 1
}
f(n) = 2n+3
S(n) = n+3 word
Time complexity : f(n) = 2n+3 O (n) Linear
Space complexity : S(n) = n+3 word O (n) Linear
Analysis of algorithms
Frequency count method:
Example : 2 Time Space
Algorithm
Complexity Complexity
Algorithm Add(A, B, n) A- n2
{ B- n2
for (i:=0; i<n; i++) n+1 C- n2
n- 1
for (j:=0; j<n; j++) n*(n+1)
i- 1
C[i, j]:= A[i, j]+B[i, j];
n*n j- 1
}
f(n) = 2 n2+2n+1
S(n) = 3n2+3 word
Time complexity : f(n) = 2 n2+2n+1 O (n2 ) Quadratic
Space complexity : S(n) = 3n2+3 word O (n2) Quadratic
Analysis of algorithms
Frequency count method:
Example : 3
Time To terminate while loop
Algorithm
Complexity a≥b
Algorithm Test(a, b) T(n)
{ a: 1 Since a = 2k
a:= 1; 1*2 = 21 2k ≥ b ie 2k = b
while(a < b) 2*2 = 22
22*2 = 23 k= log2 b
statement 1;
--
a:=a*2; -- T(n) = O (log n)
} 2k
T(b) =
Priori Analysis Vs Posteriori Testing
, O, , o,
Defined for functions over the natural numbers.
o Ex: f(n) = (n2).
o Describes how f(n) grows in comparison to n2.
Define a set of functions; in practice used to compare two
function sizes.
The notations describe different rate-of-growth relations
between the defining function and the defined set of
functions.
Asymptotic notations cont….
T(n) = T(n-1) + log n + 1 T(n) = log n + log (n-1) + ------- + log 2 + log 1
= log [ n * (n-1) * - - - - * 2 * 1]
= log n! No time bound.
Use Upper bound
T (n) = O(n log n)
Recurrence Relation 4/8
Masters Theorem :
A) For decreasing functions:
Let be constants, let be a function, and
T(n) be defined on the nonnegative integers by the recurrence relation
then
Case :
1: if a < 1 then O (nk) OR O (f(n)) .
2: if a = 1 then O (nk+1) OR O (n * f(n)) .
3: if a > 1 then O (nk * an/b) OR O (f(n) * an/b ) .
Examples:
1) T(n) = T(n-1) + 1 T(n) = O(n)
2) T(n) = T(n-1) + n T(n) = O(n2)
3) T(n) = T(n-1) + log n T(n) = O(n log n)
4) T(n) = 2T(n-2) + 1 T(n) = O(2n/2)
5) T(n) = 2T(n-1) + n T(n) = O(n 2n)
Recurrence Relation 5/8
Masters Theorem :
B) For dividing functions:
Let a ≥ 1, b > 1 be constants, let be a function, and let
T(n) be defined on the nonnegative integers by the recurrence relation
then
Recurrence Relation 6/8
Masters Theorem :
B) For dividing functions: Examples for case 1
1) 3)
a = 2, b = 2, k = 0, p = 0 a = 8, b = 2, k = 1, p = 1
2)
a = 4, b = 2, k = 1, p = 0
Then T(n) = =
Recurrence Relation 7/8
Masters Theorem :
B) For dividing functions: Examples for case 2
1) 3)
a = 4, b = 2, k = 2, p = 0 a = 2, b = 2, k = 1, p = -2
Since p > -1 case a. Since p < -1 case c.
2)
a = 2, b = 2, k = 1, p = -1
Since p = -1 case b.
Then T(n) = =
Recurrence Relation 8/8
Masters Theorem :
B) For dividing functions: Examples for case 3
1)
a = 1, b = 2, k = 2, p = 0
Since p ≥ 0 case a.
Then T(n) = =
2)
a = 4, b = 2, k = 3, p = -1
Since p < 0 case b.
Then T(n) = =
General Rules 1/2
Rule 1- for Loop: The running time of a for loop is at most the
running time of the statements inside the for loop (including tests) times
the number of iterations.
As an example: The following program fragment is O (n) .
Rule 3- Nested loop: Analyze these inside out. The total running time of a
statement inside a group of nested loop is the running time of the statement
multiplied by the product of the sizes of all the loops.
As an example: The following program fragment is O (n2)
for (i=0; i<n; i++)
for ( j=0; j<n; j++)
k++;
Rule 4- Consecutive Statements: These just add (which means that the
maximum is the one that counts) .
As an example: The following program fragment is O (n2).
for (i=0; i<n; i++)
a[i] =0;
for (i=0; i<n; i++)
for ( j=0; j<n; j++)
k++;
Practical complexities
O(1) : constant
O(n) : linear
O(n^2) : quadratic
O(n^3) : cubic
O(2^n) : exponential
O(log n) : logarithmic
O(n log n) :
Plot of Function values
Plot of Practical complexities
That’s it.
CPU Time calculation
Time CPU spends computing for some task/set of instruction/program excluding I/O
delays, any other delays.
Functions to be used:
clock(); clock_t
time_taken = ((double)t)/CLOCKS_PER_SEC;