Algorithmic time complexity is concerned about how fast or slow particular algorithm performs. We define complexity as a numerical function T(n)- time versus the input size n. A given algorithm will take different amounts of time on the same inputs depending on such factors as: processor speed, instruction set, disk speed, brand of compiler and etc.
The way around is to estimate efficiency of each algorithm asymptotically. We will measure time T(n) as the number of elementary " step" ( define in any way), provided each such step takes constant time.
Asymptotic Notations
The goal of computational complexity is to classify algorithms according to their performances.
We will represent the time function T(n) using the " big-O" notation to express an algorithm runtime complexity. For example, the statement T(n)=O(n^2) says that algorithm has a quadratic time complexity.
Definition of " big Oh"
Big O specifically describes the worst-case scenario, and can be used to describe the execution time required of the space used by an algorithm.
Examples:
- 1=O(n)
- n=O(n^2)
- log(n)=O(n)
- 2n+1=O(n)
Constant time : O(1)
An algorithm is said to run in constant time if it requires them same amount of time regardless of the input size.
boolean isFirstElementNull(List<string> elements){
return elements[0]==null;
}
Examples:
- array: accessing any element
- fixed-size stack: push and pop methods
- fixed-size queue: enqueue and dequeue methods
An algorithm is said to run in linear time if its time execution is directly proportional to the input size, time grows linearly as input size increases.
In the following example, a matching string could be found during any iteration of the loop and function would return early, but Big O notation will always assume the upper limit where the algorithm will perform the maximum number of iterations.
boolean containsValue(List<Sring> elements, string value)
{
for ( string element:elements){
if (element==value) return true;
}
return false;
}
Examples:
- array: linear search, traversing, find minimum
- ArrayList: contains method
- queue: contains method
An algorithm is said to run in logarithmic time if its time execution is proportional to the logarithm of the input size.
int binarySearch( int arr[], int l, int r, int x) {
if ( r>=1){
int mid = 1 + ( r - 1 )/2;
if ( arr[mid] == x)
return mid;
if ( arr[mid] > x)
return binarySearch( arr, l , mid - 1 , x) ;
return binarySearch( arr, mid + 1 , r , x ) ;
}
return -1;
}
Example:
- binary search
An algorithm is said to run in logarithmic time if its time execution is proportional to the square of the input size.
This is common with algorithms that involve nested iterations over the data set. Deeper nested iteration will result in O(N3), O(N4) etc.
boolean containsDuplicates(List<String> elements) {
for ( int outer = 0; outer < elements.size(); outer++){
for ( int outer = 0; outer < elements.size(); inner++){
if ( outer == linner ) continue;
if ( elements [outer] == elements[inner]) return true;
}
}
return false;
}
Examples:
- bubble sort,selection sort, insertion sort.
O(2^n) denotes an algorithm whose growth doubles with each addition to the input data set. The growth curve of an O(2^N) function is exponential-starting off very shallow, then rising meteorically.An example of an O(2^N) function is the recursive calculation of Fibonacci numbers:
int fibonacc(int number){
if ( number <= 1) return number;
return fibonacci(number-2)+fibonacci(number-1);
}
Definition of "big Omega"
We need the notation for lower bound i.e the best-case scenario.A capital omega Ω notation is used in this case.Example:
- n=Ω(1)
- n^2=Ω(n)
- n^2=Ω(n log(n) )
- 2 n+1=O(n)
This is used to measure average-case complexity of a particular algorithm,i.e to find the upper and lower bounds.Examples
- 2n=Θ(n)
- n^2+2n+1=Θ(n^2)
The term analysis of algorithms is used to describe approaches to the study of the performance
of algorithm.In this course we will perform the following types of analysis:
- the worst -case runtime complexity of the algorithm is the function defined by the maximum number of steps taken on any instance of size a.
- the best-case runtime complexity of the algorithm is the function defined by the minium number of steps taken on any instance of size a.
- the average-case runtime complexity of the algorithm is the function defined by an average number of steps taken on any instance of size a.
- the amortized runtime complexity of the algorithm is the function defined by a sequence of operation applied to the input of size a and averaged over time.
- its worst-case runtime complexity is O(n)
- its best-case runtime complexity is O(1)
- its average case runtime complexity is O(n/2)=O(n)
Consider a dynamic array stack.in this model push() will double up the array size if there is no enough space.Since copying arrays cannot be performed in constant time, we say that push is also cannot be done in constant time.In this section, we will show that push() takes amortized constant time.
Let us count the number of copying operations needed to do a sequence of pushes.
We see that 3 pushes requires 2+1=3 copies.
We see that 5 pushes requires 4+2+1=7 copies.
We see that 9 pushes requires 8+4+2+1=15 copies
Asymptotically speaking, the number of copies is about the same as the number of pushes.
2^(n+1)-1
limit-------------------=2=O(1)
n->∞ 2^n+1
We say that algorithm runs at amortized constant time.







Không có nhận xét nào:
Đăng nhận xét