03-303 F22 Algorithm Analysis
03-303 F22 Algorithm Analysis
CMPS 303
Dr. Osama Halabi
Algorithm Analysis
Data Structures
Empirical Analysis
Empirical Analysis
Data Structures 5
Empirical Analysis to Compare Two Algorithm
✤ used System.currentTimeMillis() to measure the efficiency
of both repeat1 and repeat2
Data Structures 7
Algorithm Efficiency
✤ The efficiency of an algorithm is usually expressed in terms
of its use of CPU time
✤ The analysis of algorithms involves categorizing an algorithm
in terms of efficiency
✤ An everyday example: washing dishes
✤ Suppose washing a dish by hand takes 30 seconds and drying
a dish takes an additional 30 seconds
✤ Therefore, n dishes require n minutes to wash and dry
Data Structures 8
Algorithm Efficiency
✤ Suppose that we were careless while washing the dishes and splashed too
much water
✤ Suppose that each time we washed a dish, we had to dry not only that dish
but also all of the dishes we had washed before that one
✤ It would still take 30s to wash each dish, but now it would take 30s to dry the
last dish (once), 2*30 to dry the second-to-last dish (twice)…
Data Structures 9
Algorithm Efficiency
✤ The computation could be expressed as follows:
Data Structures 14
Theoretical Analysis
Theoretical Analysis
Data Structures 16
Counting Primitive Operations
✤ Basic computations performed
by an algorithm
Examples:
✤ Identifiable in pseudocode
❑
■ Evaluating an expression
Data Structures 17
Counting Primitive Operations
public static int total(int a[])
{
int s=0 1 operation
for (int i=0;i<a.length;i++) 1 operation
s=s+a[i]; n+1 operations
return m; n operations
} 3n operations
1 operation
The total time is 1+1+n+1+n+3n+1 = 5n+4
Characterizes running time as a function of the input size, f(n)=5n+4
Data Structures
Estimating Running Time
2 ops
2 ops
2n ops
2n ops
0 to n ops
1 ops
int sum=0;
for (int j=1;j<=a.length;i++)
sum+=a[j];
return sum;
}
• Total: 1+3n/2+n/2
=2+4n/2
=2+2n
Data Structures 22
Exercise
• Calculate the execution time of each of the following code
for (int i=1;i<=n,i=i*2) How many times this
loop will be repeated
cout<<“welcome”;
i will change as follows
1 2 4 8 16 32 64 128 …. n
A1=10n
A2=2n2
if runtime
time for n + 1 time for 2 n time for 4 n
is...
c lg n c lg (n + 1) c (lg n + 1) c(lg n + 2)
cn c (n + 1) 2c n 4c n
~ c n lg n 2c n lg n + 4c n lg n + runtime
c n lg n quadruples
+ cn 2cn 4cn
when
c n2 ~ c n2 + 2c n 4c n2 16c n2 problem
size doubles
c n3 ~ c n3 + 3c n2 8c n3 64c n3
c 2n c 2 n+1 c 2 2n c 2 4n
Data Structures 27
Comparison of Two Algorithms
insertion sort is Slide by Matt Stallmann
n2 / 4 included with permission.
merge sort is
2 n lg n
sort a million items?
insertion sort takes
roughly 70 hours
while
merge sort takes
roughly 40 seconds
This is a slow machine, but if
100 x as fast then it’s 40 minutes
versus less than 0.5 seconds
Data Structures 28
Big-Oh Notation
✤ The Big O notation is an asymptotic notation to describe time
complexity which means amount of time to run an algorithm.
✦ Pick c = 3 and n0 = 10 1
1 10 100 1,000
n
Data Structures 30
Big-Oh Example
✤Example: the function 1,000,000
n^2
n2 is not O(n) 100,000 100n
✦n2 ≤ cn 10,000
10n
n
✦n ≤ c 1,000
✦The above inequality 100
cannot be satisfied
10
since c must be a
1
constant 1 10
n 100 1,000
Data Structures 31
More Big-Oh Examples
❑ 7n - 2
7n-2 is O(n)
need c > 0 and n0 ≥ 1 such that 7 n - 2 ≤ c n for n ≥ n0
this is true for c = 7 and n0 = 1
❑ 3 n3 + 20 n2 + 5
3 n3 + 20 n2 + 5 is O(n3)
need c > 0 and n0 ≥ 1 such that 3 n3 + 20 n2 + 5 ≤ c n3 for n ≥ n0
this is true for c = 28 and n0 = 1
❑ 3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0 ≥ 1 such that 3 log n + 5 ≤ c log n for n ≥ n0
this is true for c = 8 and n0 = 2 (Note that log n is zero for n = 1)
Data Structures 32
Big-Oh and Growth Rate
✤ The big-Oh notation gives an upper bound on the growth
rate of a function
✤ We can use the big-Oh notation to rank functions according
to their growth rate
✤ The statement “f(n) is O(g(n))” means that the growth
rate of f(n) is no more than the growth rate of g(n)
f(n) is O(g(n)) g(n) is O(f(n))
g(n) grows more Yes No
f(n) grows more No Yes
Same growth Yes Yes
Data Structures 33
Big-Oh Rules
✤ If is f(n) a polynomial of degree d, then f(n) is O(nd), i.e.,
1. Drop lower-order terms
2. Drop constant factors
✤ Use the smallest possible class (the closest or tightest) of
functions
✦ Say “2n is O(n)” instead of “2n is O(n2)”
✤ Use the simplest expression of the class
✦ Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”
Data Structures 34
Common Asymptotic Functions
constant Ο(1)
logarithmic Ο(log n)
linear Ο(n)
quadratic Ο(n2)
cubic Ο(n3)
exponential Ο(2n)
Data Structures
Growth Functions
1- Constant O(1)
• Growth is independent of the size of input data, n.
• e.g., accessing a specific array element using its index.
int firstElement(int a[])
{
return a[0];
}
2- Logarithmic O(log n)
• Growth increases slowly comparing to n.
• e.g., loop that increases doubly, also binary search
for (int i=1;i<=n,i=i*2)
cout<<“welcome”;
Data Structures
Growth Functions
3- Linear O(n)
• Growth is directly proportional to n.
• e.g., searching for an element in an array (linear search)
Data Structures
Growth Functions
4- O(n log n)
• e.g., sorting using divide and conquer
5- Quadratic O(n 2) .
• e.g., typical in two nested loops
Data Structures
Growth Functions
6- Cubic O( n 3) .
• e.g., typical in three nested loops
7- Exponential O(2 n) .
• e.g., it growths extremely and possibly impractical
Data Structures
Exercises
END OF
End of
Algorithm Analysis