Professional Documents
Culture Documents
OPERATION COUNT
Recall: Common Upper Bounds
O(1) constant
O(log n) logarithm
O(n) linear
O(n log n) linear-log
O(n2) quadratic
O(n3) cubic
O(2n) exponential
Operation Count
Declarations
none with no initializations
count of 1 with initializations
Delimiters (such as { and })
none
Function Heading
none
Operators (Arithmetic, Relational, Logical)
for simplicity, each operator has a count of 1
Expressions
sum of all operators
Operation Count
Assignment Statement
1count for assignment operator
2 counts for ++, – –, +=, –=, *=, /=, %=
Function call
1(function call) + operation count for the operators + operation count
of the function
if or if–else Statement
operation count of conditional statement + Maximum Operation
Count (if_block, else_block)
for Statement
while Statement
do–while Statement
Nested Loops
Analyze the following code
for (j = 1; j < 10; j++)
printf (“I love Ice Cream”);
1 10 9 (2)
∴ T(n) = 38
Analyze the following code
for (j = 1; j <= 10; j++)
printf (“I love Ice Cream”);
1 11 10(2)
10
∴ T(n) = 42
Analyze the following code
for (j = 5; j < 10; j++)
printf (“I love Ice Cream”);
1 6 5(2)
∴ T(n) = 22
Analyze the following code
for (j = 5; j <= 10; j++)
printf (“I love Ice Cream”);
1 7 6(2)
∴ T(n) = 26
Let’s Generalize
1 b–a+1 b–a
b–a
Let’s Generalize
1 b–a+2 b–a+1
b–a+1
Sample Problems
return nFactorial;
}
(d)
int factorial (int nVal)
{
int j;
int nFactorial = 1;
return nFactorial;
}
(e)
int n, i, j;
scanf (“%d”, &n);
for (i = 0; i < n; i++)
for (j = 0; j< n; j++)
printf (“%d”, i * j);
for (j = 3; j<= n; j++)
1 n-3 +2 2(n-2)
for (k = 0; k<= j; k++)
n n n
Σ1
j=3
Σ
j=3
(j+2) Σ
j=3
2(j+1)
printf(“This is easy”);
n
Σ
j=3
(j+1)
for (j = 1; j < n; j++)
1 n-1 +1 2(n-1)
for (k = 2; k<= j + 1; k++)
n-1 n-1 n-1
Σ1
j=1
Σ
j=1
2(j+1) Σ
j=1
2(j)
printf(“This is easy”);
n-1
Σ
j=1
(j)
for (j = 3; j <= n; j++)
1 n-3+2 2(n-2)
for (k = j; k<= n; k++)
n n n
Σ1
j=3
Σ
j=3
(n-j+2) Σ
j=3
2(n-j+1)
printf(“This is easy”);
n
Σ
j=3
(n-j+1)
for (j = 1; j < n; j++)
1 n-1 +1 2(n-1)
for (k = j+1; k<= 5; k++)
n-1 n-1 n-1
Σ2 Σ (6-j) Σ
j=1
2(5-j)
j=1 j=1
printf(“This is easy”);
n-1
Σ
j=1
(5-j)
Summation/Arithmetic Series
b
n
∑1 = n ∑1= b−a +1
k=a
k =1
n
n ∑ k = 1 + 2 + ... + n
n(n + 1) k =1
∑ k=
2
n
1
k =1 ∑
k =1
k = n(n + 1)
2
n
n(n + 1)(2n + 1)
∑
k =1
2
k =
6
n 2 2
n (n + 1)
∑
k =1
3
k =
4
Geometric Series:
n k 2 n
∑ x = 1 + x + x + ... + x
k =0
n x n + 1 −1
k
∑ x =
k =0 x −1
∞ k 1
∑ x =
k =0 1− x
Harmonic Series:
- arises in probabilistic analyses of algorithms
n
1 1 1 1 1
∑
k =1 k
= 1 + + + + ... +
2 3 4 n
n
1
∑
k =1 k
= ln n + O (1)
Telescoping series
n
∑(a
k =1
k − ak −1 ) = an − a0
Recurrence
An equation or inequality that describes a function in
terms of its value on smaller inputs.
Given a function defined by a recurrence relation,
our objective is to determine a closed form of the
function.
Arises in the analysis of divide and conquer
algorithms and recursive subroutines
3 approaches
Iteration Method Master Method
Substitution Method
References