0% found this document useful (0 votes)
99 views

11 - Big O and Recursion

The time complexity of the above recursive factorial function is O(n).

Uploaded by

Kim Sia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

11 - Big O and Recursion

The time complexity of the above recursive factorial function is O(n).

Uploaded by

Kim Sia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Time Complexity

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(1) < O(log n) < O(n) < O(n log n)


< O(n2) < O(n3) < O(2n)
Big-O
O(1) < O(log n) < O(n) < O(n log n)
< O(n2) < O(n3) < O(2n)
Big-O
Exercise:
O(g(n))
n logn n nlogn n2 n3 2n
1
2
4
8
16
32
64
Big-O
O(1) - constant
Most instructions are executed once or
at most only a few times
O(log n) - logarithmic
• Program slightly slower as n grows
• For programs that solve a big problem
by transforming it into a small
problem, cutting the size by some
constant factor
O(n) – linear
Proportional to the size of n
Big-O
O(n log n)
For algorithms that solve a problem by
breaking it up into smaller subproblems,
solve them independently and then
combining the solution

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)

Which is faster and more efficient?


Big-O - Exercise
Solution:
n A1 A2
1 10 0.5
5 50 12.5
10 100 50
15 150 112.5
20 200 200
30 300 450
Big-O - Exercise
Arrange the following in increasing
order of complexity:

a. n2, 10n
b. 4n, 4n3
c. n2, n1/3, n, nlog2n
Recursive Algorithms

Recurrence Relations and


Time Complexity
Recursive Algorithm
 Derive a recurrence relation to describe
the frequency count or operation count
of the algorithm
 Solves a problem by reducing it to an
instance of the same problem with
smaller input
Recurrence Relation - Example
T(n) =
{
5

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

Recursive Algorithm - Exercise


int isMember(int A[], int nSize, int nKey)
{ if (nSize == 0)
return 0;
else if (A[nSize-1] == nKey)
return 1;
else
return isMember(A, nSize-1, nKey);
}
Extra
Problem

Recursive Algorithm - Exercise


int factorial(int n)
{ if (n == 0 || n == 1)
return 1;
else
return (n * factorial(n-1));
}

You might also like