Discrete MathematicsAlgorithms and Growth of Functions2 of 4
Algorithms and Growth of Functions · 2

Counting Steps: Time Complexity of Loops and Recursive Algorithms

We already know how to simplify a step-count formula into Big O, Big Ω\Omega, and Big Θ\Theta, and now we need to count the steps directly from a piece of code.

1

Single Loops: Stepping vs. Doubling

To find the time complexity of a single loop that runs from start to finish, we count how many turns the loop takes as a function of the input size nn and how much work happens inside each turn.

Single Loops: Stepping vs. Doubling
Text solution
Step by a constantWhen `for i = 1 to n` steps by +1\counter{+1} (or by +2\counter{+2}, which visits every other number), the steps form a line (or half a line) whose length grows proportionally to nn.
Steps=n=Θ(n)andSteps=n2=Θ(n)\displaystyle \begin{aligned} \text{Steps} &= \work{n} = \Theta(n) \\ \text{and} \quad \text{Steps} &= \work{\frac{n}{2}} = \Theta(n) \end{aligned}
Track i\counter{i} as a power of 22When a loop starts at i=1\counter{i = 1} and doubles the counter on each turn (`i = i * 2`), the value of i\counter{i} after k\work{k} steps is 2k\counter{2^k}.
i=2×2×⋯×2⏟k doublings=2k\displaystyle \begin{aligned} \counter{i} &= \underbrace{2 \times 2 \times \dots \times 2}_{\work{k} \text{ doublings}} = \counter{2^k} \end{aligned}
Solve for the stopping step k\work{k}The loop stops as soon as i\counter{i} reaches nn, so we set 2k=n\counter{2^k} = n and take the base-22 logarithm to find the total number of turns.
2k=n  ⟹  k=log⁡2(n)=Θ(log⁡n)\displaystyle \begin{aligned} \counter{2^k} = n \implies \work{k} &= \work{\log_2(n)} \\ &= \Theta(\log n) \end{aligned}
2

Nested Loops: Grids, Rectangles, and Triangles

Now we want to find the total number of steps when one loop is nested inside another, so that the inner loop runs all over again on every turn of the outer loop. When the inner loop's upper limit changes with the outer counter i\counter{i}, counting the steps row by row uses the sum of the first nn positive integers:

1+2+3+⋯+n\displaystyle \work{1} + \work{2} + \work{3} + \dots + \work{n} =n(n+1)2\displaystyle = \frac{n(n + 1)}{2}
Nested Loops: Grids, Rectangles, and Triangles
for i = 1 to n:\texttt{for i = 1 to n:}
for j = 1 to n:\texttt{for j = 1 to n:}
do 1 step of work\texttt{do 1 step of work}
i\htmlClass{lc-pink}{i}
1\htmlClass{lc-pink}{1}
2\htmlClass{lc-pink}{2}
Text solution
Multiply rows by columns when the bounds are independentWhen the outer loop runs nn times and the inner loop runs the same number of turns every time (either n\work{n} steps by adding 11, or log⁡2(n)\work{\log_2(n)} steps by doubling j\counter{j}), the steps form a full grid or rectangle.
n×n=n2=Θ(n2)andn×log⁡2(n)=Θ(nlog⁡n)\displaystyle \begin{aligned} n \times \work{n} &= n^2 = \Theta(n^2) \\ \text{and} \quad n \times \work{\log_2(n)} &= \Theta(n \log n) \end{aligned}
Add the rows when the inner bound depends on i\counter{i}In `for i = 1 to n: for j = 1 to i`, row 1\counter{1} has 1\work{1} dot, row 2\counter{2} has 2\work{2} dots, and row n\counter{n} has n\work{n} dots, forming a right-triangular staircase that fills half of the nn by nn square.
Steps=1+2+3+⋯+n=n(n+1)2\displaystyle \begin{aligned} \text{Steps} &= \work{1} + \work{2} + \work{3} + \dots + \work{n} \\ &= \frac{n(n + 1)}{2} \end{aligned}
Drop the lower-order term and constant multiplierExpanding the triangle's area into half of the n2\work{n^2} square plus half of the n\leftover{n} diagonal dots shows that half a square still grows like n2n^2.
12n2+12n\displaystyle \work{\frac{1}{2}n^2} + \leftover{\frac{1}{2}n} =Θ(n2)\displaystyle = \Theta(n^2)

In an nn by nn grid of steps, stopping the inner loop at i\counter{i} cuts the square in half along its diagonal to give n(n+1)2=Θ(n2)\frac{n(n + 1)}{2} = \Theta(n^2) dots, whereas doubling j\counter{j} shrinks the width to log⁡2(n)\work{\log_2(n)} columns and drops the total work to Θ(nlog⁡n)\Theta(n \log n).

Stopping the inner loop at i cuts the square of steps in half, while doubling j on each turn shrinks it to a thin strip.
3

Loops That Stop Early: Best Case and Worst Case

Every loop so far ran all the way to its final limit, so the step count depended only on nn. Now we want to count the array comparisons in Linear Search (`if A[i] == target: return true`), where the loop can stop early depending on what is inside the array AA.

Loops That Stop Early: Best Case and Worst Case
Linear Search\htmlClass{lc-mute}{\text{Linear Search}}
target = 7\htmlClass{lc-blue}{\texttt{target = 7}}
function contains(A, n, target):\texttt{function }\htmlClass{lc-violet}{\texttt{contains}}\texttt{(A, n, }\htmlClass{lc-blue}{\texttt{target}}\texttt{):}
for i = 1 to n:\texttt{for i = 1 to n:}
if A[i] == target:\texttt{if A[i] == }\htmlClass{lc-blue}{\texttt{target}}\texttt{:}
return true\texttt{return true}
return false\texttt{return false}
comparisons: ?\htmlClass{lc-green}{\text{comparisons:}}\ \htmlClass{lc-orange}{?}
AA
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
n\htmlClass{lc-mute}{n}
Text solution
Find the best caseWhen `target` sits in the very first slot A[1]A[1], the first comparison succeeds and the function returns immediately.
Best case\displaystyle \text{Best case} =1 comparison\displaystyle = \work{1 \text{ comparison}} =Θ(1)\displaystyle = \Theta(1)
Find the worst caseWhen `target` sits in the last slot A[n]A[n] or is not in AA at all, the loop checks every slot from A[1]A[1] through A[n]A[n] before finishing.
Worst case=n comparisons=Θ(n)\displaystyle \begin{aligned} \text{Worst case} &= \work{n \text{ comparisons}} \\ &= \Theta(n) \end{aligned}
Bound the algorithm across all inputs of size nnBecause any input takes at least 1\work{1} comparison and at most n\work{n} comparisons, the best case sets the lower bound Ω\Omega and the worst case sets the upper bound OO.
All inputs: Ω(1)⏟at least 1andO(n)⏟at most n\displaystyle \text{All inputs: } \underbrace{\Omega(1)}_{\text{at least } \work{1}} \quad \text{and} \quad \underbrace{O(n)}_{\text{at most } \work{n}}

In Linear Search on A[1..n]A[1..n], the number of comparisons equals the slot position k\work{k} where `target` first appears: from 1\work{1} comparison when `target` is in A[1]A[1] up to all n\work{n} comparisons when `target` is in A[n]A[n] or missing from the array.

Even on the same array, Linear Search can stop after one check or check every slot, depending on where the item you want sits.
4

Recursive Chains: Subtracting vs. Halving

When a function calls itself instead of running a loop, we want to find the total steps T(n)T(n) by adding the local work inside one call to the work done by its smaller recursive calls, down to the base case at n=1n = 1.

nn
1 step\htmlClass{lc-green}{1 \text{ step}}
n−1n - 1
1 step\htmlClass{lc-green}{1 \text{ step}}
n−2n - 2
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
11
1 step\htmlClass{lc-green}{1 \text{ step}}
n boxes\htmlClass{lc-violet}{n} \text{ boxes}
nn
1 step\htmlClass{lc-green}{1 \text{ step}}
n2\tfrac{n}{2}
1 step\htmlClass{lc-green}{1 \text{ step}}
n4\tfrac{n}{4}
1 step\htmlClass{lc-green}{1 \text{ step}}
1 step\htmlClass{lc-green}{1 \text{ step}}
11
1 step\htmlClass{lc-green}{1 \text{ step}}
log⁡2(n)+1 boxes\htmlClass{lc-violet}{\log_2(n) + 1} \text{ boxes}
Recursive Chains: Subtracting vs. Halving
nn
local work\htmlClass{lc-green}{\text{local work}}
Text solution
Set up the recurrence for a single callA function that does 1\work{1} step of local work and calls itself once has total steps equal to the smaller call's steps plus 1\work{1}.
T(n)=T(smaller size)⏟recursive call+1⏟local work\displaystyle \begin{aligned} T(n) &= \underbrace{\calls{T(\text{smaller size})}}_{\calls{\text{recursive call}}} + \underbrace{\work{1}}_{\work{\text{local work}}} \end{aligned}
Count the chain when subtracting 11In T(n)=T(n−1)+1T(n) = \calls{T(n - 1)} + \work{1}, the input size steps down through n,n−1,n−2,…,1n, n - 1, n - 2, \dots, 1, creating a single chain of n\calls{n} boxes doing 1\work{1} step each.
T(n)\displaystyle T(n) =n×1\displaystyle = \calls{n} \times \work{1} =n\displaystyle = n =Θ(n)\displaystyle = \Theta(n)
Count the chain when halving nnIn T(n)=T(n2)+1T(n) = \calls{T(\tfrac{n}{2})} + \work{1}, cutting nn in half takes log⁡2(n)\log_2(n) steps to reach 11, creating a chain of log⁡2(n)+1\calls{\log_2(n) + 1} boxes doing 1\work{1} step each.
T(n)=(log⁡2(n)+1)×1=Θ(log⁡n)\displaystyle \begin{aligned} T(n) &= (\calls{\log_2(n) + 1}) \times \work{1} \\ &= \Theta(\log n) \end{aligned}
5

Divide-and-Conquer Recursion Trees

Next, we want to find the total steps T(n)T(n) when a function makes two recursive calls on half the input size (n2\tfrac{n}{2}), which branches the calls into a binary recursion tree. When each call does 1\work{1} step of local work, the row totals double at each level and we use the geometric sum of powers of 22:

1+2+4+8+⋯+n\displaystyle \work{1} + \work{2} + \work{4} + \work{8} + \dots + \work{n} =2n−1\displaystyle = 2n - 1
Divide-and-Conquer Recursion Trees
Merge Sort\text{Merge Sort}
11
22
55
77
33
44
66
88
n2\dfrac{n}{2}
n2\dfrac{n}{2}
Text solution
Add the local work across each row for Merge SortIn T(n)=2T(n2)+nT(n) = \calls{2T(\tfrac{n}{2})} + \work{n}, Row rr has 2r\calls{2^r} calls that each do n2r\work{\frac{n}{2^r}} local merging work, so the doubling calls and halving sizes cancel on every row.
Row r work\displaystyle \text{Row } r \text{ work} =2r×n2r\displaystyle = \calls{2^r} \times \work{\frac{n}{2^r}} =n\displaystyle = \work{n}
Multiply work per row by the number of rowsHalving nn down to 11 produces log⁡2(n)+1\log_2(n) + 1 rows of n\work{n} work each.
T(n)=n(log⁡2(n)+1)=Θ(nlog⁡n)\displaystyle \begin{aligned} T(n) &= \work{n}(\log_2(n) + 1) \\ &= \Theta(n \log n) \end{aligned}
Add the doubling rows when local work is 1\work{1}In T(n)=2T(n2)+1T(n) = \calls{2T(\tfrac{n}{2})} + \work{1}, Row rr does 2r×1=2r\calls{2^r} \times \work{1} = \work{2^r} work down to 2log⁡2(n)=n\calls{2^{\log_2(n)}} = \calls{n} calls on the bottom row, so the whole tree adds up to roughly twice the bottom row.
T(n)=1+2+4+⋯+n=2n−1=Θ(n)\displaystyle \begin{aligned} T(n) &= \work{1} + \work{2} + \work{4} + \dots + \work{n} \\ &= 2n - 1 = \Theta(n) \end{aligned}

In a binary recursion tree that halves nn at each level, doing local work equal to the subproblem size makes the bars on every row add up to the same width n\work{n}, whereas doing 1\work{1} step of local work per call makes each row's total double from 1\work{1} at the top down to n\work{n} on the bottom row.

When a recursion tree branches in two and halves the input at each level, doing work equal to each call's size makes every row add up to the full input size, while doing one step per call makes the row totals double until the bottom row holds roughly half the work.
6

Exponential Recursion Trees

Finally, we want to find the total moves T(n)T(n) when a function still makes two recursive calls per turn, as in the Towers of Hanoi puzzle, but only subtracts 11 from nn at each level instead of dividing nn by 22.

T(n)=2T(n2)+1T(n) = \htmlClass{lc-violet}{2T(\tfrac{n}{2})} + \htmlClass{lc-green}{1}
nn
n2\tfrac{n}{2}
n4\tfrac{n}{4}
11
n calls\htmlClass{lc-violet}{n} \text{ calls}
log⁡2(n)+1 levels\log_2(n) + 1 \text{ levels}
T(n)=2T(n−1)+1T(n) = \htmlClass{lc-violet}{2T(n - 1)} + \htmlClass{lc-green}{1}
nn
n−1n - 1
n−2n - 2
n−3n - 3
11
⋮\vdots
2n−1 calls\htmlClass{lc-violet}{2^{n-1}} \text{ calls}
n levelsn \text{ levels}
Exponential Recursion Trees and Shape Summary
Text solution
Write the recurrence for moving nn disksMoving nn disks requires moving the top n−1n - 1 disks twice and moving the largest disk once.
T(n)\displaystyle T(n) =2T(n−1)+1\displaystyle = \calls{2T(n - 1)} + \work{1}
Count the work on each row across nn levelsBecause each call makes 2\calls{2} calls of size n−1n - 1 doing 1\work{1} step each, the row totals double (1,2,4,8,…\work{1}, \work{2}, \work{4}, \work{8}, \dots) across all nn levels from size nn down to size 11.
Row r work=2rfor r=0,1,…,n−1\displaystyle \begin{aligned} \text{Row } r \text{ work} &= \work{2^r} \\ \text{for } r &= 0, 1, \dots, n - 1 \end{aligned}
Sum the doubling rows over all nn levelsAdding the powers of 22 from Row 00 through Row n−1n - 1 gives an exponential step count.
T(n)=1+2+4+⋯+2n−1=2n−1=Θ(2n)\displaystyle \begin{aligned} T(n) &= \work{1} + \work{2} + \work{4} + \dots + \work{2^{n - 1}} \\ &= 2^n - 1 = \Theta(2^n) \end{aligned}

You can spot Θ(n2)\Theta(n^2) for a triangular loop or Θ(nlog⁡n)\Theta(n \log n) for T(n)=2T(n2)+nT(n) = \calls{2T(\tfrac{n}{2})} + \work{n} right away from its shape, but when a question asks for the exact number of steps, write out the row sum first before dropping constants and lower-order terms.

Key concepts

Before writing a complexity bound, check whether the code can stop early depending on the contents of the input (such as `if A[i] == target: return true`). When a loop can exit early, a single formula in nn cannot describe every input: you bound the best case (Θ(1)\Theta(1) when `target` is in A[1]A[1]) and the worst case (Θ(n)\Theta(n) when `target` is in A[n]A[n] or missing) separately, which makes the algorithm Ω(1)\Omega(1) and O(n)O(n) across all inputs of size nn. Only when code always runs to its final limit do the minimum and maximum steps match, giving a single tight Θ\Theta bound for all inputs of size nn.

For loops that run to completion, the shape of the dots gives the step count:

Key concept

In a single loop, adding or subtracting a constant (`for i = 1 to n (stepping by +2)`) steps through a line of n2=Θ(n)\work{\frac{n}{2}} = \Theta(n) dots, while multiplying or dividing by a constant (`i = i * 2` while `i < n`) reaches nn in log⁡2(n)=Θ(log⁡n)\work{\log_2(n)} = \Theta(\log n) steps.

Key concept

In nested loops, multiply the outer count by the inner count whenever every row has the same width: an nn by nn square (`j = 1 to n`) takes n2=Θ(n2)n^2 = \Theta(n^2) steps, and an nn by log⁡2(n)\work{\log_2(n)} rectangle (`j = j * 2`) takes nlog⁡2(n)=Θ(nlog⁡n)n \work{\log_2(n)} = \Theta(n \log n) steps. Do not multiply rows by columns when the inner bound depends on the outer counter (`for j = 1 to i`). Instead, sum the rows as a right triangle:

1+2+3+⋯+n=n(n+1)2=Θ(n2)\displaystyle \begin{aligned} \work{1} + \work{2} + \work{3} + \dots + \work{n} &= \frac{n(n + 1)}{2} \\ &= \Theta(n^2) \end{aligned}

For recursive algorithms, the number of recursive calls per turn sets the branching and the input reduction sets the height:

Key concept

A single recursive call per turn forms a vertical chain: subtracting 11 (`T(n) = T(n - 1) + 1`) stacks n\calls{n} boxes for Θ(n)\Theta(n), while halving (`T(n) = T(n/2) + 1`) stacks log⁡2(n)+1\calls{\log_2(n) + 1} boxes for Θ(log⁡n)\Theta(\log n).

Key concept

Two recursive calls per turn form a binary tree, which we sum row by row. Halving the input (`2T(n/2)`) gives log⁡2(n)+1\log_2(n) + 1 levels: when each call does local work equal to its subproblem size (`T(n) = 2T(n/2) + n`), doubling calls and halving sizes cancel so every row totals n\work{n}, giving n(log⁡2(n)+1)=Θ(nlog⁡n)\work{n}(\log_2(n) + 1) = \Theta(n \log n); when each call does 1\work{1} step (`T(n) = 2T(n/2) + 1`), don't multiply by the height, because the rows double down to n\work{n} on the bottom row:

1+2+4+⋯+n=2n−1=Θ(n)\displaystyle \begin{aligned} \work{1} + \work{2} + \work{4} + \dots + \work{n} &= 2n - 1 \\ &= \Theta(n) \end{aligned}

Key concept

Subtracting 11 across two recursive calls (`T(n) = 2T(n - 1) + 1`) doubles the row work across all nn levels, growing to 1+2+4+⋯+2n−1\work{1} + \work{2} + \work{4} + \dots + \work{2^{n-1}} == 2n−12^n - 1 == Θ(2n)\Theta(2^n).

7

Practice

1

Suppose n=2k\given{n = 2^k} for a positive integer kk. Find the exact number of steps performed by the following loop and give a tight Big Theta bound: ```text i = n while i > 1: do 1 step of work i = i / 2 ```

Write the value of i\counter{i} in terms of kk after 1,2,…1, 2, \dots turns. After how many halvings does i\counter{i} reach 11?

Starting at i=n=2k\counter{i} = \given{n = 2^k}, each turn performs 1\work{1} step of work and cuts i\counter{i} in half: after 1\work{1} step i=2k−1\counter{i} = \counter{2^{k-1}}, after 2\work{2} steps i=2k−2\counter{i} = \counter{2^{k-2}}, and after k\work{k} steps i=20=1\counter{i} = \counter{2^0} = \counter{1}. Watch out for the strict loop condition `i > 1`: the loop stops the moment i\counter{i} hits 1\counter{1} rather than running one extra turn for i=1\counter{i = 1} (which is the slip that gives log⁡2(n)+1\fails{\log_2(n) + 1}). Thus the loop performs k=log⁡2(n)\work{k} = \work{\log_2(n)} steps, which is Θ(log⁡n)\Theta(\log n).
2

Suppose n=2k\given{n = 2^k} with k≥1\given{k \ge 1}. Find the exact number of steps performed by these nested loops and give a tight Big Theta bound: ```text i = 1 while i < n: for j = 1 to n (stepping by +2): do 1 step of work i = i * 2 ```

Does the inner loop's limit depend on i\counter{i}, or does every turn of the outer loop perform the exact same number of inner steps?

The outer loop doubles i\counter{i} through 1,2,4,…,2k−1=n2\counter{1}, \counter{2}, \counter{4}, \dots, \counter{2^{k-1}} = \counter{\tfrac{n}{2}}, stopping as soon as i\counter{i} reaches 2k=n\counter{2^k} = n, so it runs k=log⁡2(n)k = \log_2(n) times. On each turn of the outer loop, the inner loop steps j\counter{j} by +2\counter{+2} through the odd numbers 1,3,5,…,n−1\counter{1}, \counter{3}, \counter{5}, \dots, \counter{n - 1}. Be careful not to confuse `stepping by +2` with multiplying by 22 (which would wrongly give log⁡2(n)\fails{\log_2(n)} inner steps and (log⁡2n)2\fails{(\log_2 n)^2} overall) or forget that skipping every even number cuts the row width from n\work{n} to n2\work{\tfrac{n}{2}} (which would give nlog⁡2(n)\fails{n\log_2(n)} as the exact count). Multiplying the log⁡2(n)\log_2(n) rows by n2\work{\tfrac{n}{2}} dots per row gives n2log⁡2(n)\work{\frac{n}{2}}\log_2(n) steps, which is Θ(nlog⁡n)\Theta(n \log n).
3

Find the exact number of steps as a function of nn and give a tight Big Theta bound for the following nested loop: ```text for i = 1 to n: for j = i to n: do 1 step of work ```

How many values of j\counter{j} run from i\counter{i} to nn on row i=1\counter{i = 1}, row i=2\counter{i = 2}, and down to row i=n\counter{i = n}?

When i=1\counter{i = 1}, j\counter{j} runs from 1\counter{1} to n\counter{n} (n\work{n} steps). When i=2\counter{i = 2}, j\counter{j} runs from 2\counter{2} to n\counter{n} (n−1\work{n - 1} steps). This continues down to i=n\counter{i = n}, where j\counter{j} runs from n\counter{n} to n\counter{n} (1\work{1} step). Don't subtract the endpoints as n−i\fails{n - i} without adding 11 for the starting value j=i\counter{j = i}, a slip that drops 11 dot from every row and gives n(n−1)2\fails{\frac{n(n - 1)}{2}}. Adding the rows from nn down to 11 gives
n+(n−1)+(n−2)+⋯+1=n(n+1)2=12n2+12n steps,\displaystyle \begin{aligned} & \work{n} + \work{(n - 1)} + \work{(n - 2)} + \dots + \work{1} \\ &= \frac{n(n + 1)}{2} \\ &= \work{\frac{1}{2}n^2} + \leftover{\frac{1}{2}n} \text{ steps}, \end{aligned}
which is Θ(n2)\Theta(n^2).
4

The following function checks whether an array A[1..n]A[1..n] (with n≥2\given{n \ge 2}) is sorted in non-decreasing order: ```text function isSorted(A, n): for i = 1 to n - 1: if A[i] > A[i + 1]: return false return true ``` Using the array comparison `A[i] > A[i + 1]` as the step count, describe a best-case input and a worst-case input, give the exact number of comparisons and tight Big Theta bound for each case, and give the Big Omega and Big O bounds that hold across all inputs of size nn.

Which pair of elements makes `return false` trigger on i=1\counter{i = 1}, and what kind of array forces the loop to check every adjacent pair?

Don't assume a sorted array is the best case just because the function checks for sorting: a sorted array never triggers `return false`, so it forces the loop to run all the way through! Instead, the best case occurs when the very first pair is out of order (A[1]>A[2]\fails{A[1] > A[2]}), where the loop returns `false` on i=1\counter{i = 1} after 1\work{1} comparison, which is Θ(1)\Theta(1). The worst case occurs when the array is completely sorted in non-decreasing order (or only the final pair has A[n−1]>A[n]\fails{A[n - 1] > A[n]}); watching the upper bound `n - 1` so you don't write n\fails{n} comparisons, the loop checks every adjacent pair from i=1\counter{i = 1} to n−1\counter{n - 1} for n−1\work{n - 1} comparisons, which is Θ(n)\Theta(n). Across all inputs of size nn, the algorithm is Ω(1)\Omega(1) and O(n)O(n).
5

Suppose n=2k\given{n = 2^k} for a non-negative integer kk. Consider the following recursive function: ```text function process(n): if n == 1: do 1 step of work return process(n / 2) process(n / 2) for i = 1 to n / 2: do 1 step of work ``` Write a recurrence for the exact number of steps T(n)T(n), find the total work on each row of the recursion tree, and determine both the exact total step count and a tight Big Theta bound.

How many recursive calls and how many loop steps happen when n>1n > 1, and what happens at the bottom row when n=1n = 1?

For n>1n > 1, the function makes 2\calls{2} recursive calls of size n2\tfrac{n}{2} and performs n2\work{\tfrac{n}{2}} steps in its loop, giving the recurrence
T(n)=2T(n2)+n2with T(1)=1.\displaystyle \begin{aligned} T(n) &= \calls{2T\left(\frac{n}{2}\right)} + \work{\frac{n}{2}} \\ &\quad \text{with } T(1) = \work{1}. \end{aligned}
In the recursion tree, Row 00 has 1\calls{1} call of size nn doing n2\work{\tfrac{n}{2}} steps, Row 11 has 2\calls{2} calls of size n2\tfrac{n}{2} each doing n4\work{\tfrac{n}{4}} steps for 2(n4)=n2\calls{2}(\work{\tfrac{n}{4}}) = \work{\tfrac{n}{2}} steps, and each internal row rr (for 0≤r<log⁡2(n)0 \le r < \log_2(n)) has 2r\calls{2^r} calls of size n2r\tfrac{n}{2^r} each doing n2r+1\work{\tfrac{n}{2^{r+1}}} steps, totaling n2\work{\tfrac{n}{2}} steps per row across the log⁡2(n)\log_2(n) internal rows. At the bottom row r=log⁡2(n)r = \log_2(n), don't apply the internal-row formula n2\work{\tfrac{n}{2}} (which would give n2(log⁡2(n)+1)\fails{\frac{n}{2}(\log_2(n) + 1)}): the 2log⁡2(n)=n\calls{2^{\log_2(n)}} = \calls{n} calls at the bottom have size 11 and hit the `if n == 1` base case, doing 1\work{1} step each for n\work{n} steps. Summing all rows gives n2log⁡2(n)+n\work{\frac{n}{2}}\log_2(n) + \work{n} steps, which is Θ(nlog⁡n)\Theta(n \log n).
6

Compare two recursive functions that both return 2n−12^{n-1} for an integer n≥1\given{n \ge 1}, where each function call counts as 11 step of local work: ```text function calcA(n): if n == 1: return 1 return calcA(n - 1) + calcA(n - 1)

function calcB(n): if n == 1: return 1 x = calcB(n - 1) return x + x ``` For each function, write the recurrence for the total steps, find the exact number of steps as a function of nn, and give its tight Big Theta bound.

Count how many times each function actually calls itself on n−1n - 1 before returning.

In `calcA`, each call with n>1n > 1 makes two separate recursive calls of size n−1n - 1, giving TA(n)=2TA(n−1)+1T_A(n) = \calls{2T_A(n - 1)} + \work{1} with TA(1)=1T_A(1) = \work{1}. Its binary tree has nn levels (from size nn down to 11) whose row totals double each level (1,2,4,…,2n−1\work{1}, \work{2}, \work{4}, \dots, \work{2^{n-1}}); be careful to include all nn levels, because stopping one row early gives only 2n−1−1\fails{2^{n-1} - 1}. Adding all nn rows gives 1+2+4+⋯+2n−1=2n−1\work{1} + \work{2} + \work{4} + \dots + \work{2^{n-1}} = 2^n - 1 steps, which is Θ(2n)\Theta(2^n). In `calcB`, don't write 2TB(n−1)+1\fails{2T_B(n - 1) + 1} just because `x` appears twice in `x + x`. Storing `x = calcB(n - 1)` calls the function only once and reuses the number, giving TB(n)=TB(n−1)+1T_B(n) = \calls{T_B(n - 1)} + \work{1} with TB(1)=1T_B(1) = \work{1}, a single chain of n\work{n} steps, which is Θ(n)\Theta(n).