Euclid's algorithm for finding greatest common divisor is an elegant algorithm that can be written iteratively as well as recursively. The time complexity of this algorithm is O(log^2 n) where n is the larger of the two inputs.
2. EUCLID’S ALGORITHM
(Quick history of this recently discovered algorithm)
Euclid's Algorithm appears as the solution to the Proposition
VII.2 in the Elements (written around 300 BC):
Given two numbers not prime to one another, to find their
greatest common measure.
What Euclid called "common measure" is termed nowadays
a common factor or a common divisor. Euclid VII.2 then
offers an algorithm for finding the greatest common
divisor(gcd) of two integers. Not surprisingly, the algorithm
bears Euclid's name.
Algorithms - Arora Euclid's Algorithm - GCD 2
3. EUCLID’S ALGORITHM (CONT. )
// Preconditions: n > m > 0
// Return an error if preconditions are not met.
// n%m is the remainder after dividing n with m.
Recursive version
gcd(n,m)
r = n%m
if r == 0 return m
// else
return gcd(m, r)
Iterative version
gcd(n,m)
r = n%m
while (r > 0) {
n = m
m = r
r = n%m
}
return m
Algorithms - Arora Euclid's Algorithm - GCD 3
4. EUCLID’S ALGORITHM (CONT. )
The algorithm is based on the following two observations:
1. If m divides n, then gcd(n, m) = n.
2. If n = mk + r, for integers k and r, then gcd(n, m) = gcd(m,
r). Indeed, every common divisor of n and m also divides
r. Thus gcd(n, m) divides r. But, of course, gcd(n, m)|m.
Therefore, gcd(n, m) is a common divisor of m and r and
hence gcd(n, m) ≤ gcd(m, r). The reverse is also true
because every divisor of m and r also divides n.
Algorithms - Arora Euclid's Algorithm - GCD 4
5. ANALYZING EUCLID’S ALGORITHM
What is the relationship between n, m and r?
There is no known relationship between n and m, other than the
precondition that n > m.
But we can claim something more interesting between n and r
r < m
Also, r n - m
r < n/2
Af ter 2 recursive cal ls (or two iterations of the loop) , the first
argument is no more than hal f .
This leads to the fol lowing recurrence relation: T(n) = T(n/2) +
2 (where the second 2 represents the 2 recursive cal ls or two
iterations of the loop. )
What is the time complexity for this recurrence relation?
Algorithms - Arora Euclid's Algorithm - GCD 5
6. ANALYZING EUCLID’S ALGORITHM
T(n) = T(n/2) + 2*(time to do modulo operation)
I f modulo operation is a constant time operation, then:
T(n) = T(n/2) + 2
T(n) = O(log n)
However, usually modulo operation is not real ly constant time,
but takes the same time as number of bits, bytes or decimal
digits (using long division)
In that case:
T(n) = T(n/2) + 2*log n
T(n) = O(log2n) // This is (log n)2, not log log n
Algorithms - Arora Euclid's Algorithm - GCD 6
7. LOG2(N): LOGARITHMIC OR QUADRATIC?
log(n) factor is a bit misleading.
This is because we are used to looking at n as the size of the
input. For example, given n numbers in sor ted order, binary
search can find the input in O( log n) time. In that case, n
represents the SIZE OF THE INPUT (2n or 4n bytes, etc)
However, i n c a s e o f E u c l id’ s a l g o rit hm, n i s t h e input value,
not the size of the input . This is fundamentally dif ferent.
Given value of n can be encoded in log n bits. (binary
encoding)
T h a t i s , t h e E u c l id’ s a l g o r ithm r u ns i n l o g2n time, when given
log n bits. That is, it runs in time that is quadratically related
to the input size.
Algorithms - Arora Euclid's Algorithm - GCD 7
8. ANALYZING EUCLID’S ALGORITHM (CONT. )
log(n) is the size of the input (propor tional to the number of
bits, bytes or decimal digits)
Gi ve n s b i t s , E u c l id’ s a l g o r ithm wi l l fi n i s h i n O( s2) time.
[When in confusion, always think of the size of the input as
your determining factor. ]
Algorithms - Arora Euclid's Algorithm - GCD 8
9. TIGHTNESS OF ANALYSIS
To be precise, we have only shown that our analysis of
E u c l id’ s a l g o r it hm i s l o g2n time. Let us revisit our analysis
Relationship between n and r
r < m
Also, r n - m
r < n/2
Is our analysis tight? In other words, are there some values of
n and m, such gcd(n,m) actually requires log n steps?
Looking for counter example:
n = 1000, m = 50. r=0. Only takes 1 step, not log 1000 steps.
n = 1000, m = 417. r=166. In next iteration, n = 417, m = 166, r =
85. In next iteration, n = 166, m = 85, r = 81. In next iteration, n =
85, m = 81, r = 4. In next iteration, n = 81, m = 4, r = 1. Only took 4
iterations.
Algorithms - Arora Euclid's Algorithm - GCD 9
10. TIGHTNESS OF ANALYSIS (CONT. )
Using induction, one can prove that if n and m are Fibonacci
numbers Fk+2 and Fk+1, E u c li d’ s a l g o ri thm t a kes k s te p s .
Proof :
n = Fk+2
m = Fk+1
r = Fk+2 – Fk+1 = Fk
Using induction hypothesis, steps(m,r) = k-1
We know that Fk is propor tional to k, where is the golden
ratio.
T h e r efo re, u s i ng two F i b o na cc i numb e r s n a nd m, E u c l id’ s
algorithm can take log (n) steps.
I t is also possible to prove that the two successive Fibonacci
n umb e r s p r e s e n t t h e wo r s t c a s e fo r t h e E u c l id’ s a l g o r ithm.
Algorithms - Arora Euclid's Algorithm - GCD 10
11. CONCLUSIONS
T h e a na l y si s o f E u c l id’ s a l g o r it hm i s t i g h t .
T h e r e fo re , i n t h e wo r s t c a s e , E u c l id’ s a l g o r ithm c a n i n d e e d
take O( log n) steps.
Modulo operation takes time propor tional to the number of
bits/bytes/decimal digits.
Gi ve n va l u e s n a n d m, tot a l t ime o f E u c l id’ s a l g o r it hm i s
bounded by O( log2n) .
Gi ve n s b i t s / by te s/ de c imal d i g it s, E u c l id ’ s a l g o r it hm t a ke s
O(s2) time.
Algorithms - Arora Euclid's Algorithm - GCD 11