<<

arXiv:1808.07957v2 [cs.DS] 15 Oct 2019 wyfo eo omly egv ipepoft h follow the to proof simple 1. a the Theorem give At we Formally, . the zero. of from away iteration recursive every with mass o hsnme eegvn[ given alg were the of iterations this (recursive) of for number the by dominated applications other and these on details more For others. RSA and the and inverses multiplicative computing coefficients, diviso common greatest th the is computes history algorithm Algorithm in the (See recorded BC, algorithm 300 ancient in most the Perhaps Introduction 1 ..[ e.g. loih requires algorithm appropriate define doe it occur: not simila does A phenomenon same analysi step. the the since each case , at univariate decreases of case highest-degree the in that Note [ in algorithm. easily the be of cannot nature complexity recursive runtime actual the simplicity, day.” present the algorit to all survived of has granddaddy that the algorithm is algorithm] Euclidean “[The anie sbsdo h so-called the on based is idea main u nlsssol ecnrse ihteaayi f[ of analysis the with contrasted be should analysis Our o factor a by shrinks argument larger the that shows analysis ags nee erae tec step. each at decreases integer largest sn lgtymr ohsiae oeta ucinyie 2. function potential Theorem sophisticated more slightly a Using r h ioac ubr:1 : Fibonacci the are Here ubr eosrtstetgtesof tightness the demonstrates numbers Therefore ∗ † eateto ES S iiin nvriyo ihgn A Michigan, of University , CSE EECS, of Department IM,Uiested otele,CR,Mnplir Fra Montpellier, CNRS, Montpellier, Universit´e de LIRMM, Lam44 steoeain nieec trto r eeetr” t “elementary”, are iteration each inside operations the As task many for subroutine a as serves Algorithm Euclidean The u e nlsspoie la,“n-ie pe on wh bound upper “one-line” clean, a provides analysis new Our Knu97 φ ulda loih.Wievr hr ipe h banduprboun upper obtained the simple, short very While Algorithm. Euclidean n mr)ln ntems nin loih nHistory in Algorithm Ancient most the on line (more) One = egv e ipeadsot(oeln” nlssfrternieof runtime the for analysis (“one-line”) short and simple new a give We 1+ ,te h ubro trto ihteso-called the with iteration of number the ties ], m , 2 √ CLRS09 / o any For 5 o any For log ≅ 1 1 φ o h ecito fteagrtm)Tefloigi quo a is following The algorithm.) the of description the for potential m . ( 10387 eoe the denotes 61803398875 φx ) e,tepofo hscamrqie oehtnon-triv somewhat a requires claim this of proof the Yet, ]). trtos hni utb h aethat case the be must it then iterations, ,y x, ,y x, + y ∈ ∈ ,ipyn h piaiyof optimality the implying ), ucin n hwta hs ucin oeacntn frac constant a lose functions these that show and functions φx N N , rn Grenet Bruno Lam44 h ulda loih efrsa most at performs Algorithm Euclidean the , 1 h ulda loih efrsa most at performs Algorithm Euclidean the , + , 2 , y 3 ≥ , , 5 oeta Method Potential . . . , φF Sed83 this m n,ide,selecting indeed, And, . +2 , ∗ Abstract nlss o o eea pair general a for Now analysis. + odnratio golden Knu97 F 1 m +1 , ≥ laVolkovich Ilya Knu98 c.Email: nce. seeg [ e.g. (see F Lam44 our . nAbr I Email: MI. Arbor, nn m d nee ihe result. tighter even an lds loih,CieeRmidrTheorem Chinese algorithm, +3 nerdfo t ecito u othe to due description its from inferred m,bcuei steods nontrivial oldest the is it because hms, rtm ned aiu pe bounds upper various Indeed, orithm. , aetm,tefntosaebounded are functions the time, same e [ see , Fibonacci smc ipe:tedge fthe of degree the simpler: much is s analysis. ayagmn a akn nthe in lacking was argument easy r ' tlat2every 2 least at f x CLRS09 n theorems: ing hc ttsta fteEuclidean the if that states which ] ulda loih.Published Algorithm. Euclidean e s ≥ φ [email protected] eata utm opeiyis complexity runtime actual he CLRS09 not m x gd ftoitgrnumbers. integer two of (gcd) r c un u ob pia.The optimal. be to out turns ich F Knu97 +1 and m † +2 . uha:fidn B´ezout’s finding as: such s odta h i-ieo the of bit-size the that hold .Tefis n,presented one, first The ]. ubr.Aohrkn of kind Another numbers. y nnear-optimal. in d and , log ) oeseicly we specifically, More ]). scneuieFibonacci consecutive as Knu98 log 1 [email protected] y . 5 x φ ( h well-known the ≥ ( x and two φx + .Yt ept its despite Yet, ]. F efo [ from te a case-analysis. ial y m + )+1 y +1 trtos(see iterations y . ehave: we ) where , ino their of tion iterations. iterations. Knu98 . F i -s ]: 2 The Algorithm and the Runtime Analysis

In this section we review the algorithm and give the new, simplified runtime analysis. The descrip- tion of the algorithm is given below in Algorithm 1. The algorithm is given non-negative , x and y as an input. We assume w.l.o.g that x>y.

Input: Two non-negative integers, x>y ∈ N Output: gcd(x,y)

1 if y = 0 then return x; 2 if y = 1 then return 1; 3 return gcd(y,x mod y) Algorithm 1: Euclidean Algorithm For the sake of analysis, let us fix x>y and let m denote the number of the (recursive) iterations of algorithm.

Definition 2.1. For 1 ≤ i ≤ m, let xi and yi denote the values of x and y in the i-th iteration of the algorithm, respectively. In particular, x1 = x,y1 = y.

Observation 2.2. For 1 ≤ i ≤ m: xi >yi ≥ 0.

Proof. Follows from the fact that yi = xi 1 mod yi 1 = xi 1 mod xi, which attains values between − − − 0 and xi − 1. ∆ Definition 2.3. For 1 ≤ i ≤ m, we define si = xi + yi.

Next, we will show that si is a potential function for this algorithm. In particular, the function loses a constant of its mass in every step, yet it is bounded away from zero. The following is immediate from the definition, given the above observation.

Corollary 2.4. For 1 ≤ i ≤ m: si ≥ 1. The following lemma is the heart of the argument.

Lemma 2.5. For 1 ≤ i ≤ m: si ≤ 2/3 · si 1. − Proof. Recall that si 1 = xi 1 + yi 1. Let us divide xi 1 by yi 1 with a reminder. In particular, − − − − −

xi 1 = qi 1 · yi 1 + ri 1, where 0 ≤ ri 1 ≤ yi 1 − 1. − − − − − −

Observe that si = yi 1 + ri 1 and in addition, qi 1 ≥ 1, as xi 1 >yi 1. We obtain the following: − − − − − si 1 = (qi 1+1)yi 1 +ri 1 ≥ 2yi 1+ri 1 ≥ 2yi 1+ri 1−(yi 1−ri 1)/2 = 1.5yi 1 +1.5ri 1 = 1.5si. − − − − − − − − − − − −

The second inequality holds since ri 1

i 1 Corollary 2.6. For 1 ≤ i ≤ m: si ≤ s1 · (2/3) − . Theorem 1 follows immediately from the above.

Proof of Theorem 1. By Corollaries 2.4 and 2.6:

m 1 m 1 1 ≤ sm ≤ s1 · (2/3) − = (x + y) · (2/3) − .

Therefore, m ≤ log1.5(x + y) + 1.

2 2.1 Improved Analysis In this section we show that using a slightly more sophisticated potential function yields an even tighter result. As before, for the sake of analysis, let us fix x>y and let m denote the number 1+√5 ≅ of the (recursive) iterations of algorithm. In addition, let φ = 2 1.61803398875 denote the .

∆ 1 Definition 2.7. For 1 ≤ i ≤ m, we define si = xi + φ · yi.

As before, for 1 ≤ i ≤ m: si ≥ 1. The following lemma mirrors Lemma 2.5. 1 Lemma 2.8. For 1 ≤ i ≤ m: si ≤ · si 1. φ − 1 Proof. Recall that si 1 = xi 1 + · yi 1. Let us divide xi 1 by yi 1 with a reminder. In particular, − − φ − − − xi 1 = qi 1 · yi 1 + ri 1, where 0 ≤ ri 1 ≤ yi 1 − 1. − − − − − − 1 Observe that si = yi 1 + ·ri 1 and in addition, qi 1 ≥ 1, as xi 1 >yi 1. We obtain the following: − φ − − − − 1 1 1 si 1 = qi 1 + yi 1 +ri 1 ≥ 1+ yi 1 +ri 1 = φ·yi 1 +ri 1 = φ yi 1 + · ri 1 = φ·si. −  − φ − −  φ − − − −  − φ −  1 Recall that 1 + φ = φ. The proof of Theorem 2 follows by repeating the previous argument.

3 Conclusion & Open Questions

In this short note we add one (more) line of analysis to the well-known Euclidean Algorithm. The new analysis does not require any background and can be taught even in an introductory-level undergraduate class. It would be nice to see if we could replace other kind of analyses that rely on Fibonacci numbers by a potential argument. One such example is the analysis of the height of an AVL-Tree [AVL62, Sed83].

References

[AVL62] G. Adelson-Velsky and E. Landis. An algorithm for the organization of information. In Proceedings of the USSR Academy of Sciences (in Russian), volume 146, pages 263–266, 1962. [CLRS09] T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein. Introduction to , 3rd Edition. MIT Press, 2009. [Knu97] D. E. Knuth. The art of , Volume I: Fundamental Algorithms, 3rd Edition. Addison-Wesley, 1997. [Knu98] D. E. Knuth. The art of computer programming, Volume II: Seminumerical Algorithms, 3rd Edition. Addison-Wesley, 1998. [Lam44] G. Lam´e. Note sur la limite du nombre des divisions dans la recherche du plus grand commun diviseur entre deux nombres entiers. Comptes Rendus Acad. Sci., (19):867–870, 1844. [Sed83] R. Sedgewick. Algorithms. Addison-Wesley, 1983.

3