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/2, then amodb<b≤a/2. If b>a/2, then a contains b only once, so amodb=a−b<a−a/2=a/2. ■
After two rounds, (a,b) becomes (amodb,…) in the first position, so the first number at least halves every two rounds. A number can be halved at most about log2b times before reaching 0, so Euclid’s algorithm needs at most about 2log2b+1 rounds: O(logmin(a,b)).
The slowest inputs are consecutive Fibonacci numbers (1,1,2,3,5,8,13,…), because each quotient is 1 and the numbers shrink as slowly as possible (Lamé’s theorem).
Python · gcd_steps.py
import mathdef 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)
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 n in terms of the running time on smaller inputs.
Algorithm
Recurrence
Solution
Recursive factorial
T(n)=T(n−1)+c, T(1)=c
Θ(n)
Recursive binary search
T(n)=T(n/2)+c, T(1)=c
Θ(logn)
Merge sort
T(n)=2T(n/2)+cn
Θ(nlogn) (Unit 4)
The same unfolding on T(n)=T(n/2)+c gives T(n)=T(n/2k)+kc, and the base case is reached when n/2k=1, i.e. k=log2n: T(n)=c(log2n+1)=Θ(logn). Unit 4 gives systematic methods (substitution, recursion tree, master method).
Python · recursive_counts.py
calls = 0def 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}")
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/complexity-analysis-of-simple-algorithms/euclid-s-algorithm-and-recursive-constructs/ to read this page.