11 - Big O and Recursion
11 - Big O and Recursion
Big-Oh Notation
Time Complexity
• Order of magnitude of the frequency
count or the operation count
• The time taken by the algorithm
grows with the size of the input
Input Size: the number of items in the
input
Running time: the number of primitive
operations or steps executed, independent
of the actual machines
Running Time
Best case
Average case
Worst case or Big-O
an upper bound on the running time for
any input
the execution of the algorithm will not take
any longer than this
Big-O
• Worst case time complexity
• O(g(n))
• n is the input size
• g(n) is the number of steps to execute the
algorithm (frequency and operation counts)
Ex. n3+7n-5 = O(n3)
2n2+4n = O(n2)
105 = O(1)
4n-2000 = O(n)
Big-O
Common computing times:
* n is the input parameter
O(n2) - quadratic
For algorithms that process all pairs of
data items
O(n3) – cubic
For algorithms that process triples of data
items
Big-O
O(2n) - exponential
• Brute-force solution
• Change in speed of computer or
change in constant will give no big
improvement
• To improve, change order of
magnitude by designing a new
algorithm
Big-O - Exercise
Given 2 algorithms A1 and A2
performing the same task on n inputs:
A1 A2
10n n2/2
O(n) O(n2)
a. n2, 10n
b. 4n, 4n3
c. n2, n1/3, n, nlog2n
Recursive Algorithms
T(n-1) + 6
n=0
n>0
Lecture
T(n) =
{ 10
T(n/2) + 5
n=1
n>1
Assign
No. 2
Recurrence Relation - Example
T(n) =
{
10 n=0
2T(n-1) + 5 n > 0
Extra
Problem
T(n) =
{ 15 n=1
2T(n/2) + 4 n > 1
Extra
Problem
Recursive Algorithm Analysis
int sumArrayElem(int A[ ], int n)
{ if (n == 0)
return 0;
else
return sumArrayElem(A, n-1) + A[n-1];
}
T(n) =
{ 2
T(n-1) + 5
n=0
n>0
Recursive Algorithm Analysis
int binarySearch(int A[ ], int nLo, int nHi, int nKey)
{ int nMid = (nLo + nHi) / 2;
{
if (nLo > nHi)
return 0; 6 n=1
else if (A[nMid] == nKey) T(n) =
return 1; T(n/2) + 8 n>1
else if (A[nMid] > nKey)
return binarySearch(A, nLo, nMid-1, nKey);
else
return binarySearch(A, nMid+1, nHi, nKey);
}
Assign
No. 2