AlgorithmAnalysis -1
AlgorithmAnalysis -1
Analysis-I
Dr.Kumkum Saxena
Order Analysis
◼ Judging the Efficiency/Speed of an Algorithm
◼ Thus far, we’ve looked at a few different
algorithms:
◼ Max # of 1’s
◼ Linear Search vs Binary Search
◼ Sorted List Matching Problem
◼ and others
◼ But we haven’t really examined them, in detail,
regarding their efficiency or speed
Kumkum Saxena
Big-O Notation
◼ What is Big O?
◼ Big O comes from Big-O Notation
◼ In C.S., we want to know how efficient an algorithm
is…how “fast” it is
◼ More specifically…we want to know how the
performance of an algorithm responds to changes
in problem size
◼ The goal is to provide a qualitative insight on the # of
operations for a problem size of n elements.
◼ And this total # of operations can be described with a
mathematical expression in terms of n.
▪ This expression is known as Big-O
int func1(int n) {
int i, j, x = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
x++;
}
}
return x;
}
int func3(int n) {
int i, x = 0;
for (i = 1; i <= n; i++)
x++;
for (i = 1; i<=n; i++)
x++;
return x;
}
int func4(int n) {
while (n > 0) {
printf(“%d”, n%2);
n = n/2;
}
}
Kumkum Saxena
And More Algorithm Analysis
◼ Examples of Analyzing Code:
◼ Last time we went over examples of analyzing
code
◼ We did this in a somewhat naïve manner
▪ Just analyzed the code and tried to “trace” what was going on
◼ This Lecture:
◼ We will do this in a more structured fashion
◼ We mentioned that summations are a tool for you to help
coming up with a running time of iterative algorithms
◼ Today we will look at some of those same code
fragments, as well as others, and show you how to use
summations to find the Big-O running time
Dr. Kumkum Saxena Algorithm Analysis -1 page 72
More Algorithm Analysis
◼ Example 1:
◼ Determine the Big O running time of the following
code fragment:
◼ We have two for loops
◼ They are NOT nested
▪ The first runs from k = 1 up to (and including) n/2
▪ The second runs from j = 1 up to (and including) n2
1 + 1
k =1 j =1
The constant value, 1, inside each summation refers
to the one, and only, operation in each for loop.
1 + 1
k =1 j =1
You use the formula: k = k *n
i =1
n/2 n2
n
k =1
1 +
j =1
1 =
2
+ n 2
1
n
i =1 j =1
You use the formula: k = k *n
i =1
n n n
i =1
1 =
j =1
n = n
i =1
2 All we did is apply the
above formula twice.
int func3(int n) {
sum = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n * n; j++) {
sum++;
}
}
}
Dr. Kumkum Saxena Algorithm Analysis -1 page 79
More Algorithm Analysis
◼ Example 3:
◼ Determine the Big O running time of the following
code fragment:
◼ Here we again have two for loops
◼ And they are nested. So is this O(n2)?
▪ The outer loop runs from i = 0 up to (and not including) n
▪ The inner loop runs from j = 0 up to (and not including) n2
◼ The sole (only) operation is a “sum++” within the inner
loop
1
i =0 j =0
You use the formula: k = k *n
i =1
n −1 n 2 −1 n −1 n −1
i =0
1 =
j =0
n 2
= n
i =0
2
1 = n 3
i =0
All we did is apply the
above formula twice.
int func3(int n) {
bigNumber = 0;
for (i = 100; i <= 2n; i++) {
for (j = 1; j < n * n; j++) {
bigNumber += i*n + j*n;
}
}
}
Dr. Kumkum Saxena Algorithm Analysis -1 page 82
More Algorithm Analysis
◼ Example 4:
◼ Write a summation that describes the number of
multiplication operations in this code fragment:
◼ Here we again have two for loops
◼ Pay attention to the limits (bounds) of the for loop
▪ The outer loop runs from i = 100 up to (and including) 2n
▪ The inner loop runs from j = 1 up to (and not including) n2
◼ Now examine the number of multiplications
▪ Because this problem specifically said to “describe the
number of multiplication operations, we do not care about
ANY of the other operations
▪ bigNumber += i*n + j*n;
▪ There are TWO multiplication operations in this statement
Dr. Kumkum Saxena Algorithm Analysis -1 page 83
More Algorithm Analysis
◼ Example 4:
◼ Write a summation that describes the number of
multiplication operations in this code fragment:
◼ We express the number of multiplications in the form of
a summation and then we solve that summation:
2n n 2 −1
2
i =100 j =1
2n n 2 −1 2n 2n
i =100
2 =
j =1
2( n
i =100
2
− 1) = 2( n 2
− 1) 1 = 2( n
i =100
2
− 1)(2n − 99)