2082 4

CSC325 · 2082 · 5 marks

Solve the recurrence relation T(n) = 2T(n/2) + n using recursion tree method.

Solve the recurrence relation T(n) = 2T(n/2) + n using recursion tree method. [5]

Design and Analysis of AlgorithmsChapter 1: Foundation of Algorithm AnalysisSolving RecurrencesNumerical: Recursion tree method

Solving $T(n) = 2T(n/2) + n$ Using the Recursion Tree Method

Given

  • Recurrence: $T(n) = 2T\left(\dfrac{n}{2}\right) + n$
  • Branching factor: $a = 2$ (each node makes 2 recursive calls)
  • Subproblem shrink factor: $b = 2$ (size divided by 2)
  • Cost at a node of size $m$: $m$ (the "$+n$" term)
  • Base case: $T(1) = \Theta(1)$

Solution

Building the recursion tree

Level 0:            n                         cost = n
                  /   \
Level 1:       n/2     n/2                    cost = n/2 + n/2 = n
              /  \     /  \
Level 2:   n/4  n/4  n/4  n/4                 cost = 4·(n/4) = n
             ...        ...
Level k:  2^k nodes each of size n/2^k        cost = 2^k·(n/2^k) = n
             ...
Level log n: n leaves each of size 1          cost = n

Cost per level

Level $i$Number of nodesSize per nodeCost per level
0$1$$n$$n$
1$2$$n/2$$n$
2$4$$n/4$$n$
$i$$2^i$$n/2^i$$2^i \cdot \dfrac{n}{2^i} = n$
$\log_2 n$$n$$1$$n$

Key observation: Every level contributes exactly $n$.

Number of levels

The recursion stops when the subproblem size reaches 1:

$$\frac{n}{2^k} = 1 \implies 2^k = n \implies k = \log_2 n$$

Number of levels $= \log_2 n + 1$ (levels $0$ through $\log_2 n$).

Summing all levels

$$T(n) = \sum_{i=0}^{\log_2 n} n = n ,(\log_2 n + 1) = n\log_2 n + n$$

Final result

$$\boxed{T(n) = \Theta(n \log n)}$$

This matches the complexity of Merge Sort, which obeys the same recurrence.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.