SlideShare a Scribd company logo
1 of 12
EUCLID’S ALGORITHM 
FOR FINDING 
GREATEST COMMON DIVISOR
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
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
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
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
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
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
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
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
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
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
REFERENCES 
 http://www.standardwisdom.com/algorithms-book 
 http://en.wikipedia.org/wiki/Euclidean_algorithm 
 http://www.cut -the-knot.org/blue/Euclid.shtml 
 http://www.albany.edu/~csi503/pdfs/lect05.pdf 
Algorithms - Arora Euclid's Algorithm - GCD 12

More Related Content

What's hot (20)

GCD of n Numbers
GCD of n NumbersGCD of n Numbers
GCD of n Numbers
 
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.pptANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory
 
Graph theory and its applications
Graph theory and its applicationsGraph theory and its applications
Graph theory and its applications
 
Graph colouring
Graph colouringGraph colouring
Graph colouring
 

Viewers also liked

Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Amrinder Arora
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchAmrinder Arora
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part IIAmrinder Arora
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1Amrinder Arora
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
BTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsBTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsAmrinder Arora
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An IntroductionAmrinder Arora
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Amrinder Arora
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Amrinder Arora
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsAmrinder Arora
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresAmrinder Arora
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsAmrinder Arora
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine LearningAmrinder Arora
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 

Viewers also liked (20)

Algorithmic Puzzles
Algorithmic PuzzlesAlgorithmic Puzzles
Algorithmic Puzzles
 
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
 
NP completeness
NP completenessNP completeness
NP completeness
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
BTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsBTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTs
 
NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An Introduction
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data Structures
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine Learning
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 

Similar to Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis

2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiouvafopoulos
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CMeghaj Mallick
 
Assignment 2 (1) (1).docx
Assignment 2 (1) (1).docxAssignment 2 (1) (1).docx
Assignment 2 (1) (1).docxpinstechwork
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithmMuhammad Arish
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...2022cspaawan12556
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptxpallavidhade2
 
Large Semi Primes Factorization with Its Implications to RSA.pdf
Large Semi Primes Factorization with Its Implications to RSA.pdfLarge Semi Primes Factorization with Its Implications to RSA.pdf
Large Semi Primes Factorization with Its Implications to RSA.pdfDr. Richard Otieno
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 

Similar to Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis (20)

2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Notion of Algorithms.pdf
Notion of Algorithms.pdfNotion of Algorithms.pdf
Notion of Algorithms.pdf
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using C
 
Assignment 2 (1) (1).docx
Assignment 2 (1) (1).docxAssignment 2 (1) (1).docx
Assignment 2 (1) (1).docx
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithm
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Big o
Big oBig o
Big o
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Slide2
Slide2Slide2
Slide2
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Large Semi Primes Factorization with Its Implications to RSA.pdf
Large Semi Primes Factorization with Its Implications to RSA.pdfLarge Semi Primes Factorization with Its Implications to RSA.pdf
Large Semi Primes Factorization with Its Implications to RSA.pdf
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 

More from Amrinder Arora

Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Amrinder Arora
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaAmrinder Arora
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Amrinder Arora
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersAmrinder Arora
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresAmrinder Arora
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackAmrinder Arora
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsAmrinder Arora
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data StructuresAmrinder Arora
 

More from Amrinder Arora (10)

Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom Filters
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
Learning to learn
Learning to learnLearning to learn
Learning to learn
 

Recently uploaded

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis

  • 1. EUCLID’S ALGORITHM FOR FINDING GREATEST COMMON DIVISOR
  • 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
  • 12. REFERENCES  http://www.standardwisdom.com/algorithms-book  http://en.wikipedia.org/wiki/Euclidean_algorithm  http://www.cut -the-knot.org/blue/Euclid.shtml  http://www.albany.edu/~csi503/pdfs/lect05.pdf Algorithms - Arora Euclid's Algorithm - GCD 12