Objectives of
this lecture
Generating
running time function
unsigned int sum (int n)
{
unsigned int i, partial_sum;
/* 1 */
partial_sum = 0;
/* 2 */
for (i=0; i<=n; i++)
/* 3 */ partial_sum += i*i*i;
/* 4 */
return (partial_sum);
}
Line 1 & 4: This counts for unit time each; [2]
Line 3: This
counts for 3 units (two multiplications and one addition) and is executed
n-times [3n]
Line 2: This has hidden cost of initializing i (1
time); testing (n+1 times); and incrementing (n times) [2n + 2]
f(n) = 2 + 3n + 2 +2n
= 5n + 4.
General
Simplification Rules:
1. for
Loops: The running time of a for loop is at most the running time of
the statements inside the loop times the number of iteration
2. Nested Loops:
Analyze these inside-out. The total
running time of a statement inside a group of nested loops is the running time
of the statements multiplied by the product of the sizes of all the loops.
E.g., the following
program fragment is O(n2)
for (i=0; i<n; i++)
for (j=0; j<n; j++)
k++;
E.g.,
the following program fragment which has O(n) + O(n2) can be taken
to be O(n2).
for (i=0; i<n; i++)
a[i] = 0;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
a[i] += a[j] +i +j;
6. Recursive
Functions: These are evaluated by generating a recurrence relation. We shall see how to do this later.
int
max_subsequence_sum (int a[], int n)
{ int
this_sum, max_sum, i, j, k;
/* 1 */
max_sum=0;
/* 2 */
for (i=0; i<n; i++)
/* 3 */ for (j=i; j<n; j++)
/* 4 */ { this_sum=0;
/* 5 */ for (k=i; k<=j; k++)
/* 6 */ this_sum += a[k];
/* 7 */ if (this_sum > max_sum)
/* 8 */ max_sum = this_sum;
}
/* 9*/
return (max_sum);
}
We observe that the statements in line 5 and 6
which are O(1), are nested inside 3 loops.
The first loop is executed n times.
The second loop is executed n – i + 1,
which can be small, but could also be n.
Since we are estimating the worst case, we take n.
The third loop executes j – i + 1, which
again could be n.
Thus, statements in line 5 and 6 are
executed O(n3) times.
Statements 7 and 8 take O(n2) since
they are contained in two loops and so can be ignored. Other statements can also be ignored
for similar reasons.
Thus, the overall running time of the algorithm
is O(n3).