Table of Contents This series is my solutions for algorithm exercises in'The Algorithm Design Manual' 3rd edition. It's my solutions, not model answers, so if you find some mistakes or more sophisticated solutions, please post in comment area below.
Here
What value is returned by the following function? Express your answer as a function of n. Give the worst-case running time using Big Oh notation.
function Conundrum( n)
r:= 0
for i= 1 to n do
for j= 1 + 1 to n do
for k= i+ j- 1 to n do
r= r+ 1
return r
Let T ( n ) T(n) T ( n ) denote the number of times r = r + 1 r=r+1 r = r + 1 is executed as a function of n.
T ( n ) = ∑ i = 1 n ∑ j = i + 1 n ∑ k = i + j − 1 n \begin{equation} T(n)=\sum_{i=1}^{n}\sum_{j=i+1}^{n}\sum_{k=i+j-1}^{n} \end{equation} T ( n ) = i = 1 ∑ n j = i + 1 ∑ n k = i + j − 1 ∑ n Using k's range, i and j is bounded as follows:
i + j − 1 ≤ k ≤ n ⟹ i + j ≤ n + 1 \begin{align} i+j-1\le k \le n \\ \implies i+j \le n+1 \end{align} i + j − 1 ≤ k ≤ n ⟹ i + j ≤ n + 1 So, we can rewrite T ( n ) T(n) T ( n ) , then
T ( n ) = ∑ i = 1 n ∑ j = i + 1 n F ( n , i , j ) \begin{equation} T(n)=\sum_{i=1}^{n}\sum_{j=i+1}^{n}F(n,i,j) \end{equation} T ( n ) = i = 1 ∑ n j = i + 1 ∑ n F ( n , i , j ) F ( n ) = { n + 2 − i − j if i + j ≤ n + 1 0 else \begin{equation} F(n)= \begin{cases} n+2-i-j & \quad \text{if } i+j \le n+1\\ 0 & \quad \text{else } \end{cases} \end{equation} F ( n ) = { n + 2 − i − j 0 if i + j ≤ n + 1 else Formula F ( ) F() F ( ) 's characteristic narrows the j's summation range and erases itself in formula T ( ) T() T ( ) :
T ( n ) = ∑ i = 1 n ∑ j = i + 1 n − i + 1 n + 2 − i − j \begin{equation} T(n)=\sum_{i=1}^{n}\sum_{j=i+1}^{n-i+1}n+2-i-j \end{equation} T ( n ) = i = 1 ∑ n j = i + 1 ∑ n − i + 1 n + 2 − i − j Similarly to k, using j's range, i is bounded as follows:
i + 1 ≤ j ≤ n − i + 1 ⟹ i ≤ n 2 \begin{align} i+1 \le j \le n-i+1\\ \implies i \le \frac{n}{2} \end{align} i + 1 ≤ j ≤ n − i + 1 ⟹ i ≤ 2 n i is integer, so:
i ≤ ⌊ n 2 ⌋ \begin{equation} i \le \left\lfloor\frac{n}{2}\right\rfloor \end{equation} i ≤ ⌊ 2 n ⌋ The new range introduces:
T ( n ) = ∑ i = 1 ⌊ n 2 ⌋ ∑ j = i + 1 n − i + 1 n + 2 − i − j ⏟ T’(n) \begin{equation} T(n)=\sum_{i=1}^{\left\lfloor\frac{n}{2}\right\rfloor}\underbrace{\sum_{j=i+1}^{n-i+1}n+2-i-j}_\text{T'(n)} \end{equation} T ( n ) = i = 1 ∑ ⌊ 2 n ⌋ T’(n) j = i + 1 ∑ n − i + 1 n + 2 − i − j First reduce the T ′ ( n ) T'(n) T ′ ( n ) :
T ′ ( n ) = ∑ j = i + 1 n − i + 1 ( n − i + 2 ) − ( ∑ j = 1 n − i + 1 j − ∑ j = 1 i j ) = 1 2 ( 4 i 2 − 4 n i − 6 i + n 2 + 3 n + 2 ) \begin{align} T'(n)=\sum_{j=i+1}^{n-i+1}(n-i+2)-(\sum_{j=1}^{n-i+1}j-\sum_{j=1}^{i}j)\\ =\frac{1}{2}(4i^2-4ni-6i+n^2+3n+2) \end{align} T ′ ( n ) = j = i + 1 ∑ n − i + 1 ( n − i + 2 ) − ( j = 1 ∑ n − i + 1 j − j = 1 ∑ i j ) = 2 1 ( 4 i 2 − 4 ni − 6 i + n 2 + 3 n + 2 ) With the result above, reduce T ( n ) T(n) T ( n ) :
T ( n ) = 1 6 ⌊ n 2 ⌋ ( 4 ⌊ n 2 ⌋ 2 − 6 n ⌊ n 2 ⌋ − 3 ⌊ n 2 ⌋ + 3 n 2 + 3 n − 1 ) \begin{equation} T(n)=\frac{1}{6}\left\lfloor\frac{n}{2}\right\rfloor(4\left\lfloor\frac{n}{2}\right\rfloor^2-6n\left\lfloor\frac{n}{2}\right\rfloor-3\left\lfloor\frac{n}{2}\right\rfloor+3n^2+3n-1) \end{equation} T ( n ) = 6 1 ⌊ 2 n ⌋ ( 4 ⌊ 2 n ⌋ 2 − 6 n ⌊ 2 n ⌋ − 3 ⌊ 2 n ⌋ + 3 n 2 + 3 n − 1 ) The worst case running time is the order of Θ ( n 3 ) \Theta(n^3) Θ ( n 3 ) .