L1 AlgorithmAnalysis
L1 AlgorithmAnalysis
Initially prepared by Dr. İlyas Çiçekli; improved by various Bilkent CS202 instructors.
• We will focus on
– How to estimate the time required for an algorithm
– How to reduce the time required
• To analyze algorithms:
– First, we start counting the number of significant operations in a
particular solution to assess its efficiency.
– Then, we will express the efficiency of algorithms using growth
functions.
– Complex Operations (matrix addition, array resizing) are not single step
Total cost = 1 + 1
🡺 The time required for this algorithm is
constant
Don’t forget: We assume that each simple operation takes one unit of time
}
else
absval = n; 1
Total cost = 1 + 1 + (n + 1) + n + n
🡺 The time required for this algorithm is proportional to n
Total cost = 1 + 1 + (n + 1) + n + n * (n + 1) + n * n + n * n + n
🡺 The time required for this algorithm is proportional to n2
CS202 - Fundamentals of Computer Science II 8
Algorithm Growth Rates
• We measure the time requirement of an algorithm as a function of the
problem size.
• The most important thing is to learn how quickly the time requirement
of an algorithm grows as a function of the problem size.
• An algorithm’s proportional time requirement is known as growth rate.
• We can compare the efficiency of
two algorithms by comparing
their growth rates.
CS 202 12
Big-O Notation
c*f(n) c1*f(n)
T(n) T(n)
T(n) c*f(n) c2*f(n)
Choose c = 2 and n0 = 1
🡺 2n2 ≤ 2n3 for all n ≥ 1
CS 202 14
Example
Choose c = 3 and n0 = 1
CS 202 15
Example
• Show that f(n) = n2 - 3n + 10 is order of O(n2)
– Show that there exist constants c and n0 that satisfy the condition
Try c = 3 and n0 = 2
CS 202 17
A Comparison of Growth-Rate Functions
• Linear time (n) and n log n algorithms remain practical even for one
billion items
• Best-case performance
– This is useless! Why?
• Average-case performance
– It is valid if you can figure out what the “average” input is
– It is computed considering all possible inputs and their distribution
– It is usually difficult to compute
Worst-case:
– If the item is in the last location of the array; or
– If it is not found in the array
Best-case:
– If the item is in the first location of the array
Average-case:
– How can we compute it? (assume each element is unique)
CS202 - Fundamentals of Computer Science II 26
How to find the growth-rate of C++ codes?
29
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d - %d\n", i, j);
}
}
30
for (int i = 0; i < n; i++)
{
printf("%d\n", i);
}
31
printf("Hello\n");
32
for (int i = 0; i < n; i++)
{
printf("%d\n", i);
}
33
for (int i = 0; i < n; i++)
{
printf("%d\n", i); int foo(int i, int j, int n)
} {
for (int k=0; k<n; k++)
for (int i = 0; i < n; i++) {
{ printf(“%f\n”, (i+j)/n);
for (int j = 0; j < n; j++) }
{ }
printf("%d\n", foo(i, j, n));
}
}
34
bool arrayContainsElement(int arr[], int n, int element)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == element) return true;
}
return false;
}
35
What about recursive functions?
...
...
return n *factorial(n-1);
}
40
Binary Search
int binarySearch( int a[], int left, int right, int x) {
int mid; // mid will be the index of
// target when it’s found.
if (right >= 1){
mid = left + (right-left)/2;
if (a[mid] == x)
return mid;
if (a[mid] > x)
return binarySearch(a, left, mid-1, x);
return binarySearch(a, mid+1, right, x);
}
return -1;
}