Professional Documents
Culture Documents
Unit I
Unit I
DesignAND ANALYSIS
and OF of
Analysis
ALGORITHMS
Algorithms
From,
Ashwini Janagal
Associate Professor
Department of ISE,
JNN College of Engineering,
Shivamogga-577204,
Karnataka,
India.
What is an Algorithm?
I. Introduction: What is an Algorithm
DEFINITION:
An ALgorithm is a finite set of instructions that, if followed,
accomplishes a particular tas. In addition, all algorithms must satisfy
the 5 crieteria.
1. Input: Zero or More
2. Output : At least One Quantity
3. Definitness: Clear and Unambiguous.
4. Finiteness: Algorithm terminates
5. Effectiveness: Feasible and Basic.
What is an Algorithm?
I. Introduction: What is an Algorithm
What are the possible inputs given for the task (0 or more) ?
Example:
Input: 2 signed integers / 2signed real numbers / 1 integer and one real
number etc.
What is an Algorithm?
I. Introduction: What is an Algorithm
What are the possible inputs given for the task (0 or more) ?
Example:
Quantity Produced
Example:
Task: To add 2 whole numbers.
For all the different I/Ps the program is going to end somewhere.
Example:
While(i>=100)
{
printf(“HI WE ARE IN WHILE LOOP\n”);
}
Do you think this programme will terminate?
What is an Algorithm?
I. Introduction: What is an Algorithm
Study of Algorithm
Example;
Task: Binary Search. (If numbers are not sorted then sort them and then search)
Best: If numbers are already in Ascending Order
Average?
Worst?
What is an Algorithm?
Study of Algorithm
Program testing
Debugging Profiling
What is an Algorithm?
Study of Algorithm
Program Testing
How to Device algorithm?
Statement
<variable>:= <Expression>
Algorithm Specification
Pseudocode Conventions
7. Looping Statements
while <condition> do
{
<statement 1>
:
:
<statement n>
}
Algorithm Specification
Pseudocode Conventions
7. Looping Statements
for variable:=val1 to val2 step step do
{
<statement 1>
:
:
<statement n>
}
Algorithm Specification
Pseudocode Conventions
7. Looping Statements
Can you convert a for loop to while loop????
Algorithm Specification
Pseudocode Conventions
7. Looping Statements
Only loop where loop will
repeat
execute as long as condition
is false
<statement 1>
:
:
<statement n>
until <condition>
Algorithm Specification
Pseudocode Conventions
8. Conditional Statement
if <condition> then <statement>
if <condition> then <statement 1> else <statement 2>
case
{
:<condition1>:<statement 1>
:
:
:<condition n>:<statement n>
:else:<statement n+1>
}
Algorithm Specification
Pseudocode Conventions
for i:=1 to n do
{
Exaxmine a[i] to a[n] and suppose a[k] is the smallest;
Assignment
Assignment
if-then-else
if-then-else
Recursion
While Loop
Algorithm Specification
Recursive Algorithms
of their size
Algorithm Specification
Recursive Algorithms
C with A as temporary
stop.
Algorithm Specification
Recursive Algorithms
Algorithm TowersOfHanoi(n,A,C,B)
//TO me Disks from A to C
{
if(n≥1)
{
//n-1 disks from A to B
TowersOfHanoi(n-1,A,B,C);
Move the Disk from A to C
//n-1 disks from B to C
TowersOfHanoi(n-1,B,C,A);
}
}
PERFORMANCE ANALYSIS
HOW MUCH
HOW FAST ?
MEMORY IS USED ?
Algorithm abc(a,b,c)
{
Types of Space
return a+b+b*c+(a+b-c)/(a+b)+4.0;
}
1. Fixed Part: Size of data types, Space for code,
Algorithm Sum(a,n)
{ simple variables, fixed size variables etc.
s:=0.0;
2. Variable
for i:=1Part:
to n Size
do varies in the course of
s:=s+a[i];
execution.
return Example
s; may be a pointer variable and
}
Dynamic Memory Allocation.
Algorithm RSum(a,n)
{
if(n≤0) then return 0.0;
else return RSum(a,n-1)+a[n]
}
PERFORMANCE ANALYSIS
Space Complexity
Algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0;
} Space requirement of an Algorithm P = S(P)=c+Sp
Algorithm Sum(a,n)
c = Constant (Fixed Part)
{
s:=0.0; Part (Instance Characteristics)
Sp=Variable
for i:=1 to n do
s:=s+a[i];
return s;
}
Algorithm RSum(a,n)
{
if(n≤0) then return 0.0;
else return RSum(a,n-1)+a[n]
}
PERFORMANCE ANALYSIS
Space Complexity
Algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0;
}
Algorithm Sum(a,n)
{
s:=0.0;
for i:=1 to n do
s:=s+a[i];
1. return
Fixed Part:
s; a, b and c need 3 words.
}
2. Variable Part Sp = 0
Algorithm RSum(a,n)
{
if(n≤0) then return 0.0;
else return RSum(a,n-1)+a[n]
}
PERFORMANCE ANALYSIS
1. Fixed Part: Array “a” need “n” words
Space Complexity
“n”, “i” and “s” need 3 words.
Algorithm abc(a,b,c)
2. Variable Part Sp= 0
{
return a+b+b*c+(a+b-c)/(a+b)+4.0;
Therefore SSum(n)≥ n+3
}
Algorithm Sum(a,n)
{
s:=0.0;
for i:=1 to n do
s:=s+a[i];
return s;
}
Algorithm RSum(a,n)
{
if(n≤0) then return 0.0;
else return RSum(a,n-1)+a[n]
}
PERFORMANCE
Here we ANALYSIS
need to find the depth of
Space Complexity
Stack....Because when function call is made
arguments are pushed to system Stack.
Algorithm abc(a,b,c)
{ Assumption: Return Address 1 word.
return
Each a+b+b*c+(a+b-c)/(a+b)+4.0;
time function call is made ‘n’, a[n] and
}
return address are pushed to stack
Algorithm Sum(a,n)
Depth of recursion is (n+1) (n, n-1, n-2,..., 3,
{
2,1 and 0 also for false conditioning)
s:=0.0;
SRSum(a,n)≥
for i:=1 to n 3(n+1)
do
s:=s+a[i];
return s;
}
Algorithm RSum(a,n)
{
if(n≤0) then return 0.0;
else return RSum(a,n-1)+a[n]
}
PERFORMANCE ANALYSIS
Time Complexity
and Execution.
without compiling.
multiplication etc.
PERFORMANCE ANALYSIS
Time Complexity
Well this also is not accurate as many other programs are running in
instance.
Example:
return a+b+b*c+(a+b-c)/(a+b)+4.0;
step.
Rules:
1. Comments --> 0 Steps;
2. ASsignment (Without any function call) --> 1 step;
3. while (<expr>) has step count of expr.
4. for i:=<expr> to <expr1> do
First entry step count=step count of <expr> + step count of <expr1>
Remaining entry= step count <expr1>
PERFORMANCE ANALYSIS
Time Complexity
Original Code
Algorithm Sum(a,n)
{
s:=0.0;
for i:=1 to n do
s:=s+a[i];
return s;
}
PERFORMANCE ANALYSIS
Time Complexity
that instance.
MATRIX ADDITION
Original Code
Algorithm Add(a,b,c,m,n)
{
for i:=1 to m do
for j:=1 to n do
c[i,j]:=a[i,j]+b[i,j];
}
PERFORMANCE ANALYSIS
Time Complexity
Algorithm Add(a,b,c,m,n)
{
for i:=1 to m do
{
count:=count+1;
for j:=1 to n do
{
count:=count+1;
c[i,j]:=a[i,j]+b[i,j];
count:=count+1;
}
count:=count+1;
}
count:=count+1;
}
PERFORMANCE ANALYSIS
Time Complexity
Algorithm Add(a,b,c,m,n)
{ n times for each value of i
for i:=1 to m do
{ So this for loop is executed m×n times
count:=count+1;
for j:=1 to n do
{
count:=count+1;
c[i,j]:=a[i,j]+b[i,j];
count:=count+1;
}
count:=count+1;
}
count:=count+1;
}
PERFORMANCE ANALYSIS
Time Complexity
n times for each value of i
Original CodeSo+ this
Step for loop is executed m×n times
CountEmbedded
Also we can see two increments in
Algorithm Add(a,b,c,m,n)
{ count so 2×m×n
for i:=1 to m do
{
count:=count+1;
for j:=1 to n do
{
count:=count+1;
c[i,j]:=a[i,j]+b[i,j];
count:=count+1;
}
count:=count+1;
}
count:=count+1;
}
PERFORMANCE ANALYSIS
Time Complexity
This for loop is executed m times
Also
Original Code we CountEmbedded
+ Step can see two increments in
count so 2×m
Algorithm Add(a,b,c,m,n)
{
for i:=1 to m do
{
count:=count+1;
for j:=1 to n do
{
count:=count+1;
c[i,j]:=a[i,j]+b[i,j];
count:=count+1;
}
count:=count+1;
}
count:=count+1;
}
PERFORMANCE ANALYSIS
Time Complexity
Algorithm Add(a,b,c,m,n)
{
for i:=1 to m do
{
count:=count+1;
for j:=1 to n do
{
count:=count+1;
Last count of 1
c[i,j]:=a[i,j]+b[i,j];
count:=count+1;
}
count:=count+1;
}
count:=count+1;
}
PERFORMANCE ANALYSIS
Time Complexity
Original Code + Step CountEmbedded
Algorithm Add(a,b,c,m,n)
{
for i:=1 to m do
{
Everything will add upto
count:=count+1;
for j:=1 to n do
{
count:=count+1;
c[i,j]:=a[i,j]+b[i,j];
count:=count+1;
}
count:=count+1;
}
count:=count+1;
}
PERFORMANCE ANALYSIS
Time Complexity
Example :1
Statements s/e Frequency Total
Steps
Algorithm Sum(a,n) 0 0
{ 0 0
1
s:=0.0; 1 1
n+1
for i:=1 to n do 1 n+1
n
s:=s+a[i]; 1 n
1
return s; 1 1
} 0 0
2n+3
PERFORMANCE ANALYSIS
Time Complexity : Example
x=
Statements s/e Frequency Total
n=0 n>0 Steps
Algorithm RSum(a,n) 0 0 0
{ 0
1 1
if(n≤0) then 1 1 1
1 0
return 0.0; 1 1 0
else 0
0 1
return RSum(a,n-1)+a[n]; 1+x 0 1+x
2 2+x
PERFORMANCE ANALYSIS
Time Complexity
Algorithm Fibonocci(n)
{
if(n≤1) then
write(n);
else
{
fnm2:=0; fnm1:=1;
for i:=2 to n do
{
fn:=fnm1+fnm2;
fnm2:=fnm1; fnm1:=fn;
}
write(fn);
}
}
PERFORMANCE ANALYSIS
1+1--> For loop Entry
Time Complexity : Example
Statements s/e Frequency Total Steps
n=0/1 n>1 n=0/1 n>1
Algorithm Fibonocci(n) 0 0 0
{
if(n≤1) then 1 1 0 1 0
write (n); 1 1 0 1 0
else
{
fnm2:=0; fnm1:=1; 2 0 1 0 2
for i:=2 to n do 1 n 0 n
0
{
fn:=fnm1+fnm2; 1 0 n-1 0 n-1
fnm2:=fnm1; fnm1:=fn; 2 n-1 0 2(n-1)
0
}
write (fn); 1 0 1 0 1+1
}
}
PERFORMANCE ANALYSIS
Time Complexity : Example
Statements = s/e Frequency Total Steps
= n=0/1 n>1 n=0/1 n>1
Algorithm Fibonocci(n) 0 0 0
{
if(n≤1) then 1 1 0 1 0
write (n); 1 1 0 1 0
else
{
fnm2:=0; fnm1:=1; 2 0 1 0 2
for i:=2 to n do 1 n 0 n
0
{
fn:=fnm1+fnm2; 1 0 n-1 0 n-1
fnm2:=fnm1; fnm1:=fn; 2 n-1 0 2(n-1)
0
}
write (fn); 1 0 1 0 1+1
}
}
2
2+n+n-
1+2n-2+1+1
Efficiency
PERFORMANCE ANALYSIS Framework
Units for Measuring Running Time
What next?
So what is “BASIC
OPERATION”
Efficiency
PERFORMANCE ANALYSIS Framework
Units for Measuring Running Time
So what is “BASIC
OPERATION”?
So what is “BASIC
OPERATION”? in SORTING
the array
Efficiency
PERFORMANCE ANALYSIS Framework
Units for Measuring Running Time
So what is “BASIC
OPERATION”? in SORTING
the array
So what is “BASIC
OPERATION”? in MATRIX
MULTIPLICATION
Efficiency
PERFORMANCE ANALYSIS Framework
Units for Measuring Running Time
ARRAY
10 15 20 80 44 64 35 67
Efficiency
PERFORMANCE ANALYSIS Framework
Units for Measuring Running Time
Running Time
1
If
t ( n ) n ( n 1)
2
and we double the input size then compare t(n)
1
t ( n ) n ( n 1)
2
1 2 1
t (n) n n
2 2
PERFORMANCE ANALYSIS
Units for Measuring Running Time
If 1
t ( n ) n ( n 1)
2
and we double the input size then compare
t(n)
1
t ( n ) n ( n 1)
2
1 2 1 VERY SMALL
t (n) n n
2 2
PERFORMANCE ANALYSIS
Units for Measuring Running Time
If 1
t ( n ) n ( n 1)
2
and we double the input size then compare
t(n)
1
t ( n ) n ( n 1)
2
1 2 1 1 2
t (n) n n n
2 2 2
PERFORMANCE ANALYSIS
Units for Measuring Running Time NOW WE
COMPARE
If 1
t ( n ) n ( n 1)
2
and we double the input size then compare t(n)
1 2 1 2
( 2n ) 4n
t ( 2n ) 2 2
4
t (n) 1 2 1 2
n n
2 2
PERFORMANCE ANALYSIS
Units for Measuring Running Time NOW WE
COMPARE
If 1
t ( n ) n ( n 1)
2
and we double the input size then compare t(n)
1 2 1 2
( 2n ) 4n
t ( 2n ) 2 2
4
t (n) 1 2 1 2
n n
2 2 Time taken
is 4 times
more
Asymptotic Notation
Asymptotic Growth
Asymptotic Notation for Growth in Algorithms
running time Vs. Input Size
Order of Growth
a set of functions whose asymptotic growth behavior
is considered equivalent
Increases by 1
Order of Growth
Increase
Order of Growth
4 times more
Successful (p=1)
Unsuccessful (p=0)
Worst-Case, Best-Case, and Average-Case Efficiencies
Amotized Efficiency
Worst case in single run but when mixed with
other operation and run together give better
performance.
Asymptotic Notations and Basic Efficiency Classes
Examples
n O(n 2 ) n 3 O(n 2 )
100n 5 O ( n 2 ) 0.00001n 3 O ( n 2 )
1
n ( n 1) O ( n 2 ) n 4 n 1 O(n 2 )
2
Asymptotic Notations and Basic Efficiency Classes
Examples
n 3 ( n 2 )
1
2
n ( n 1) ( n 2 ) 100n 5 ( n ) 2
n 4 n 1 ( n 2 )
Asymptotic Notations and Basic Efficiency Classes
Examples
an bn c ( n ) Ifa 0
2 2
n sin n ( n )
2 2
Asymptotic Notations and Basic Efficiency Classes
Definition:
if t(n) is bounded above by some constant multiple of g(n) for all large n, i.e.,
if there exist some positive constant c and some non negative integer n0 such
that
Asymptotic Notations and
Asymptotic Notations and Basic Efficiency Classes
Basic Efficiency Classes
Example: Remember
Asymptotic Notations and
Asymptotic Notations and Basic Efficiency Classes
Basic Efficiency Classes
Example: Remember
Asymptotic Notations and
Asymptotic Notations and Basic Efficiency Classes
Basic Efficiency Classes
Definition:
if t(n) is bounded above by some constant multiple of g(n) for all large n, i.e.,
if there exist some positive constant c and some non negative integer n0 such
that
Asymptotic Notations and
Asymptotic Notations and Basic Efficiency Classes
Basic Efficiency Classes
Example: Remember
Asymptotic Notations and
Asymptotic Notations and Basic Efficiency Classes
Basic Efficiency Classes
Example: Remember
Asymptotic Notations and Basic Efficiency Classes
Definition:
if t(n) is bounded both below and above by some constant multiple of g(n) for
all large n, i.e., if there exist some positive constants c1 and c2 and some non
Example: Remember
Prove
Upper Bound =
Lower Bound =
So we choose
Asymptotic Notations and Basic Efficiency Classes
Theorem:
If
Theorem:
If
Step 1: If we take arbitrary 4 real numbers like a1, b1, a2 and b2.
And if , Then a1+a2 ≤ 2max{b1,b2}
Step 2: Since
Similarly
Step 3:
Asymptotic Notations and Basic Efficiency Classes
Useful Property Involving the Asymptotic Notations
Theorem:
If
Step 4:
Asymptotic Notations and Basic Efficiency Classes
Useful Property Involving the Asymptotic Notations
Theorem:
If
Step 5:
Therefore,
Constant c=
Asymptotic Notations and Basic
Asymptotic Efficiency
Notations and Classes
Basic Efficiency Classes
Useful Property Involving the Asymptotic Notations
lim =
n−>∞ g(n)
{
t(n) 0Impliesthat (n)has Smal erOrderof growthcompared
¿
g(n)¿c>0Impliesthat (n)has Sameorder of growthcompared¿g(n)¿∞Impliesthat (n)has Larger order of growthcompared¿g(n)¿ }
{ }
𝑡 (𝑛) 0 𝐼𝑚𝑝𝑙𝑖𝑒𝑠𝑡ℎ𝑎𝑡 𝑡 (𝑛)∈ 𝑂 (𝑔 (𝑛))
lim = 𝑐 >0 𝐼𝑚𝑝𝑙𝑖𝑒𝑠 𝑡ℎ𝑎𝑡 𝑡 (𝑛)∈ 𝜃 (𝑔 (𝑛))
𝑛−>∞ 𝑔(𝑛)
∞ 𝐼𝑚𝑝𝑙𝑖𝑒𝑠 𝑡ℎ𝑎𝑡 𝑡 ( 𝑛) ∈ 𝛺 (𝑔 (𝑛))
Asymptotic Notations and Basic
Asymptotic Efficiency
Notations and Classes
Basic Efficiency Classes
Using Limits for Comparing Order of Growth
{ }
t (n) 0 Implies that t (n) ∈O ( g (n))
lim = c > 0 Implies that t ( n) ∈ θ( g (n))
n −>∞ g (n)
∞ Implies that t ( n)∈ Ω ( g (n))
EXAMPLE 1: Compare the orders of growth of n(n - 1) and n2.
= ==
A Constant. Therefore
Asymptotic Notations and Basic
Asymptotic Efficiency
Notations and Classes
Basic Efficiency Classes
Using Limits for Comparing Order of Growth
{ }
t (n) 0 Implies that t (n) ∈O ( g (n))
lim = c > 0 Implies that t ( n) ∈ θ( g (n))
n −>∞ g (n)
∞ Implies that t ( n)∈ Ω ( g (n))
L'Hopital's rule =
= = 2 =0 log2n
Asymptotic Notations and Basic
Asymptotic Efficiency
Notations and Classes
Basic Efficiency Classes
Using Limits for Comparing Order of Growth
{ }
t (n) 0 Implies that t (n) ∈O ( g (n))
lim = c > 0 Implies that t ( n) ∈ θ( g (n))
n −>∞ g (n)
∞ Implies that t ( n)∈ Ω ( g (n))
EXAMPLE 2 Compare the orders of growth of n! and 2.
= == =
n!
Asymptotic Notations and Basic Efficiency Classes
Asymptotic Notations and Basic Efficiency Classes
Basic Efficiency Classes
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
A 12 34 23 67 45 16 26 55 1
i
Maxval
n 9
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
0 1 2 3 4 5 6 7 8
A 12 34 23 67 45 16 26 55 1
i
Maxval 12
n 9
Mathematical Analysis of Non-Recursive Algorithms
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
0 1 2 3 4 5 6 7 8
A 12 34 23 67 45 16 26 55 1
i 1
Maxval 12
n 9
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
0 1 2 3 4 5 6 7 8
A 12 34 23 67 45 16 26 55 1
i 1
Maxval 12
n 9
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
ALGORITHM MaxElernent(A[O .. n -
1])
maxval <-- A[0]
for i<--1 to n-1 do
if A[i]>maxval
maxval<-- A[i]
return maxval
0 1 2 3 4
A 12 34 23 67 45
i 1
Maxval 12 34
n 9
Mathematical Analysis of Non-Recursive Algorithms
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
Ex1: Finding the value of the largest element in a list of ‘n’ numbers
0 1 2 3 4
A 12 34 23 67 23
i
j
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j 1
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j 12
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j 1 2 3
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j 123
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j 1 23 4
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j 1 23 4
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 0
j 1 234 5
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 234 5
n 5
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 2345 2
n 5
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 2345 2
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 2345 23
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 2345 23
n 5
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 2345 23 4
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 2345 23 4
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01
j 1 2345 234 5
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01 2
j 1 2345 234 5
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01 2
j 1 2345 2345 3
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01 2
j 1 2345 2345 3
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01 2
j 1 2345 2345 3
4
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01 2
j 1 2345 2345 3
4
n 5
Mathematical Analysis of Non-Recursive Algorithms
0 1 2 3 4
A 12 34 23 67 23
i 01 2
j 1 2345 2345 3
4
n 5
Asymptotic
Mathematical Analysis Notations and
of Non-Recursive Basic Efficiency Classes
Algorithms
cworst(n) =
Mathematical Analysis of Non-Recursive Algorithms
cworst(n) =
=
cworst(n) = =
n-1 = n-1
Mathematical Analysis of Non-Recursive Algorithms
n-1
(n-1)(n-2-0+1)-
𝑛 −2
𝐶 𝑊𝑜𝑟𝑠𝑡 (𝑛)=(𝑛 −1)(𝑛 −1 − )
2
Mathematical Analysis of Non-Recursive Algorithms
𝑛 −2
𝐶 𝑊𝑜𝑟𝑠𝑡 (𝑛)=(𝑛 −1)(𝑛 −1 − )
2
𝐶 𝑊𝑜𝑟𝑠𝑡 (𝑛)=(𝑛 − 1 )
[ ]
𝑛
2
𝑛 (𝑛− 1)
𝐶 𝑊𝑜𝑟𝑠𝑡 (𝑛)=
2
=
Mathematical Analysis of Non-Recursive Algorithms A 0 1
0 11 12
ALGORITHM MatrixMultiplication(A[O .. n- 1, O ..
n- 1],
00 01 10 11
B[O .. n- 1, O .. n- 1])
A 11 12 13 14
for ito n-1 do B
C
for j
i
c[i,j]=0
j
for k
return c
Mathematical Analysis of Non-Recursive Algorithms
B 0 1
0 15 16
Ex1: MATRIX MULTIPLICATION
1 17 18
ALGORITHM MatrixMultiplication(A[O .. n- 1, O ..
n- 1],
00 01 10 11
B[O .. n- 1, O .. n- 1]) A 11 12 13 14
for k
return c
A 0 1
B 0 1
ALGORITHM MatrixMultiplication(A[O .. n- 1, O ..
0 15 16
n- 1],
1 17 18
B[O .. n- 1, O .. n- 1])
00 01 10 11
for ito n-1 do
A 11 12 13 14
for j B 15 16 17 18
c[i,j]=0 C
i
for
j
C[i, j]+= C[i, j]+ A[i, k]*B[k, j] k
return c n 2
A 0 1
B[O .. n- 1, O .. n- 1]) A 11 12 13 14
B 15 16 17 18
for ito n-1 do
C
for j i 0
c[i,j]=0 j
k
for
n 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
return c
A 0 1
B[O .. n- 1, O .. n- 1]) 00 01 10 11
A 11 12 13 14
for ito n-1 do
B 15 16 17 18
for j C
c[i,j]=0 i 0
j 0
for
k
C[i, j]+= C[i, j]+ A[i, k]*B[k,
n j]
2
return c
Mathematical Analysis A Algorithms
of Non-Recursive 0 1 B 0 1
C 0 1
0 11 12 0 15 16
0 0
Ex1: 1MATRIX MULTIPLICATION 1 13 14 1 17 18
c[i,j]=0 i 0
j 0
for
k
C[i, j]+= C[i, j]+ A[i, k]*B[k,
n j]
2
return c
C 0 1 A 0 1 B 0 1
Mathematical
0 0 Analysis of Non-Recursive Algorithms
0 11 12 0 15 16
1 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1, O ..15
n- 1],
B 16 17 18
B[O .. n- 1, O .. n- 1]) C 0
for
return c
A 0 1
B 0 1
C 0 1 0 and
11 Basic
12 Efficiency Classes
Mathematical Analysis ofAsymptotic Notations
Non-Recursive Algorithms 0 15 16
0 0 1 13 14
1 17 18
1
Ex1: MATRIX MULTIPLICATION
return c
A 0 1 B 0 1
C 0
Mathematical 1
Analysis of Non-Recursive Algorithms
0 11 12 0 15 16
0 165
1 13 14 1 17 18
1
Ex1: MATRIX MULTIPLICATION
for j i 0
j 0
c[i,j]=0
k 01
for
n 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
return c
C 0 1 A 0 1
Mathematical Analysis of Non-Recursive Algorithms B 0 1
0 165 0 11 12
0 15 16
1 1 13 14
Ex1: MATRIX MULTIPLICATION 1 17 18
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
B 15 16 17 18
B[O .. n- 1, O .. n- 1])
C 369
forC[0,0]+=C[0]
ito n-1 [0]+A[0]
do [1]*B[1] [0]
i 0
C[0,0]+=165+12*17=369
for j j 0
k 1
c[i,j]=0
n 2
for
return c
C
Mathematical 0 1 of Non-Recursive Algorithms
Analysis A 0 1 B 0 1
0 369 0 11 12 0 15 16
1
Ex1: MATRIX MULTIPLICATION 1 13 14 1 17 18
00 01 10 11
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. 11n- 1],
A 12 13 14
B[O .. n- 1, O .. n- 1]) B 15 16 17 18
for n 2
return c
A 0 1
C 0 1 B 0 1
Mathematical Analysis of Non-Recursive0 Algorithms
11 12
0 369 0 15 16
1 13 14
1 1 17 18
Ex1: MATRIX MULTIPLICATION
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n-
1], 00 01 10 11
A 11 12 13 14
B[O .. n- 1, O .. n- 1])
B 15 16 17 18
for ito n-1 do
C 369
for j i 0
c[i,j]=0 j 01
k 01 2
for
n 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
return c
A 0 1 B 0 1
C 0
Mathematical 1
Analysis of Non-Recursive Algorithms
0 11 12 0 15 16
0 369 0
1 13 14 1 17 18
Ex1:1 MATRIX MULTIPLICATION
00 01 10 11
ALGORITHM MatrixMultiplication(A[O .. n- 1, AO .. 11n- 12 13 14
1], B 15 16 17 18
B[O .. n- 1, O .. n- 1]) C 0
i 0
for ito n-1 do
j 01
for j k 01 2
c[i,j]=0 n 2
for
return c
A 0 1 B 0 1
C 0
Mathematical 1
Analysis of Non-Recursive Algorithms
0 11 12 0 15 16
0 369 0
1 13 14 1 17 18
Ex1:1 MATRIX MULTIPLICATION
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
B 15 16 17 18
B[O .. n- 1, O .. n- 1])
C 0
for ito n-1 do i 0
for j j 01
k 01 2 0
c[i,j]=0
n 2
for
return c
B 0 1
C 0 1 A 0 1
Mathematical Analysis of Non-Recursive Algorithms 0 15 16
0 369 0 0 11 12
1 17 18
1 1 13 14
Ex1: MATRIX MULTIPLICATION
00 01 10 11
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
A 11 12 13 14
B[O .. n- 1, O .. n- 1])
B 15 16 17 18
forC[0,1]+=C[0]
ito n-1 do[1]+A[0] [0]*B[0] [1] C 369 176
C[0,1]+=0+11*16=176
for j i 0
j 01
c[i,j]=0
k 01 2 0
for n 2
return c
C 0 1
A 0
Mathematical Analysis of Non-Recursive Algorithms 1 B 0 1
0 369 176
0 11 12 0 15 16
1
Ex1: MATRIX MULTIPLICATION 1 13 14 1 17 18
00 n- 01
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. 1], 10 11
A 11 12 13 14
B[O .. n- 1, O .. n- 1])
B 15 16 17 18
for ito n-1 do C 369 176
for j i 0
j 01
c[i,j]=0
k 01 2 01
for
n 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
return c
C 0 1 A 0 1 B 0 1
Mathematical
0 369 176 Analysis of Non-Recursive
0 11 Algorithms
12 0 15 16
1 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
B 15 16 17 18
B[O .. n- 1, O .. n- 1])
C 369 392
forC[0,1]+=C[0][1]+A[0][1]*B[1][1]
ito n-1 do i 0
C[0,1]+=176+12*18=392
for j j 01
k 01 2 01
c[i,j]=0
n 2
for
return c
C 0 1
A
Mathematical Analysis of Non-Recursive 0 1
Algorithms B 0 1
0 369 392
0 11 12 0 15 16
1
Ex1: MATRIX MULTIPLICATION 1 13 14 1 17 18
00 01 10 11
ALGORITHM MatrixMultiplication(A[O .. n- 1,AO ..11n- 1],
12 13 14
B 15 16 17 18
B[O .. n- 1, O .. n- 1])
C 369 392
for ito n-1 do
i 0
for j j 01
c[i,j]=0 k 01 2 01 2
n 2
for
return c
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive Algorithms
0 369 392 0 11 12 0 15 16
1 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
B[O .. n- 1, O .. n- 1])
00 01 10 11
for ito n-1 do
A 11 12 13 14
for j
B 15 16 17 18
c[i,j]=0 C 369 392
i 0
for
j 01 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 01 2 01 2
return c n 2
C 0
Mathematical1Analysis of Non-Recursive
A 0 Algorithms
1 B 0 1
0 369 392 0 11 12 0 15 16
00 n- 01
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. 1], 10 11
A 11 12 13 14
B[O .. n- 1, O .. n- 1])
B 15 16 17 18
for ito n-1 do C 369 392
for j i 01
j 01 2
c[i,j]=0
k 01 2 01 2
for
n 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
return c
C 0 1
Mathematical A Algorithms
0 369 392Analysis of Non-Recursive 0 1 B 0 1
1 0 11 12 0 15 16
for j i 01
j 01 2 0
c[i,j]=0
k 01 2 01 2
for
n 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
return c
C 0 1 A 0 1 B 0 1
Mathematical
0 369 392
Analysis of Non-Recursive
0 11 12
Algorithms
0 15 16
1 0 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
B 15 16 17 18
B[O .. n- 1, O .. n- 1])
C 369 392 0
for ito n-1 do i 01
for j j 012 0
k 012 01 2
c[i,j]=0
n 2
for
return c
C 0 1
A 0 1 B 0 1
Mathematical
0 369 392 Analysis of Non-Recursive Algorithms
0 11 12 0 15 16
1 0
1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1,15O ..16n- 1],
B 17 18
B[O .. n- 1, O .. n- 1]) C 369 392 0
for
return c
C 0 1 A 0 1 B 0 1
Mathematical
0 369 392 Analysis of Non-Recursive
0 Algorithms
11 12 0 15 16
1 0 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
B[O .. n- 1, O .. n- 1])
00 01 10 11
forC[1,0]+=C[1][0]+A[1][0]*B[0][0]
ito n-1 do
A 11 12 13 14
for j
C[1,0]+=0+13*15=195 B 15 16 17 18
c[i,j]=0 C 369 392 195
i 01
for
j 012 0
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 012 012 0
return c n 2
C 0 1 A 0 1
Mathematical Analysis of Non-Recursive Algorithms B 0 1
0 369 392 0 11 12
0 15 16
1 195 1 13 14
Ex1: MATRIX MULTIPLICATION 1 17 18
B[O .. n- 1, O .. n- 1])
00 01 10 11
for ito n-1 do
A 11 12 13 14
for j
B 15 16 17 18
c[i,j]=0 C 369 392 195
i 01
for
j 012 0
C[i, j]+= C[i, j]+ A[i, k]*B[k, 012
j]
k 012 01
return c n 2
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive
0 Algorithms
11 12
0 369 392 0 15 16
1 195 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
B[O .. n- 1, O .. n- 1])
00 01 10 11
forC[1,0]+=C[1][0]+A[1][1]*B[1][0]
ito n-1 do
A 11 12 13 14
for j
C[1,0]+=195+14*17=433 B 15 16 17 18
c[i,j]=0 C 369 392 433
i 01
for
j 012 0
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 012 012 01
return c n 2
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive Algorithms
0 369 392 0 11 12 0 15 16
1 433 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
B[O .. n- 1, O .. n- 1])
for i 01
j 012 0
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 012 012 01 2
return c n 2
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive Algorithms
0 369 392 0 11 12 0 15 16
1 433 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
B 15 16 17 18
B[O .. n- 1, O .. n- 1])
C 369 392 433
for ito n-1 do i 01
for j j 012 01
k 012 012 012
c[i,j]=0
n 2
for
return c
C 0 1 A 0 1
B
Mathematical Analysis of Non-Recursive Algorithms 0 1
0 369 392 0 11 12
0 15 16
1 433 0 1 13 14
Ex1: MATRIX MULTIPLICATION 1 17 18
00 01 10 11
ALGORITHM MatrixMultiplication(A[O .. n- A
1, O 11
.. n-121], 13 14
B[O .. n- 1, O .. n- 1]) B 15 16 17 18
for n 2
return c
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive Algorithms
0 369 392 0 11 12 0 15 16
1 433 0 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
B[O .. n- 1, O .. n- 1])
for i 01
j 012 01
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 012 012 012 0
return c n 2
C 0 1
Mathematical Analysis of Non-Recursive
A 0 1
Algorithms
B 0 1
0 369 392
1 433 0 0 11 12 0 15 16
Ex1: MATRIX MULTIPLICATION
1 13 14 1 17 18
B[O .. n- 1, O .. n- 1])
forC[1,1]+=C[1][1]+A[1][0]*B[0][1]
ito n-1 do 00 01 10 11
A 11 12 13 14
for j
C[1,1]+=0+13*16=208 B 15 16 17 18
c[i,j]=0 C 369 392 433 208
for i 01
j 012 01
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 012 012 012 0
return c n 2
C 0
Mathematical 1Analysis of Non-Recursive Algorithms
A 0 1 B 0 1
0 369 392
0 11 12 0 15 16
Ex1:1MATRIX
433 208
MULTIPLICATION
1 13 14 1 17 18
B[O .. n- 1, O .. n- 1]) 00 01 10 11
for j 012 01
k 012 012 012 01
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
n 2
return c
C 0 1 B 0 1
Mathematical Analysis of ANon-Recursive
0 1 Algorithms
0 369 392 0 15 16
0 11 12
1 433 208 1 17 18
1 13 14
Ex1: MATRIX MULTIPLICATION
00 01 10 11
B[O .. n- 1, O .. n- 1]) B 15 16 17 18
C 369 392 433 460
forC[1,1]+=C[1][1]+A[1][1]*B[1][1]
ito n-1 do
i 01
for j
C[1,1]+=208+14*18=460 j 012 01
return c
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive Algorithms
0 369 392 0 11 12 0 15 16
1 433 208 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
00 01 10 11
A 11 12 13 14
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
B 15 16 17 18
B[O .. n- 1, O .. n- 1])
C 369 392 433 460
for ito n-1 do i 01
for j j 012 01
k 012 012 012 01 2
c[i,j]=0
n 2
for
return c
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive Algorithms
0 369 392 0 11 12 0 15 16
1 433 208 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
B[O .. n- 1, O .. n- 1])
for i 01
j 012 01 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 012 012 012 01 2
return c n 2
C 0 1 A 0 1 B 0 1
Mathematical Analysis of Non-Recursive Algorithms
0 369 392 0 11 12 0 15 16
1 433 208 1 13 14 1 17 18
Ex1: MATRIX MULTIPLICATION
B[O .. n- 1, O .. n- 1])
for i 01 2
j 012 01 2
C[i, j]+= C[i, j]+ A[i, k]*B[k, j]
k 012 012 012 01 2
return c n 2
00 01 10 11
Mathematical Analysis of Non-Recursive Algorithms
A 11 12 13 14
B 15 16 17 18
Ex1: MATRIX MULTIPLICATION
C 369 392 433 460
i 01 2
return c
Mathematical Analysis of Non-Recursive Algorithms
B[O .. n- 1, O .. n- 1])
for j ∑ 1
𝑘= 1
c[i,j]=0
for
return c
Mathematical Analysis of Non-Recursive Algorithms
𝑛 − 1 𝑛 −1
∑ ∑
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n- 1],
1
B[O .. n- 1, O .. n- 1]) 𝑗 =0 𝑘= 0
for j
c[i,j]=0
for
return c
Mathematical Analysis of Non-Recursive Algorithms
𝑛 −1 𝑛−1 𝑛 −1
𝑀 (𝑛)= ∑ ∑ ∑ 1
ALGORITHM MatrixMultiplication(A[O .. n- 1, O .. n-
1], 𝑖=0 𝑗=0 𝑘=0
B[O .. n- 1, O .. n- 1])
for j
c[i,j]=0
for
return c
Mathematical Analysis ofAsymptotic Notations
Non-Recursive and Basic Efficiency Classes
Algorithms
B[O .. n- 1, O .. n- 1])
for j
c[i,j]=0
for
return c
Mathematical Analysis of Non-Recursive Algorithms
B[O .. n- 1, O .. n- 1])
for j
c[i,j]=0
for
return c
Mathematical Analysis of Non-Recursive Algorithms
B[O .. n- 1, O .. n- 1])
for j
BASIC OPERATION HAS
c[i,j]=0
1 Addition + 1 Multiplication
for
B[O .. n- 1, O .. n- 1])
BASIC OPERATION HAS
for ito n-1 do
1 Addition + 1 Multiplication
for j
c[i,j]=0
for
B[O .. n- 1, O .. n- 1])
c[i,j]=0 then
for t(n)
C[i, j]+= C[i, j] + (A[i, k] * B[k, j])
=
return c
=(+)
Mathematical Analysis of Recursive Algorithms
Ex1:FACTORIAL
ALGORITHM F(n)
if n=0
return 1
else
return F(n-1)*n
Mathematical Analysis of Recursive Algorithms
Ex1:FACTORIAL
ALGORITHM F(n)
if n=0
return 1
else
return F(n-1)*n
Mathematical Analysis of Recursive Algorithms
Ex1:FACTORIAL
n 4
ALGORITHM F(n)
if n=0
return 1
else
return F(n-1)*n
Mathematical Analysis of Recursive Algorithms
Ex1:FACTORIAL
ALGORITHM F(n)
if n=0
return 1
else
return F(n-1)*n
Mathematical Analysis of Recursive Algorithms
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
if n=0 n 2 × F(1)
return 1 ADDRESS SPACE OF FIFTH INSTANCE
else n 1 × F(0)
return F(n-1)*nADDRESS SPACE OF SIXTH INSTANCE
n 0
Mathematical Analysis of Recursive Algorithms
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
Ex1:FACTORIAL
ALGORITHM F(n)
if n=0
return 1
else
return F(n-1)*n
1. Number of multiplication=M(n)=M(n-1)+1
Mathematical Analysis of Recursive Algorithms
Ex1:FACTORIAL
ALGORITHM F(n)
if n=0
return 1
else
return F(n-1)*n
M(n)=M(n-1)+1
=(M((n-1)-1)+1)+1 = M(n-2)+2
=(M((n-2)-1)+1)+2 = M(n-3)+3
...........................
= M(n-i)+i
...........................
= M(n-n)+n = n
Mathematical Analysis of Recursive Algorithms
Recursion
M(n)=M(n-1)+1+M(n-1)
=1 for n=1
Mathematical Analysis of Recursive Algorithms
Recursion
Recursion
So,
Mathematical Analysis of Recursive Algorithms
Recursion
Sorting
Searching
String Processing
Graph Problems
Combinatorial Problems
Geometric Problems
Neumerical Problems
Important Problem Types
Sorting
Register number
Why Sorting?
It makes searching easy.
Important Problem Types
Sorting
There is no algorithm whi can perform better for any order list.
Stable:
Before d e i h j
a[i]=a[j]
Sorting where i<j
After d e i h j
e<j
Sorting
Important Problem Types
Searching
DELETION
Important Problem Types
String Operation
Non-numerical data.
symbols etc.)
Permutation
Combination
Subset
Considered
Very Difficult
Integrals
Functions.
Important Problem Types
Neumerical Problems
In the last 25 years or so, the computing industry has shifted