Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Algorithms and Problem Solving (15B17CI411)

EVEN 2021

Module 1: Lecture 3

Jaypee Institute of Information Technology (JIIT)


A-10, Sector 62, Noida
Recurrences and Running Time
• An equation or inequality that describes a function in terms of its value
on smaller inputs.
T(n) = T(n-1) + n
• Recurrences arise when an algorithm contains recursive calls to itself

• What is the actual running time of the algorithm?

• Need to solve the recurrence

2
Function1(n) Function2(n) Function3(n)
{ ... { ... { ...
for(i=n/2;i<=n;i++) for(i=1;i<=n;i++) Function1(n);
for(j=1; j<=n;j+i) for(i=1;i<=n;i++)
for(j=1; j<=n/2;j++)
print(“ok”); ... Function2(n);
for(k=1;k<=n;k*2)
} }
print(“ok”);
}

T(n) = O(n2log2n) T(n) = O(nlog2n) T(n) = O(n2log2n)


Function1(n) Function2(A, n) Function3(A,l,r)
{ ... { ... { ...
n--; j = FindMax(A); \\O(n) m=(l+r)/2;
Remove(A, j); \\O(n) Function3(A,l,m);
Function1(n);
l = length(A); \\O(n) Function3(A,m+1,r);
} Function2(A,l);
}
...
}

T(n) = c + T(n-1) T(n) = n + T(n-1) T(n) = n + 2T(n/2)


Example Recurrences

5
Analysis of BINARY-SEARCH
BINARY-SEARCH (A, first, last, x)
if (first > last)
constant time: c1
return FALSE
mid ← ⎣(first+last)/2⎦ constant time: c2
if x = A[mid]
constant time: c3
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, first, mid-1, x) same problem of size n/2

if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, last, x) same problem of size n/2

T(n) = c + T(n/2)

6
How to solve Recurrence Relations
• Substitution method
• Iteration method
• Recursion Tree method
• Master method
Substitution Method
1. Guess the form of the solution.
2. Use mathematical induction to find the constants and show that
the solution works.
Substitution method
❖ Guess a solution
⮚ T(n) = O(g(n))
⮚ Induction goal: apply the definition of the asymptotic notation
⮚ T(n) ≤ c g(n), for some c > 0 and n ≥ n0

⮚ Induction hypothesis: T(k) ≤ c g(k) for all k < n


❖ Prove the induction goal
⮚ Use the induction hypothesis to find some values of the constants c and n0
for which the induction goal holds

Binary Search
The Iteration Method
Steps followed to solve any recurrence using iterating methods are:
• Expend the recurrence
• Express the expansion as a summation by plugging the recurrence back into
itself until you see a pattern.
• Evaluate the summation by using the arithmetic or geometric summation
formulae

14
Recursive Linear Search
LinearSrch(A, n, x) { T(n) = c + T(n-1)
int loc;
n--; = c + c + T(n-2)
if (n >= 0) { = c + c + c + T(n-3)
if (A[n] == x)
return n; ...
else
= kc + T(n-k)
loc = LinearSrch(A, n, x);
}
Say k=n  T(n) = nc + T(0)
else
return -1; = O(n)
return loc;
}
T(n/2) = c + T(n/4)
T(n/4) = c + T(n/8)

k times

17

18

You might also like