2. 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
3. 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
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
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
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
10. 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
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) *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
14. • 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)
17. • 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
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
Summation(i=1 to n) 1
=1(n-1)+1
= summation number(upperlimeit-lower limit+1).
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