recursive-resident-evil.html

A Recursive Resident Evil problem

Welcome to Recursive City (optional challenge).

All logs are base 2.

Problem 1: containOutbreak

Umbrella Corp’s bio-containment system uses the following recursive function to monitor and isolate viral spread across NN sectors. The system continuously divides the infected zone in half until containment is complete.

Give the tightest possible Big-Θ\Theta runtime bound for containOutbreak(N) as a function of NN. Show your work drawing a recursion tree.

int containOutbreak(int N) {
    if (N <= 1) {
        return 1;
    }
    int M = N;
    while (M >= 1) {
        M = M / 2;
    }
    return containOutbreak(N / 2);
}
Show solutionHide solution

Each call makes one recursive call on N/2N/2, so the recursion tree is a single chain. Since NN is halved at every level, the tree has height log⁡N\log N, which gives log⁡N+1\log N + 1 levels: level i=0i = 0 is the root and level i=log⁡Ni = \log N is the base case.

Recursion tree for containOutbreak: a single chain of nodes N, N/2, N/4, down to 2 and 1
level iiwork
00log⁡N\log N
11log⁡(N/2)\log(N/2)
22log⁡(N/4)\log(N/4)
⋮\vdots⋮\vdots
log⁡N−1\log N - 1log⁡2=1\log 2 = 1
log⁡N\log NΘ(1)\Theta(1) (base case)

At level ii, the input is N/2iN/2^i, and the while loop halves MM until it reaches 00. That takes about log⁡(N/2i)\log(N/2^i) iterations, so the total work is

∑i=0log⁡Nlog⁡N2i=∑i=0log⁡N(log⁡N−log⁡2i)quotient rule: log⁡ab=log⁡a−log⁡b=∑i=0log⁡Nlog⁡N  −  ∑i=0log⁡Nlog⁡2i=∑i=0log⁡Nlog⁡N  −  ∑i=0log⁡Nilog⁡2power rule: log⁡ak=klog⁡a=∑i=0log⁡Nlog⁡N  −  log⁡2∑i=0log⁡Ni \begin{aligned} \sum_{i=0}^{\log N} \log\frac{N}{2^i} &= \sum_{i=0}^{\log N} \left(\log N - \log 2^i\right) && \text{quotient rule: } \log\tfrac{a}{b} = \log a - \log b \\ &= \sum_{i=0}^{\log N} \log N \;-\; \sum_{i=0}^{\log N} \log 2^i \\ &= \sum_{i=0}^{\log N} \log N \;-\; \sum_{i=0}^{\log N} i \log 2 && \text{power rule: } \log a^k = k \log a \\ &= \sum_{i=0}^{\log N} \log N \;-\; \log 2 \sum_{i=0}^{\log N} i \end{aligned}

Our logs are base 2, so log⁡2=1\log 2 = 1. Now we evaluate each sum.

First sum: ii runs from 00 to log⁡N\log N, so we add up log⁡N+1\log N + 1 copies of log⁡N\log N (one per level):

∑i=0log⁡Nlog⁡N=log⁡N (log⁡N+1)=(log⁡N)2+log⁡N \sum_{i=0}^{\log N} \log N = \log N\,(\log N + 1) = (\log N)^2 + \log N

Second sum: this is an arithmetic sum, so we use 1+2+⋯+k=k(k+1)21 + 2 + \dots + k = \frac{k(k+1)}{2} with k=log⁡Nk = \log N:

∑i=0log⁡Ni=log⁡N (log⁡N+1)2=(log⁡N)2+log⁡N2 \sum_{i=0}^{\log N} i = \frac{\log N\,(\log N + 1)}{2} = \frac{(\log N)^2 + \log N}{2}

Combining:

[(log⁡N)2+log⁡N]−1⋅(log⁡N)2+log⁡N2=(log⁡N)2−12(log⁡N)2+log⁡N−12log⁡N=12(log⁡N)2+12log⁡N \begin{aligned} \left[(\log N)^2 + \log N\right] - 1 \cdot \frac{(\log N)^2 + \log N}{2} &= (\log N)^2 - \tfrac{1}{2}(\log N)^2 + \log N - \tfrac{1}{2}\log N \\ &= \tfrac{1}{2}(\log N)^2 + \tfrac{1}{2}\log N \end{aligned}

Let X=(log⁡N)2X = (\log N)^2. The leftover 12log⁡N\tfrac{1}{2}\log N term is lower order, so it doesn’t contribute asymptotically:

X−12X=12X  ∈  Θ ⁣(12X)=Θ(X)=Θ ⁣((log⁡N)2) X - \tfrac{1}{2}X = \tfrac{1}{2}X \;\in\; \Theta\!\left(\tfrac{1}{2}X\right) = \Theta(X) = \boxed{\Theta\!\left((\log N)^2\right)}

Note: precisely, the while loop runs ⌊log⁡(N/2i)⌋+1\lfloor \log(N/2^i) \rfloor + 1 times, and the base case does 11 unit of work. These extra +1+1’s add only Θ(log⁡N)\Theta(\log N) in total, so they don’t change the bound.

Alternative solution. Using the quotient rule up front, each level’s work simplifies to log⁡N2i=log⁡N−i\log\frac{N}{2^i} = \log N - i. So the levels do log⁡N, log⁡N−1, log⁡N−2, …, 1\log N,\ \log N - 1,\ \log N - 2,\ \dots,\ 1 units of work, and the base case at level log⁡N\log N does constant work. Reading the tree from the bottom up, this is just an arithmetic sum:

∑i=0log⁡N(log⁡N−i)=0+1+2+⋯+log⁡N=log⁡N (log⁡N+1)2∈Θ ⁣((log⁡N)2) \sum_{i=0}^{\log N} (\log N - i) = 0 + 1 + 2 + \dots + \log N = \frac{\log N\,(\log N + 1)}{2} \in \Theta\!\left((\log N)^2\right)

Problem 2: gVirusOutbreak

Bryan Hodukavich, an organ delivery driver for Lightfoot Medical Transport has just breached Raccoon City General. Inside, Umbrella Corporation’s bio-containment network is executing a binary search and quarantine routine. However, the G-Virus has mutated, causing the system to bifurcate its diagnostic scanning into two parallel recursive sub-tasks at every step.

int gVirusOutbreak(int N) {
    if (N <= 1) {
        return 1;
    }
    int M = N;
    while (M >= 1) {
        M = M / 2;
    }
    return gVirusOutbreak(N / 2) + gVirusOutbreak(N / 2);
}

(a) Runtime

Find the tightest possible Big-Θ\Theta bound for gVirusOutbreak(N) as a function of NN. You may show your work using a recursion tree diagram. Final steps for this runtime analysis involve simplifying a common yet messy summation that appears in math, CS, and courses in probability. A change-of-index, infinite geometric series, algebraic manipulation, and/or calculus may help with this final part.

Show solutionHide solution

Each call makes two recursive calls on N/2N/2, so the number of nodes doubles at every level. The tree has height log⁡N\log N: level i=0i = 0 is the root and level i=log⁡Ni = \log N is the leaf (base case) level, which has 2log⁡N=N2^{\log N} = N nodes.

Recursion tree for gVirusOutbreak: each node N splits into two N/2 nodes, then four N/4 nodes, down to N leaves of size 1
level ii# nodeswork per node
0011log⁡N\log N
1122log⁡(N/2)\log(N/2)
2244log⁡(N/4)\log(N/4)
⋮\vdots⋮\vdots⋮\vdots
log⁡N\log NNNΘ(1)\Theta(1)

The work done by a single node depends on whether it is an internal node or a leaf:

work per node at level i={log⁡N2iif i<log⁡N(internal node)Θ(1)if i=log⁡N(leaf, base case) \text{work per node at level } i = \begin{cases} \log\dfrac{N}{2^i} & \text{if } i < \log N \quad \text{(internal node)} \\[8pt] \Theta(1) & \text{if } i = \log N \quad \text{(leaf, base case)} \end{cases}

Notice that plugging i=log⁡Ni = \log N into the internal-node formula gives log⁡N2log⁡N=log⁡1=0\log\frac{N}{2^{\log N}} = \log 1 = 0. That formula counts while loop iterations, but a leaf never reaches the loop: it hits the base case and returns. So the leaves still do Θ(1)\Theta(1) work each, and we count them separately.

Summing over the levels, with 2i2^i nodes at level ii:

∑i=0log⁡N−12ilog⁡N2i⏟internal nodes (while loop)  +  2log⁡N⋅Θ(1)⏟leaves (base case)  =  ∑i=0log⁡N−12ilog⁡N2i  +  Θ(N) \underbrace{\sum_{i=0}^{\log N - 1} 2^i \log\frac{N}{2^i}}_{\text{internal nodes (\texttt{while} loop)}} \;+\; \underbrace{2^{\log N} \cdot \Theta(1)}_{\text{leaves (base case)}} \;=\; \sum_{i=0}^{\log N - 1} 2^i \log\frac{N}{2^i} \;+\; \Theta(N)

Strategy: Instead of computing the internal-node sum exactly, we trap it between an upper bound and a lower bound. If both bounds grow like the same function of NN, that function is our Big-Θ\Theta bound. We start with the upper bound.

Upper bound. First, we unravel the log⁡N2i\log\frac{N}{2^i} term using log properties. Then we let X=log⁡NX = \log N, so the upper index log⁡N−1\log N - 1 becomes X−1X - 1:

∑i=0log⁡N−12ilog⁡N2i=∑i=0log⁡N−12i(log⁡N−log⁡2i)quotient rule: log⁡ab=log⁡a−log⁡b=∑i=0X−12i(X−log⁡2i)let X=log⁡N=∑i=0X−12i(X−i)power rule: log⁡2i=ilog⁡2=i \begin{aligned} \sum_{i=0}^{\log N - 1} 2^i \log\frac{N}{2^i} &= \sum_{i=0}^{\log N - 1} 2^i \left(\log N - \log 2^i\right) && \text{quotient rule: } \log\tfrac{a}{b} = \log a - \log b \\ &= \sum_{i=0}^{X - 1} 2^i \left(X - \log 2^i\right) && \text{let } X = \log N \\ &= \sum_{i=0}^{X - 1} 2^i \left(X - i\right) && \text{power rule: } \log 2^i = i \log 2 = i \end{aligned}

Next, we change the index. Let j=X−ij = X - i, which rewrites to i=X−ji = X - j. Substituting into each term:

2i(X−i)=2X−j(X−(X−j))=2X−j⋅j=2log⁡N−j⋅j=2log⁡N2j⋅jexponent rule: am−n=aman=N2j⋅j2log⁡N=N \begin{aligned} 2^i (X - i) &= 2^{X - j} \bigl(X - (X - j)\bigr) \\ &= 2^{X - j} \cdot j \\ &= 2^{\log N - j} \cdot j \\ &= \frac{2^{\log N}}{2^j} \cdot j && \text{exponent rule: } a^{m - n} = \tfrac{a^m}{a^n} \\ &= \frac{N}{2^j} \cdot j && 2^{\log N} = N \end{aligned}

The endpoints change too. Plugging each original endpoint into j=X−ij = X - i:

i=0  ⟹  j=X−0=Xi=X−1  ⟹  j=X−(X−1)=1 i = 0 \;\Longrightarrow\; j = X - 0 = X \qquad\qquad i = X - 1 \;\Longrightarrow\; j = X - (X - 1) = 1

So jj runs from XX down to 11. Since addition is commutative, the order we add the terms in doesn’t matter, so we can swap the endpoints and put j=1j = 1 at the bottom of the summation:

∑i=0X−12i(X−i)  =  ∑j=1XN2j⋅j  =  N∑j=1log⁡Nj2j \sum_{i=0}^{X - 1} 2^i (X - i) \;=\; \sum_{j=1}^{X} \frac{N}{2^j} \cdot j \;=\; N \sum_{j=1}^{\log N} \frac{j}{2^j}

We can upper bound ∑j2j\sum \frac{j}{2^j} by letting jj run all the way from 11 to ∞\infty. This helps us because adding additional positive terms to a sum only increases the sum:

∑j=1log⁡Nj2j  ≤  ∑j=1∞j2j  =  M \sum_{j=1}^{\log N} \frac{j}{2^j} \;\leq\; \sum_{j=1}^{\infty} \frac{j}{2^j} \;=\; M

where MM is the resulting sum. Now we find MM:

M=121+222+323+424+⋯=12+24+38+416+⋯ \begin{aligned} M &= \frac{1}{2^1} + \frac{2}{2^2} + \frac{3}{2^3} + \frac{4}{2^4} + \cdots \\ &= \frac{1}{2} + \frac{2}{4} + \frac{3}{8} + \frac{4}{16} + \cdots \end{aligned}

How can we massage this to look like a toy geometric series that we’re familiar with? Say,

12+14+18+116+⋯ \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{16} + \cdots

We know this toy series evaluates to S=a11−rS = \dfrac{a_1}{1 - r}, where a1a_1 is the first term of the series and rr is the common ratio, provided that ∣r∣<1|r| < 1. Here a1=12a_1 = \frac{1}{2} and r=12r = \frac{1}{2}:

S=a11−r=121−12=1212=1 S = \frac{a_1}{1 - r} = \frac{\frac{1}{2}}{1 - \frac{1}{2}} = \frac{\frac{1}{2}}{\frac{1}{2}} = 1

Now we rewrite MM. If we take 12⋅M\frac{1}{2} \cdot M, every term is cut in half:

12M=14+28+316+432+⋯ \frac{1}{2} M = \frac{1}{4} + \frac{2}{8} + \frac{3}{16} + \frac{4}{32} + \cdots

But M−12MM - \frac{1}{2} M lines up like a classic subtraction problem, where we subtract terms with the same denominator:

M=12+24+38+416  +  ⋯−12M=14+28+316  +  ⋯M−12M=12+14+18+116  +  ⋯ \def\arraystretch{2.2} \begin{array}{rrcccccccl} & M & = & \dfrac{1}{2} & + & \dfrac{2}{4} & + & \dfrac{3}{8} & + & \dfrac{4}{16} \;+\; \cdots \\ {-} & \tfrac{1}{2} M & = & & & \dfrac{1}{4} & + & \dfrac{2}{8} & + & \dfrac{3}{16} \;+\; \cdots \\ \hline & M - \tfrac{1}{2} M & = & \dfrac{1}{2} & + & \dfrac{1}{4} & + & \dfrac{1}{8} & + & \dfrac{1}{16} \;+\; \cdots \end{array}

This is a geometric series we’re familiar with! With a1=12a_1 = \frac{1}{2} and r=12r = \frac{1}{2}:

M−12M=121−12=1    ⟹    12M=1    ⟹    M=2 M - \frac{1}{2} M = \frac{\frac{1}{2}}{1 - \frac{1}{2}} = 1 \;\;\Longrightarrow\;\; \frac{1}{2} M = 1 \;\;\Longrightarrow\;\; M = 2

We now revisit the original summation with this new knowledge. We don’t know the exact value of the finite sum, but it is at most the infinite sum M=2M = 2:

N∑j=1log⁡Nj2j  ≤  N∑j=1∞j2j  =  N⋅2  =  2N N \sum_{j=1}^{\log N} \frac{j}{2^j} \;\leq\; N \sum_{j=1}^{\infty} \frac{j}{2^j} \;=\; N \cdot 2 \;=\; 2N

Finally, we return to summing over the levels, with 2i2^i nodes at level ii. Calling the total work T(N)T(N), the internal-node summation is now upper bounded by 2N2N:

T(N)  =  ∑i=0log⁡N−12ilog⁡N2i⏟≤ 2N  +  2log⁡N⋅Θ(1)⏟Θ(N)  ≤  2N+Θ(N) T(N) \;=\; \underbrace{\sum_{i=0}^{\log N - 1} 2^i \log\frac{N}{2^i}}_{\leq\, 2N} \;+\; \underbrace{2^{\log N} \cdot \Theta(1)}_{\Theta(N)} \;\leq\; 2N + \Theta(N)

So T(N)∈O(N)T(N) \in O(N).

Lower bound. We’re not finished (unfortunately) ☹. We need to find a lower bound now. Recall the summations we found by adding up all the work in the recursion tree:

T(N)  =  ∑i=0log⁡N−12ilog⁡N2i⏟internal nodes (while loop)  +  2log⁡N⋅Θ(1)⏟leaves (base case) T(N) \;=\; \underbrace{\sum_{i=0}^{\log N - 1} 2^i \log\frac{N}{2^i}}_{\text{internal nodes (\texttt{while} loop)}} \;+\; \underbrace{2^{\log N} \cdot \Theta(1)}_{\text{leaves (base case)}}

We focus on the first summation. Recall that we were able to massage it with a change of index, which lets us rewrite it as:

∑i=0log⁡N−12ilog⁡N2i  =  N∑j=1Xj2j,where X=log⁡N \sum_{i=0}^{\log N - 1} 2^i \log\frac{N}{2^i} \;=\; N \sum_{j=1}^{X} \frac{j}{2^j}, \qquad \text{where } X = \log N

Now we find a lower bound for the factor representing the summation, ∑j=1Xj2j\sum_{j=1}^{X} \frac{j}{2^j}:

∑j=1Xj2j  ≥  ∑j=1X12j \sum_{j=1}^{X} \frac{j}{2^j} \;\geq\; \sum_{j=1}^{X} \frac{1}{2^j}

How does this effectively create a lower bound for us to work with? Every jj is at least 11, so j2j≥12j\frac{j}{2^j} \geq \frac{1}{2^j} for every term, and replacing each term with a smaller one can only make the sum smaller. We do this because in rigorous asymptotic analysis, we typically take advantage of lower and upper bounds, and here the lower bound leads us to a finite geometric series:

∑j=1Xj2j  ≥  ∑j=1X12j  =  12+122+123+124+⋯+12X  =  Q \sum_{j=1}^{X} \frac{j}{2^j} \;\geq\; \sum_{j=1}^{X} \frac{1}{2^j} \;=\; \frac{1}{2} + \frac{1}{2^2} + \frac{1}{2^3} + \frac{1}{2^4} + \cdots + \frac{1}{2^X} \;=\; Q

We’ll apply the same trick we used to find the upper bound (recall M−12MM - \frac{1}{2}M), but now with respect to QQ:

Q=12+122+123+⋯+12X−12Q=122+123+⋯+12X+  12X+1Q−12Q=12+0+0+⋯+0−  12X+1 \def\arraystretch{2.2} \begin{array}{rrcccccccccccl} & Q & = & \dfrac{1}{2} & + & \dfrac{1}{2^2} & + & \dfrac{1}{2^3} & + & \cdots & + & \dfrac{1}{2^X} & \\ {-} & \tfrac{1}{2} Q & = & & & \dfrac{1}{2^2} & + & \dfrac{1}{2^3} & + & \cdots & + & \dfrac{1}{2^X} & + \;\dfrac{1}{2^{X+1}} \\ \hline & Q - \tfrac{1}{2} Q & = & \dfrac{1}{2} & + & 0 & + & 0 & + & \cdots & + & 0 & - \;\dfrac{1}{2^{X+1}} \end{array}

Every middle term cancels, leaving only the first term of QQ and the last term of 12Q\frac{1}{2}Q:

Q−12Q=12−12X+1    ⟹    12Q=12−12X+1    ⟹    Q=1−22X+1=1−12X Q - \frac{1}{2} Q = \frac{1}{2} - \frac{1}{2^{X+1}} \;\;\Longrightarrow\;\; \frac{1}{2} Q = \frac{1}{2} - \frac{1}{2^{X+1}} \;\;\Longrightarrow\;\; Q = 1 - \frac{2}{2^{X+1}} = 1 - \frac{1}{2^X}

So:

∑j=1Xj2j  ≥  ∑j=1X12j  =  1−12X,where X=log⁡N \sum_{j=1}^{X} \frac{j}{2^j} \;\geq\; \sum_{j=1}^{X} \frac{1}{2^j} \;=\; 1 - \frac{1}{2^X}, \qquad \text{where } X = \log N

Since 2X=2log⁡N=N2^X = 2^{\log N} = N, the right-hand side equals 1−1N1 - \frac{1}{N}. Whenever N≥2N \geq 2, we have 1N≤12\frac{1}{N} \leq \frac{1}{2} (for example, N=2N = 2 makes the right-hand side exactly 12\frac{1}{2}). Thus:

1−12X  =  1−1N  ≥  1−12  =  12⟹∑j=1log⁡Nj2j  ≥  12,for N≥2 1 - \frac{1}{2^X} \;=\; 1 - \frac{1}{N} \;\geq\; 1 - \frac{1}{2} \;=\; \frac{1}{2} \qquad\Longrightarrow\qquad \sum_{j=1}^{\log N} \frac{j}{2^j} \;\geq\; \frac{1}{2}, \quad \text{for } N \geq 2

Now we multiply both sides of the inequality by NN. Since N>0N > 0, the inequality doesn’t flip:

N∑j=1log⁡Nj2j  ≥  N2 N \sum_{j=1}^{\log N} \frac{j}{2^j} \;\geq\; \frac{N}{2}

Tying this back to T(N)T(N):

T(N)  =  N∑j=1log⁡Nj2j⏟≥ N2  +  2log⁡N⋅Θ(1)⏟Θ(N)  ≥  N2+Θ(N) T(N) \;=\; \underbrace{N \sum_{j=1}^{\log N} \frac{j}{2^j}}_{\geq\, \frac{N}{2}} \;+\; \underbrace{2^{\log N} \cdot \Theta(1)}_{\Theta(N)} \;\geq\; \frac{N}{2} + \Theta(N)

So T(N)∈Ω(N)T(N) \in \Omega(N).

We’ve achieved a lower bound! Since the lower bound and the upper bound are in terms of the same function of NN,

N2+Θ(N)⏟lower bound: Ω(N)  ≤  T(N)  ≤  2N+Θ(N)⏟upper bound: O(N) \underbrace{\frac{N}{2} + \Theta(N)}_{\text{lower bound: } \Omega(N)} \;\leq\; T(N) \;\leq\; \underbrace{2N + \Theta(N)}_{\text{upper bound: } O(N)}

we have shown that gVirusOutbreak(N) ∈Θ(N)\in \boxed{\Theta(N)}.

PHEW!

(b) Space complexity

What is the tightest Big-Θ\Theta bound on the memory space (call stack) required by gVirusOutbreak(N)?

Show solutionHide solution

Θ(log⁡N)\Theta(\log N). Even though the tree has about 2N2N nodes, they are never all on the call stack at once. The first call to gVirusOutbreak(N / 2) runs to completion and returns before the second one starts, so the stack only ever holds one root-to-leaf path. That path has log⁡N+1\log N + 1 frames, and each frame stores only a constant amount of data (NN and MM), so the space used is Θ(log⁡N)\Theta(\log N).

Problem 3: nemesisProtocol

As Bryan escapes Raccoon City General, Umbrella Corporation unleashes its Nemesis-T Type, a bio-weapon programmed to hunt down every surviving witness. Unlike the earlier containment routines, Nemesis leaves nothing to chance: before splitting its pursuit into two parallel search teams, it sweeps every single one of the NN sectors in its current zone.

int nemesisProtocol(int N) {
    if (N <= 1) {
        return 1;
    }
    for (int i = 0; i < N; i++) {
        searchSector(i); // Assume searchSector() runs in Θ(1)
    }
    return nemesisProtocol(N / 2) + nemesisProtocol(N / 2);
}

(a) Runtime

Find the tightest possible Big-Θ\Theta bound for nemesisProtocol(N) as a function of NN. You may show your work using a recursion tree diagram.

Show solutionHide solution

Each call makes two recursive calls on N/2N/2, so level ii has 2i2^i nodes, and the tree has height log⁡N\log N. A node at level ii has input N/2iN/2^i, so its for loop calls the Θ(1)\Theta(1) searchSector method N/2iN/2^i times.

Recursion tree for nemesisProtocol: each node N splits into two N/2 nodes, then four N/4 nodes, down to N leaves of size 1
level ii# nodeswork/nodework/level
0011NNNN
1122N/2N/2NN
2244N/4N/4NN
⋮\vdots⋮\vdots⋮\vdots⋮\vdots
log⁡N\log NNNΘ(1)\Theta(1)Θ(N)\Theta(N)

At every internal level, the number of nodes doubles while the work per node halves, so each level does the same amount of work:

2i⋅N2i=N 2^i \cdot \frac{N}{2^i} = N

The leaf level has 2log⁡N=N2^{\log N} = N base-case nodes doing Θ(1)\Theta(1) work each, which is another Θ(N)\Theta(N). Summing over the log⁡N\log N internal levels plus the leaves:

∑i=0log⁡N−1N⏟internal nodes  +  Θ(N)⏟leaves  =  Nlog⁡N+Θ(N)  ∈  Θ(Nlog⁡N) \underbrace{\sum_{i=0}^{\log N - 1} N}_{\text{internal nodes}} \;+\; \underbrace{\Theta(N)}_{\text{leaves}} \;=\; N\log N + \Theta(N) \;\in\; \boxed{\Theta(N \log N)}

Unlike gVirusOutbreak, no single level dominates here: every level does the same NN work, so the total is (work per level) ×\times (number of levels).

(b) Space complexity

What is the tightest Big-Θ\Theta bound on the memory space (call stack) required by nemesisProtocol(N)? How does it compare to gVirusOutbreak(N), and why?

Show solutionHide solution

Θ(log⁡N)\Theta(\log N), the same as gVirusOutbreak(N). The for loop changes how much time each call takes, but not how deep the recursion goes. As before, the first recursive call finishes before the second one starts, so the stack only ever holds one root-to-leaf path of log⁡N+1\log N + 1 frames, each storing a constant amount of data (NN and ii).

2851 words