03 mathematical anaylsis
TRANSCRIPT
Mathematical Analysis of Non recursive Algorithms
• Identify input size
• Identify Basic Operation
• Identify Case Complexity i.e. (Every Case, Best Case, Worst Case, Average Case)
• Setup summation/recurrence relation
• solve summation/recurrence relation or atleast order of growth of its relation
Finding largest element in array
Algorithm max_element(A,n)
maxval=A[0]
for i=1 to n-1
if A[i]>maxval
maxval=A[i]
Return maxval
• Input size : number of elements in array
• Basic operation : comparison
• Case complexity : Every Case
• Number of times basic operation executed are
• T(n)=∑ 1=n-1є Ө (n)i=1
n-1
Element uniqueness problem
Algorithm element_unique(A,n)
for i=0 to n-2
for j=i+1 to n-1
if A[i]==A[j]
return false
return true
• Input size : number of elements in array
• Basic operation : comparison of element of Array
• Case complexity : Worst Case
• Number of times basic operation executed are
T(n)=∑ ∑ 1 = ∑ [(n-1)-(i+1)+1]
=n(n-1)/2
i=0
n-2
j=i+1
n-1
i=0
n-2
Matrix Multiplication
Algorithm matrix_mul(A,B,C)
for i=1 to n
for j=1 to n
C[i,j]=0
for k=1 to n
C[i,j]=C[i,j]+A[i,k ]*B[k,j]
return C
• Input Size : order of matrix
• Basic operation:multiplication
• Case complexity: every case
• T(n)=∑ ∑ ∑ 1= ∑ ∑ n = ∑ n2 = n3 n
i=1 j=1 k=1
n n
i=1 i=1
nn n
i=1
Mathematical Analysis Of Recursive Algorithms
• Identify input size
• Identify Basic Operation
• Identify Case Complexity i.e. (Every Case, Best Case, Worst Case, Average Case)
• Setup recurrence relation
• solve recurrence relation or atleast order of growth of its relation
• Input size: 4 bytes
• Basic Operation: multiplication
• Case complexity: Every case
• Recurrence Relation: An equation or inequality that describes function in terms of its smaller inputs.
• to determine solution we need initial condition that makes recursion stop.
• F(n)=F(n-1) *n for n>0 (Basic Operation)M(n)=M(n-1) +1 (to multiply F(n-1) by n)
compute(F(n-1))
M(n-1)=(M(n-2)+1)
M(n-2)=(M(n-3)+1)
by substituting values backward
M(n-1)=M(n-3)+1+1
M(n)=M(n-3)+1+1+1
• M(n)=M(n-i)+i
we know the value of initial condition
M(0)=0
by taking i=n
M(n)=M(n-n)+n
=0+n=nєӨ(n)
Algorithm hanoi(n,source,auxiliary,destination)
if n=1 move disk from source to destination
else
hanoi(n-1,source,destination,auxiliary)
move disk from source to destination
hanoi(n-1,auxiliary,source,destination)
• input size:number of diks, n
• Basic operation:move
• Case Complexity: Every caseso recurrence relation would be
• M(n)=M(n-1)+1+M(n-1)
=2M(n-1)+1
or M(n-1)=2M(n-2)+1
M(n-2)=2M(n-3)+1
Initial Condition: M(1)=1
by backward substituting valuesM(n)=2M(n-1)+1
=2(2(M(n-2)+1)+1
=22M(n-2)+2+1
=22(2M(n-3)+1)+2+1
=23M(n-3)+22+2+1
By generalizing
=2iM(n-i)+2i-1+2i-2+…+1
M(n)=2iM(n-i)+2i-1+2i-2+…+1 geometric series so
=2n-1M(n-(n-1))+2n-1 -1
=2n-1M(1)+2n-1 -1
=2n-1+2n-1 -1
=2n -1
• Recurrence Tree
Calculating number of bits to store decimal number
Algorithm bincov(n)
if n=1 return 1
else
return bincov (floor(n/2))+1
• Input size: 4 bytes
• Basic operation: Summation
• complexity:every case
Recurrence Relation:
A(n)=A(floor(n/2))+1
initial condition:A(1)=0
• Smoothness rule: for solving problems of ceiling or flooring assume that n= 2k ;
which claims under broad assumption order of growth observed for n=2k correct results for all values of n.
A(n)=A( n/2)+1 A(n/2)=A(n/4)+1
A(n/4)=A(n/8)+1
by backward substitution
A(n)=A(n/4)+2=A(n/8)+3=A(n/ 2k )+k
by taking 2k =n
A(n)=A(1)+k=k=lg(n)єӨ(lgn)