MCS-211 · Block 1 Unit 3

Complexity Analysis of Simple Algorithms

Section 8 of 8

Block 1 · Unit 3Complexity Analysis of Simple Algorithms6 min · Section 8 of 8

3.8Euclid's Algorithm and Recursive Constructs

Covers IGNOU MCS-211 §programme guide, Unit 3 (Euclid Algorithm for GCD; Recursive Constructs)

The programme guide lists two more topics for this unit — the analysis of Euclid’s GCD algorithm and of recursive constructs — which the printed unit does not cover. This page fills that gap; both ideas are used heavily in Unit 4 and Block 2.

How fast is Euclid’s algorithm?

Recall from Unit 1: while b ≠ 0: (a, b) ← (b, a mod b); return a. The basic operation is the mod (division) in each round.

Proof. If b≤a/2b \le a/2, then a mod b<b≤a/2a \bmod b < b \le a/2. If b>a/2b > a/2, then aa contains bb only once, so a mod b=a−b<a−a/2=a/2a \bmod b = a - b < a - a/2 = a/2. ■\blacksquare

After two rounds, (a,b)(a, b) becomes (a mod b,… )(a \bmod b, \dots) in the first position, so the first number at least halves every two rounds. A number can be halved at most about log⁡2b\log_2 b times before reaching 0, so Euclid’s algorithm needs at most about 2log⁡2b+12\log_2 b + 1 rounds: O(log⁡min⁡(a,b))O(\log \min(a, b)).

The slowest inputs are consecutive Fibonacci numbers (1,1,2,3,5,8,13,…1, 1, 2, 3, 5, 8, 13, \dots), because each quotient is 1 and the numbers shrink as slowly as possible (Lamé’s theorem).

Python · gcd_steps.py
import math

def gcd_rounds(a, b):
    rounds = 0
    while b != 0:
        a, b = b, a % b
        rounds += 1
    return rounds

# Consecutive Fibonacci numbers are the slowest inputs.
fib = [1, 1]
while len(fib) < 31:
    fib.append(fib[-1] + fib[-2])
for k in (10, 20, 30):
    a, b = fib[k], fib[k - 1]
    print(f"gcd(F{k + 1}, F{k}) = gcd({a}, {b}): {gcd_rounds(a, b)} rounds, "
          f"2*log2(b) = {2 * math.log2(b):.1f}")

# Check the bound rounds <= 2*log2(b) + 1 for every pair with 1 <= b <= a <= 2000.
worst = max(gcd_rounds(a, b) - 2 * math.log2(b) for a in range(1, 2001) for b in range(1, a + 1))
print("bound rounds <= 2 log2(b) + 1 holds for all pairs up to 2000:", worst <= 1)
Output
gcd(F11, F10) = gcd(89, 55): 9 rounds, 2*log2(b) = 11.6
gcd(F21, F20) = gcd(10946, 6765): 19 rounds, 2*log2(b) = 25.4
gcd(F31, F30) = gcd(1346269, 832040): 29 rounds, 2*log2(b) = 39.3
bound rounds <= 2 log2(b) + 1 holds for all pairs up to 2000: True

Even for numbers near a million, at most about 30 rounds are needed.

Analysing recursive constructs

A loop is analysed by counting iterations; a recursive procedure is analysed by writing a recurrence relation — an equation that expresses the running time on input size nn in terms of the running time on smaller inputs.

AlgorithmRecurrenceSolution
Recursive factorialT(n)=T(n−1)+cT(n) = T(n-1) + c, T(1)=cT(1) = cΘ(n)\Theta(n)
Recursive binary searchT(n)=T(n/2)+cT(n) = T(n/2) + c, T(1)=cT(1) = cΘ(log⁡n)\Theta(\log n)
Merge sortT(n)=2T(n/2)+cnT(n) = 2T(n/2) + cnΘ(nlog⁡n)\Theta(n \log n) (Unit 4)

The same unfolding on T(n)=T(n/2)+cT(n) = T(n/2) + c gives T(n)=T(n/2k)+kcT(n) = T(n/2^k) + kc, and the base case is reached when n/2k=1n/2^k = 1, i.e. k=log⁡2nk = \log_2 n: T(n)=c(log⁡2n+1)=Θ(log⁡n)T(n) = c(\log_2 n + 1) = \Theta(\log n). Unit 4 gives systematic methods (substitution, recursion tree, master method).

Python · recursive_counts.py
calls = 0

def factorial(n):
    global calls
    calls += 1
    return 1 if n <= 1 else n * factorial(n - 1)

def binary_search(a, key, lo, hi):
    global calls
    calls += 1
    if lo > hi:
        return -1
    mid = (lo + hi) // 2
    if a[mid] == key:
        return mid
    if key < a[mid]:
        return binary_search(a, key, lo, mid - 1)
    return binary_search(a, key, mid + 1, hi)

for n in (8, 64, 512):
    calls = 0
    factorial(n)
    f_calls = calls
    a = list(range(n))
    calls = 0
    binary_search(a, -1, 0, n - 1)      # key smaller than everything: worst case
    print(f"n = {n:4}: factorial calls = {f_calls:4}, binary search calls = {calls}")
Output
n =    8: factorial calls =    8, binary search calls = 4
n =   64: factorial calls =   64, binary search calls = 7
n =  512: factorial calls =  512, binary search calls = 10

Factorial calls grow linearly (nn); binary search calls grow like log⁡2n+1\log_2 n + 1 (4, 7, 10 for n=8,64,512n = 8, 64, 512).

Key takeaways

  • a mod b<a/2a \bmod b < a/2 for a≥b>0a \ge b > 0, so Euclid’s algorithm takes O(log⁡min⁡(a,b))O(\log \min(a, b)) rounds.
  • Consecutive Fibonacci numbers are Euclid’s worst inputs.
  • A recursive algorithm’s time satisfies a recurrence, e.g. T(n)=T(n−1)+cT(n) = T(n-1) + c or T(n)=T(n/2)+cT(n) = T(n/2) + c.
  • Unfolding gives Θ(n)\Theta(n) for factorial and Θ(log⁡n)\Theta(\log n) for binary search.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the time complexity of Euclid’s algorithm on inputs a≥b>0a \ge b > 0?

Show answer

Answer: C. O(log⁡b)O(\log b)

The remainder at least halves every two rounds, so about 2 log₂b rounds suffice. O(b) would be repeated subtraction of 1.

Question 2 · multiple choice

How many rounds does Euclid’s algorithm take for gcd⁡(13,8)\gcd(13, 8)?

Show answer

Answer: C. 5

(13, 8) → (8, 5) → (5, 3) → (3, 2) → (2, 1) → (1, 0): 5 rounds. Consecutive Fibonacci numbers take the most rounds for their size.

Question 3 · multiple choice

The recurrence T(n)=T(n−1)+cT(n) = T(n-1) + c, T(1)=cT(1) = c solves to:

Show answer

Answer: B. Θ(n)\Theta(n)

Unfolding gives T(n) = nc. Θ(log n) is the solution of T(n) = T(n/2) + c.

Question 4 · multiple choice

Which recurrence describes recursive binary search?

Show answer

Answer: D. T(n)=T(n/2)+cT(n) = T(n/2) + c

One recursive call on half the array plus constant work. 2T(n/2) would search both halves.

Question 5 · multiple choice

If a=100a = 100 and b=70b = 70, what is a mod ba \bmod b, and is it less than a/2a/2?

Show answer

Answer: B. 30, yes

100 = 1 × 70 + 30, and 30 < 50 = a/2, as the theorem promises.

Question 6 · short answer

Solve T(n)=T(n/2)+3T(n) = T(n/2) + 3 with T(1)=3T(1) = 3 for nn a power of 2, by unfolding.

Show worked answer

T(n) = T(n/2) + 3 = T(n/4) + 6 = … = T(n/2ᵏ) + 3k. The base case n/2ᵏ = 1 gives k = log₂n, so T(n) = T(1) + 3 log₂n = 3 log₂n + 3 = Θ(log n). Check n = 8: T(8) = T(4) + 3 = T(2) + 6 = T(1) + 9 = 12 = 3·3 + 3.

↑ ↓ move↵ openTab switch type