Lecture 2-The Big-Oh Notation
Lecture 2-The Big-Oh Notation
The complexity of an algorithm is simply the amount of work the algorithm performs to complete its task.
Analysis of Algorithms
Overall goal is have an understanding of the complexity of algorithms
Estimate the running time Estimate the memory space required
Depends on the input size
Frequency Count and Time Complexity (Big-Oh) Big-Oh Notation To simplify the running time estimation, for a functionf(n), we ignore the constants and lower order terms. Example: 10n3+4n2-4n+5 is O(n3). 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)
Constant; most instructions are executed once or at most only a few times. Describes an algorithm that will always execute in the same time (or space) regardless of the size of the input.
O(log n)
Program slightly slower as N grows; normally in programs that solve a big problem by transforming it into a small problem, cutting the size by some constant factor.
O(n):Linear
Describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input.
O(n log n)
Occurs in algorithms that solve a problem by breaking it up into smaller sub-problems, solve them independently and then combining the solution.
O (n2):quadratic
Represents an algorithm whose performance is directly proportional to the square of the size of the input. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(n3), O(n4), etc
O (2n): exponential
Denotes an algorithm whose growth will double with each additional element in the input.
11
vs.
Algorithm 2
T2(n) is o(n2)
The Big-Oh Notation cont... Then Algorithm 1 is usually considered better than algorithm 2 However that for small values of n, Algorithm 2 might well outperform Algorithm 1
Eg. T1(n) is 10n T2(n) is 0.1n2 10n>0.1n2 for values n up to 100 T1(n) < T2(n) only for n >100
The core of the algorithm analysis: to find out how the number of the basic operations depends on the size of the input
Example #1
Below is an algorithm SIMPLE in form of pseudo code SIMPLE 1 for x 4 to 1 2 do x * 2 3 x x- 1 4 print x
For analysis we will execute the code and calculate how many times each line of our algorithm is executed.
UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A
3
4
1,1,1
1
3
1
UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A
First hand Analysis So we have total of 12 executions If one operation takes 10 nanoseconds to execute then total execution time for our SIMPLE algorithm will be 10 * 12= 120 nanoseconds.
Example #2
Algorithm to Calculate Mean Accepts: An integer n and an array X[1],..,X[n] of real numbers Function: Finds the mean of n real nos. Returns: The of X[1],...X[n] 1. Initialize sum to 0 2. Initialize index variable i to 0 3. While i<n do the following a. Increment i by 1 b. Add X[i] to sum 4. Calculate and return Mean = sum/n
Line # 1 2 3
4
5
n
n
6
Total
1
3n + 4
Trace: n = 2 Sum = 0 i=0 0 < 2? Yes i=1 Sum = sum + x[1] 1 < 2? Yes i=2 Sum = sum + x[2] 2< 2? No Mean = sum/2 10 execution
TFC = 3n + 4 O(n)
Rule 1: for loops The running time for a loop is at most the running time of the statements inside the loop times the number of iterations
Rule 2: Nested loops The total running time is the running time of the inside statements times the product of the sizes of all the loops
Rule 3: Consecutive program fragments The total running time is the maximum of the running time of the individual fragments
Additional Rules:
1. Nested loops are multiplied together. 2. Sequential loops are added. 3. Only the largest term is kept, all others are dropped. 4. Constants are dropped.
Determine the Total Frequency Count and Time Complexity of the following code segments written in C Language.
Frequency Count
for (y=1; y<=x; y++) 1+x+1+x a += 1; x Total Frequency Count = 3x + 2 Time Complexity is O(x)
Exercises Determine the Total Frequency Count and Time Complexity of the following code segments written in C Language. Assume that the codes are syntactically correct.
1. for (m=1; m<=b; m++) for (k=0; k<=10; k++) printf ("%d", k);
2. for (x=1; x<=y; x++) { for (j=y; j>=1; j--) printf ("%d%d", m,j); for (k=1; k<=y; k++) printf ("%d", k); }