Complexity 3
Complexity 3
Most of the exercises below have solutions but you should try first to solve
them. Each subsection with solutions is after the corresponding subsection
with exercises. T
0.01n + 100n2
2n + n0.5 + 0.5n1.25
1
5. The statements below show some features of “Big-Oh” notation for the
functions f ≡ f (n) and g ≡ g(n). Determine whether each statement is
TRUE or FALSE and correct the formula in the latter case.
Rule of sums:
O(f + g) = O(f ) + O(g)
Rule of products:
O(f · g) = O(f ) · O(g)
Transitivity:
if g = O(f ) and h = O(f )
then g = O(h)
2
Which algorithm has better performance in a ”Big-Oh” sense? Work out
exact conditions when these packages outperform each other.
12. Software packages A and B of complexity O(n log n) and O(n), respec-
tively, spend exactly TA (n) = cA n log10 n and TB (n) = cB n milliseconds
to process n data items. During a test, the average time of processing
n = 104 data items with the package A and B is 100 milliseconds and 500
milliseconds, respectively. Work out exact conditions when one package
actually outperforms the other and recommend the best choice if up to
n = 109 items should be processed.
13. Let processing time of an algorithm of Big-Oh complexity O(f (n)) be
directly proportional to f (n). Let three such algorithms A, B, and C
have time complexity O(n2 ), O(n1.5 ), and O(n log n), respectively. During
a test, each algorithm spends 10 seconds to process 100 data items. Derive
the time each algorithm should spend to process 10,000 items.
14. Software packages A and B have processing time exactly TEP = 3n1.5 and
TWP = 0.03n1.75 , respectively. If you are interested in faster processing of
up to n = 108 data items, then which package should be choose?
3
Expression Dominant term(s) O(. . .)
5 + 0.001n3 + 0.025n 0.001n3 O(n3 )
500n + 100n1.5 + 50n log10 n 100n1.5 O(n1.5 )
0.3n + 5n1.5 + 2.5 · n1.75 2.5n1.75 O(n1.75 )
n2 log2 n + n(log2 n)2 n2 log2 n O(n2 log n)
n log3 n + n log2 n n log3 n, n log2 n O(n log n)
3 log8 n + log2 log2 log2 n 3 log8 n O(log n)
4. 100n + 0.01n2 0.01n2 O(n2 )
If it is FALSE then
Is it TRUE
Statement write
or FALSE?
the correct formula
Rule of products:
O(f · g) = O(f ) · O(g) TRUE
5n + 8n2 + 100n3 =
5n + 8n2 + 100n3 = O(n3 )
O(n2 log n) FALSE
6. It is obvious that T (n) ≤ |a0 | + |a1 |n + |a2 |n2 + |a3 |n3 . Thus if n ≥ 1,
then T (n) ≤ cn3 where c = |a0 | + |a1 | + |a2 | + |a3 | so that T (n) is O(n3 ).
7. In the Big-Oh sense, the algorithm B is better. It outperforms the algo-
4
rithm A when TB (n) ≤ TA (n), that is, when 2.5n2 ≤ 0.1n2 log10 n. This
inequality reduces to log10 n ≥ 25, or n ≥ n0 = 1025 . If n ≤ 109 , the
algorithm of choice is A.
8. The constant factors for A and B are:
10 1 1
cA = = ; cB =
1024 log2 1024 1024 10242
Thus, to process 220 = 10242 items the algorithms A and B will spend
1 20 1
TA (220 ) = 2 log2 (220 ) = 20280µs and TB (220 ) = 240 = 220 µs,
1024 10242
respectively. Because TB (220 ) TA (220 ), the method of choice is A.
9. In the Big-Oh sense, the algorithm B is better. It outperforms the algo-
rithm A if TB (n) ≤ TA (n), that is, if 25n ≤ 5n log10 n, or log10 n ≥ 5, or
n ≥ 100, 000.
10. In the “Big-Oh” sense, the algorithm B of complexity O(n) is better than
A of complexity O(n log n). he package B has better performance in a
The package B begins to outperform A when (TA (n) ≥ TB (n), that is,
when 0.1n log2 n ≥ 5 · n. This inequality reduces to 0.1 log2 n ≥ 5, or
n ≥ 250 ≈ 1015 . Thus for processing up to 1012 data items, the package
of choice is A.
11. In the “Big-Oh” sense, the package B of complexity O(n0.5 ) is better
than A of complexity O(n). The package B begins √ to outperform A when
(TA√(n) ≥ TB (n), that is, when 0.001n ≥ 500 n. This inequality reduces
to n ≥ 5 · 105 , or n ≥ 25 · 1010 . Thus for processing up to 109 data items,
the package of choice is A.
12. In the “Big-Oh” sense, the package B of linear complexity O(n) is better
than the package A of O(n log n) complexity. The processing times of the
packages are TA (n) = cA n log10 n and TB (n) = cB n, respectively. The
tests allows us to derive the constant factors:
100 1
cA = 104 log10 104 = 400
500 1
cB = 104 = 20
5
14. In the Big-Oh sense, the package A is better. But it outperforms the
package B when TA (n) ≤ TB (n), that is, when 3n1.5 ≤ 0.03n1.75 . This in-
equality reduces to n0.25 ≥ 3/0.03(= 100), or n ≥ 108 . Thus for processing
up to 108 data items, the package of choice is B.
2. Running time T (n) of processing n data items with another, slightly dif-
ferent algorithm is described by the recurrence:
n
T (n) = k · T + c · k · n; T (1) = 0.
k
Derive a closed form formula for T (n) in terms of c, n, and k and detemine
computational complexity of this algorithm in a “Big-Oh” sense. Hint:
To have the well-defined recurrence, assume that n = k m with the integer
m = logk n and k.
4. Derive the recurrence that describes processing time T (n) of the recursive
method:
6
The range of input variables is 0 ≤ low ≤ goal ≤ high ≤ a.length − 1.
A non-recursive method arrangeTarget() has linear time complexity
T (n) = c · n where n = high − low + 1 and returns integer target in
the range low ≤ target ≤ high. Output values of arrangeTarget()
are equiprobable in this range, e.g. if low = 0 and high = n − 1, then
every target = 0, 1, . . . , n − 1 occurs with the same probability n1 . Time
for performing elementary operations like if–else or return should not be
taken into account in the recurrence.
1 1 1 n
Hint: You might need the equation 1·2 + 2·3 + ··· + n(n+1) = n+1 for
deriving the explicit formula for T (n).
difference equations and ”telescoping” is their solution by substitution of the same equation
for gradually decreasing arguments: n − 1 or n/k.
7
(a) Derivation with “telescoping”: the recurrence T (k m ) = k · T (k m−1 ) +
c · k m can be represented and “telescoped” as follows:
T (k m ) T (k m−1 )
= +c
km k m−1
m−1
T (k ) T (k m−2 )
= +c
k m−1 k m−2
··· ··· ···
T (k) T (1)
= +c
k 1
m
Therefore, T (k
km
)
= c·m, or T (k m ) = c·k m ·m, or T (n) = c·n·logk n.
The Big-Oh complexity is O(n log n).
(b) Another version of telescoping: the like substitution can be applied
directly to the initial recurrence:
T (k m ) = k · T (k m−1 ) + c · k m
k · T (k m−1 ) = k 2 · T (k m−2 ) + c · k m
··· ··· ···
k m−1 · T (k) = k m · T (1) + c · k m so that T (k m ) = c · m · k m
(c) Guessing and math induction: let us construct a few initial values
of T (n), starting from the given T (1) and successively applying the
recurrence:
T (1) = 0
T (k) = k · T (1) + c · k = c · k
T (k 2 ) = k · T (k) + c · k 2 = 2 · c · k 2
T (k 3 ) = k · T (k 2 ) + c · k 3 = 3 · c · k 3
T (k m ) = k · T (k m−1 ) + c · k m
= k · c · (m − 1) · k m−1 + c · k m
= c · (m − 1 + 1) · k m = c · m · k m
8
The Big-Oh complexity of this algorithm is O(n log n).
2. The recurrence T (k m ) = k · T (k m−1 ) + c · k m+1 telescopes as follows:
T (km ) T (km−1 )
km+1 = km +c
T (km−1 ) T (km−2 )
km = km−1 +c
··· ··· ···
T (k) T (1)
k2 = k +c
m
Therefore, Tk(k )
m+1 = c · m, or T (k
m
) = c · k m+1 · m, or T (n) = c · k · n · logk n.
The complexity is O(n log n) because k is constant.
3. Processing time T (n) = c · k · n · logk n can be easily rewritten as T (n) =
c lnkk · n · ln n to give an explicit dependence of k. Because ln22 = 2.8854,
3 4
ln 3 = 2.7307, and ln 4 = 2.8854, the fastest processing is obtained for
k = 3.
4. Because all the variants:
T (n) = T (0) + cn or T (n) = T (n − 1) + cn if target = 0
T (n) = T (1) + cn or T (n) = T (n − 2) + cn if target = 1
T (n) = T (2) + cn or T (n) = T (n − 3) + cn if target = 2
... ... ...
T (n) = T (n − 1) + cn or T (n) = T (0) + cn if target = n − 1
5. The recurrence suggests that nT (n) = T (0)+T (1)+· · ·+T (n−2)+T (n−
1)+cn2 . It follows that (n−1)T (n−1) = T (0)+. . .+T (n−2)+c·(n−1)2 ,
and by subtracting the latter equation from the former one, we obtain the
following basic recurrence: nT (n) − (n − 1)T (n − 1) = T (n − 1) + 2cn − c.
It reduces to nT (n) = n · T (n − 1) + 2cn − c, or T (n) = T (n − 1) + 2c − nc .
Telescoping results in the following system of equalities:
T (n) = T (n − 1) +2c − nc
c
T (n − 1) = T (n − 2) +2c − n−1
··· ··· ··· ··· ···
T (2) = T (1) +2c − 2c
T (1) = T (0) +2c −c
9
6. The recurrence suggests that nT (n) = 2(T (0)+T (1)+. . .+T (n−2)+T (n−
1)) + c · n. Because (n − 1)T (n − 1) = 2(T (0) + . . . + T (n − 2)) + c · (n − 1),
the subtraction of the latter equality from the former one results in the
following basic recurrence nT (n) − (n − 1)T (n − 1) = 2T (n − 1) + c. It
reduces to nT (n) = (n + 1)T (n − 1) + c, or Tn+1 (n)
= T (n−1)
n
c
+ n(n+1) .
Telescoping results in the following system of equalities:
T (n) T (n−1) c
n+1 = n + n(n+1)
T (n−1) T (n−2) c
n = n−1 + (n−1)n
··· ··· ··· ··· ···
T (2) T (1) c
3 = 2 + 2·3
T (1) T (0) c
2 = 1 + 1·2
T (n) 1 c 1 c n
= + + ··· + =c·
n+1 1·2 2·3 (n − 1)n n(n + 1) n+1
so that T (n) = c · n.
T (0) = 0
T (1) = 12 · 0 + c = c
T (2) = 22 (0 + c) + c = 2c
T (3) = 23 (0 + c + 2c) + c = 3c
T (4) = 24 (0 + c + 2c + 3c) + c = 4c
T (n) = n2 (0 + c + 2c + . . . + (n − 1)c) + c
= n2 (n−1)n
2 c+c
= (n − 1)c + c = cn
10
algorithm corresponds to a recurrence: T (2m ) = T (2m−1 ) + 1, and by
telescoping one obtains:
T (2m ) = T (2m−1 ) + 1
T (2m−1 ) = T (2m−2 ) + 1
··· ···
T (2) = T (1) + 1
T (1) = 0
11
4. Work out the computational complexity (in the “Big-Oh” sense) of the
following piece of code and explain how you derived it using the basic
features of the “Big-Oh” notation:
providing the algorithm random( int n ) spends one time unit to return
a random integer value uniformly distributed in the range [0, n] whereas
all other instructions spend a negligibly small time (e.g., T (0) = 0).
Hints: derive and solve the basic recurrence relating T (n) in average to
1 1 1
T (n−1), . . . , T (0). You might need the equation 1·2 + 2·3 +· · ·+ n(n+1) =
n
n+1 for deriving the explicit formula for T (n).
6. Assume that the array a contains n values, that the method randomValue
takes constant number c of computational steps to produce each output
value, and that the method goodSort takes n log n computational steps
to sort the array. Determine the Big-Oh complexity for the following
fragments of code taking into account only the above computational steps:
12
1.6 Time complexity of code: solutions
1. In the outer for-loop, the variable i keeps halving so it goes round log2 n
times. For each i, next loop goes round also log2 n times, because of dou-
bling the variable j. The innermost loop by k goes round n2 times. Loops
are nested, so
the bounds may be multiplied to give that the algorithm is
O n(log n)2 .
2. Running time of the inner, middle, and outer loop is proportional to
n, log n, and log n, respectively. Thus the overall Big-Oh complexity is
O(n(log n)2 ).
More detailed optional analysis gives the same value. Let n = 2k . Then
the outer loop is executed k times, the middle loop is executed k + 1
times, and for each value j = 2k , 2k−1 , . . . , 2, 1, the inner loop has different
execution times:
j Inner iterations
2k 1
2k−1 (2k − 2k−1 ) 12
2k−2 (2k − 2k−2 ) 12
... ...
21 (2k − 21 ) 12
2 0
(2k − 20 ) 12
In total, the number of inner/middle steps is
1 1
1 + k · 2k−1 − (1 + 2 + . . . + 2k−1 ) = 1 + k · 2k−1 − (2k − 1)
2 2
n
= 1.5 + (k − 1) · 2k−1 ≡ (log2 n − 1)
2
= O(n log n)
4. The first and second successive innermost loops have O(n) and O(log n)
complexity, respectively. Thus, the overall complexity of the innermost
part is O(n). The outermost and middle loops have complexity O(log n)
and O(n), so a straightforward (and valid) solution is that the overall
complexity is O(n2 log n).
13
More detailed analysis shows that the outermost and middle loops are
interrelated, and the number of repeating the innermost part is as follows:
1 + 2 + . . . + 2m = 2m+1 − 1
where m = blog2 nc is the smallest integer such that 2m+1 > n. Thus
actually this code has quadratic complexity O(n2 ). But selection of either
answer (O(n2 log n) and O(n2 ) is valid.
5. The algorithm suggests that T (n) = T (i) + T (n − 1 − i) + 1. By summing
this relationship for all the possible random values i = 0, 1, . . . , n − 1, we
obtain that in average nT (n) = 2(T (0)+T (1)+. . .+T (n−2)+T (n−1))+n.
Because (n − 1)T (n − 1) = 2(T (0) + . . . + T (n − 2)) + (n − 1), the basic
recurrence is as follows: nT (n) − (n − 1)T (n − 1) = 2T (n − 1) + 1, or
nT (n) = (n + 1)T (n − 1) + 1, or Tn+1
(n)
= T (n−1)
n
1
+ n(n+1) . The telescoping
results in the following system of expressions:
T (n) T (n−1) 1
n+1 = n + n(n+1)
T (n−1) T (n−2) 1
n = n−1 + (n−1)n
··· ··· ··· ··· ···
T (2) T (1) 1
3 = 2 + 2·3
T (1) T (0) 1
2 = 1 + 1·2
T (n) 1 1 1 1 n
= + + ··· + =
n+1 1·2 2·3 (n − 1)n n(n + 1) n+1
so that T (n) = n.
6. The inner loop has linear complexity cn, but the next called method is
of higher complexity n log n. Because the outer loop is linear in n, the
overall complexity of this piece of code is n2 log n.
14