Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
and
Time-Space trade-off
Complexity
• A measure of the performance of an algorithm
– external factors
External Factors
• Speed of the computer on which it is run
Note:
Complexity measures the internal factors (usually more interested in time than
space)
Need for analysis
• To determine resource consumption
– CPU time
– Memory space
Worst-case: (usually)
• T(n) = maximum time of algorithm on any input of
size n.
Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs of
size n.
• Need assumption of statistical distribution of inputs.
Best-case:
• Cheat with a slow algorithm that works fast on some
input.
What is efficiency?
• Efficiency can be measured by time or space utilization by the
algorithm.
• An efficient algorithm is that which has
– Small running time (Time complexity): The time taken
by a program is the sum of the compiled time & the run
time. The time complexity of an algorithm is given by the
number of steps taken by the algorithm to compute the
function it was written for.
• Small space used (Space complexity): The space
complexity of an algorithm is the amount of memory it
needs to run.
We will consider only the time complexity.
7
How to Measure Running Time of Algorithm?
1. Experimental study (run programs)
2. Growth function: Asymptotic Algorithm Analysis
Factors affecting running time
• Speed: Speed of the machine running the program.
• Language: Language in which the program was written. For example,
programs written in assembly language generally run faster than those
written in C or C++, which in turn tend to run faster than those written in
Java.
• Compiler: Efficiency of the compiler that created the program
• The size of the input: processing 1000 records will take more time than
processing 10 records.
• Organization of the input: if the item we are searching for is at the top
of the list, it will take less time to find it than if it is at the bottom.
8
Two ways of finding complexity
• Experimental study
• Theoretical Analysis
Experimental study
– Instructions
– Simple variables
– Fixed size component variables
– Space for constants
– etc..
Cont……
• The variable part includes space for
– etc..
Time Complexity
• The time complexity of a problem is
19
Basic rules for calculating the time
complexity
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.
5. Conditional checks are constant (i.e. 1).
Example 1
//linear
for(int i = 0; i < n; i++) {
cout << i << endl;
}
• Ans: O(n)
Example 2
//quadratic
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
//do swap stuff, constant time
}
}
• Ans O(n^2)
Example 3
//quadratic
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++){
//do swap stuff, constant time
}
}
• Ans: (n(n+1)/2). This is still in the bound of
O(n^2)
Example 4
for(int i = 0; i < 2*n; i++) {
cout << i << endl;
}
• At first you might say that the upper bound is
O(2n); however, we drop constants so it
becomes O(n)
Example 5
//linear
for(int i = 0; i < n; i++) {
cout << i << endl;
}
//quadratic
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++){
//do constant time stuff
}
}
• Ans : In this case we add each loop's Big O, in
this case n+n^2. O(n^2+n) is not an acceptable
answer since we must drop the lowest term.
The upper bound is O(n^2). Why? Because it
has the largest growth rate
Example 6
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2; j++){
//do stuff
}
}
• Ans: Outer loop is 'n', inner loop is 2, this we
have 2n, dropped constant gives up O(n)
Example 7
for(int i = 1; i < n; i *= 2) {
cout << i << endl;
}
• There are n iterations, however, instead of
simply incrementing, 'i' is increased by 2*itself
each run. Thus the loop is log(n).
Example 8
for(int i = 0; i < n; i++) { //linear
for(int j = 1; j < n; j *= 2){ // log (n)
//do constant time stuff
}
}
• Ans: n*log(n)
Examples
3 * n2 + n/2 + 12 ∈ O(n2)
• The outer loop executes N times. Every time the outer loop executes, the
inner loop executes M times. As a result, the statements in the inner loop
execute a total of N * M times. Thus, the complexity is O(N * M).
• In a common special case where the stopping condition of the inner loop
is j < N instead of j < M (i.e., the inner loop also executes N times), the
total complexity for the two loops is O(N2).
Example 5
for (i = 0; i < N; i++)
{
for (j = i+1; j < N; j++)
{
sequence of statements;
}
}