Mathematical
Analysis of
Algorithms
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
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
Finding Factorial
Algorithm fact(n)
if n==0 return 1
else
return fact(n-1)*n
• 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)
Tower of hanoi
• Problem:
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 case
so 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 values
M(n)=2M(n-1)+1
=2(2(M(n-2)+1)+1
=22
M(n-2)+2+1
=22
(2M(n-3)+1)+2+1
=23
M(n-3)+22
+2+1
By generalizing
=2i
M(n-i)+2i-1
+2i-2
+…+1
M(n)=2i
M(n-i)+2i-1
+2i-2
+…+1 geometric series
so
=2n-1
M(n-(n-1))+2n-1
-1
=2n-1
M(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)

03 mathematical anaylsis

  • 1.
  • 2.
    Mathematical Analysis ofNon 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
  • 3.
    Finding largest elementin array Algorithm max_element(A,n) maxval=A[0] for i=1 to n-1 if A[i]>maxval maxval=A[i] Return maxval
  • 4.
    • 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
  • 5.
    Element uniqueness problem Algorithmelement_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
  • 6.
    • 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
  • 7.
    Matrix Multiplication Algorithm matrix_mul(A,B,C) fori=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
  • 8.
    • 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
  • 9.
  • 10.
    Mathematical Analysis Of RecursiveAlgorithms • 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
  • 11.
    Finding Factorial Algorithm fact(n) ifn==0 return 1 else return fact(n-1)*n
  • 12.
    • 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.
  • 13.
    • F(n)=F(n-1) *nfor 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
  • 14.
    • M(n)=M(n-i)+i we knowthe value of initial condition M(0)=0 by taking i=n M(n)=M(n-n)+n =0+n=nєӨ(n)
  • 15.
  • 16.
    Algorithm hanoi(n,source,auxiliary,destination) if n=1move disk from source to destination else hanoi(n-1,source,destination,auxiliary) move disk from source to destination hanoi(n-1,auxiliary,source,destination)
  • 17.
    • input size:numberof diks, n • Basic operation:move • Case Complexity: Every case so 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
  • 18.
    Initial Condition: M(1)=1 bybackward substituting values M(n)=2M(n-1)+1 =2(2(M(n-2)+1)+1 =22 M(n-2)+2+1 =22 (2M(n-3)+1)+2+1 =23 M(n-3)+22 +2+1 By generalizing =2i M(n-i)+2i-1 +2i-2 +…+1
  • 19.
  • 20.
    Calculating number ofbits to store decimal number Algorithm bincov(n) if n=1 return 1 else return bincov (floor(n/2))+1
  • 21.
    • Input size:4 bytes • Basic operation: Summation • complexity:every case Recurrence Relation: A(n)=A(floor(n/2))+1 initial condition:A(1)=0
  • 22.
    • 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)

Editor's Notes

  • #5 Summation(i=1 to n) 1 =1(n-1)+1 = summation number(upperlimeit-lower limit+1).
  • #9 Summation(k=1 to n)1=n;(1(n-1)+1)=n-1+1=n Sumation of (k=1 to n)n=n2 ;n(n-1)+n=n2-n+n=n2
  • #19 sumation i=1 to n x^i=(x^(n+1)-1)/(x-1)
  • #23 n=2^k lgn=klg(2) as lg(2)=1 so k= lgn