cs70 today euclid s gcd algorithm multiplicative inverse
play

CS70: Today Euclids GCD algorithm. Multiplicative Inverse. (define - PowerPoint PPT Presentation

CS70: Today Euclids GCD algorithm. Multiplicative Inverse. (define (euclid x y) (if (= y 0) Extended Euclid. GCD algorithm used to tell if there is a multiplicative inverse. x Midterm Review Slides. How do we find a multiplicative inverse?


  1. CS70: Today Euclid’s GCD algorithm. Multiplicative Inverse. (define (euclid x y) (if (= y 0) Extended Euclid. GCD algorithm used to tell if there is a multiplicative inverse. x Midterm Review Slides. How do we find a multiplicative inverse? (euclid y (mod x y)))) Memories, misty water colored memories... Computes the gcd ( x , y ) in O ( n ) divisions. For x and m , if gcd ( x , m ) = 1 then x has an inverse modulo m . Extended GCD Make d out of x and y ..? Extended GCD Algorithm. Euclid’s Extended GCD Theorem: For any x , y there are integers ext-gcd(x,y) gcd(35,12) a , b such that if y = 0 then return(x, 1, 0) gcd(12, 11) ;; gcd(12, 35%12) ax + by = d where d = gcd ( x , y ) . else gcd(11, 1) ;; gcd(11, 12%11) (d, a, b) := ext-gcd(y, mod(x,y)) gcd(1,0) “Make d out of sum of multiples of x and y .” return (d, b, a - floor(x/y) * b) 1 What is multiplicative inverse of x modulo m ? How did gcd get 11 from 35 and 12? Claim: Returns ( d , a , b ) : d = gcd ( a , b ) and d = ax + by . 35 −⌊ 35 12 ⌋ 12 = 35 − ( 2 ) 12 = 11 By extended GCD theorem, when gcd ( x , m ) = 1. Example: a −⌊ x / y ⌋· b = 1 −⌊ 11 / 1 ⌋· 0 = 1 1 −⌊ 35 / 12 ⌋· ( − 1 ) = 3 0 −⌊ 12 / 11 ⌋· 1 = − 1 How does gcd get 1 from 12 and 11? ax + bm = 1 12 −⌊ 12 11 ⌋ 11 = 12 − ( 1 ) 11 = 1 ext-gcd(35,12) ax ≡ 1 − bm ≡ 1 ( mod m ) . ext-gcd(12, 11) Algorithm finally returns 1. So a multiplicative inverse of x ( mod m ) !! ext-gcd(11, 1) Example: For x = 12 and y = 35 , gcd ( 12 , 35 ) = 1. But we want 1 from sum of multiples of 35 and 12? ext-gcd(1,0) ( 3 ) 12 +( − 1 ) 35 = 1 . Get 1 from 12 and 11. return (1,1,0) ;; 1 = (1)1 + (0) 0 1 = 12 − ( 1 ) 11 = 12 − ( 1 )( 35 − ( 2 ) 12 ) = ( 3 ) 12 +( − 1 ) 35 return (1,0,1) ;; 1 = (0)11 + (1)1 a = 3 and b = − 1. Get 11 from 35 and 12 and plugin.... return (1,1,-1) ;; 1 = (1)12 + (-1)11 The multiplicative inverse of 12 ( mod 35 ) is 3. Simplify. a = 3 and b = − 1. return (1,-1, 3) ;; 1 = (-1)35 +(3)12

  2. Extended GCD Algorithm. Correctness. Review Proof: step. Proof: Strong Induction. 1 Base: ext-gcd ( x , 0 ) returns ( d = x , 1 , 0 ) with x = ( 1 ) x +( 0 ) y . Induction Step: Returns ( d , A , B ) with d = Ax + By ext-gcd(x,y) Ind hyp: ext-gcd ( y , mod ( x , y )) returns ( d , a , b ) with ext-gcd(x,y) if y = 0 then return(x, 1, 0) d = ay + b ( mod ( x , y )) if y = 0 then return(x, 1, 0) else else ext-gcd ( x , y ) calls ext-gcd ( y , mod ( x , y )) so (d, a, b) := ext-gcd(y, mod(x,y)) (d, a, b) := ext-gcd(y, mod(x,y)) return (d, b, a - floor(x/y) * b) d = ay + b · ( mod ( x , y )) return (d, b, a - floor(x/y) * b) ay + b · ( x −⌊ x = y ⌋ y ) Theorem: Returns ( d , a , b ) , where d = gcd ( a , b ) and Recursively: d = ay + b ( x −⌊ x ⇒ d = bx − ( a −⌊ x y ⌋· y ) = y ⌋ b ) y bx +( a −⌊ x = y ⌋· b ) y Returns ( d , b , ( a −⌊ x d = ax + by . y ⌋· b )) . And ext-gcd returns ( d , b , ( a −⌊ x y ⌋· b )) so theorem holds! 1 Assume d is gcd ( x , y ) by previous proof. Wrap-up Midterm Review First there was logic... A statement is a true or false. Statements? 3 = 4 − 1 ? Statement! Conclusion: Can find multiplicative inverses in O ( n ) time! 3 = 5 ? Statement! Very different from elementary school: try 1, try 2, try 3... 3 ? Not a statement! 2 n / 2 n = 3 ? Not a statement...but a predicate. Predicate: Statement with free variable(s). Inverse of 500 , 000 , 357 modulo 1 , 000 , 000 , 000 , 000? Now... Example: x = 3 ≤ 80 divisions. Given a value for x , becomes a statement. versus 1 , 000 , 000 Predicate? Internet Security. n > 3 ? Predicate: P ( n ) ! Public Key Cryptography: 512 digits. x = y ? Predicate: P ( x , y ) ! 512 divisions vs. x + y ? No. An expression, not a statement. ( 10000000000000000000000000000000000000000000 ) 5 divisions. Quantifiers: ( ∀ x ) P ( x ) . For every x , P ( x ) is true. Internet Security: Next Week! ( ∃ x ) P ( x ) . There exists an x , where P ( x ) is true. ( ∀ n ∈ N ) , n 2 ≥ n . ( ∀ x ∈ R )( ∃ y ∈ R ) y > x .

  3. Connecting Statements ..and then proofs... ...jumping forward.. Direct: P = ⇒ Q ⇒ a 2 is even. Example: a is even = Approach: What is even? a = 2 k a 2 = 4 k 2 . A ∧ B , A ∨ B , ¬ A . What is even? Contradiction in induction: a 2 = 2 ( 2 k 2 ) You got this! contradict place where induction step doesn’t hold. Integers closed under multiplication! Propositional Expressions and Logical Equivalence Well Ordering Principle. a 2 is even. Stable Marriage: ( A = ⇒ B ) ≡ ( ¬ A ∨ B ) Contrapositive: P = ⇒ Q or ¬ Q = ⇒ ¬ P . first day where women does not improve. ¬ ( A ∨ B ) ≡ ( ¬ A ∧¬ B ) Example: a 2 is odd = ⇒ a is odd. first day where any man rejected by optimal women. ⇒ a 2 is even. Proofs: truth table or manipulation of known formulas. Contrapositive: a is even = Do not exist. ( ∀ x )( P ( x ) ∧ Q ( x )) ≡ ( ∀ x ) P ( x ) ∧ ( ∀ x ) Q ( x ) Contradiction: P ¬ P = ⇒ false ¬ P = ⇒ R ∧¬ R Useful for prove something does not exist: √ Example: rational representation of 2 does not exist. Example: finite set of primes does not exist. Example: rogue couple does not exist. ...and then induction... Stable Marriage: a study in definitions and WOP . Stable Marriage Algorithm. P ( 0 ) ∧ (( ∀ n )( P ( n ) = ⇒ P ( n + 1 ) ≡ ( ∀ n ∈ N ) P ( n ) . TMA: Day by Day. n -men, n -women. Thm: For all n ≥ 1, 8 | 3 2 n − 1. All men propose to favorite unrejecting woman left. Each person has completely ordered preference list Every woman rejects all but best men who proposes. contains every person of opposite gender. Induction on n . Useful Algorithmic Definitions: Base: 8 | 3 2 − 1. Pairing. Man crosses off woman who rejected him. Set of pairs ( m i , w j ) containing all people exactly once. Woman’s current proposer is “on string.” Induction Hypothesis: Assume P ( n ) : True for some n . How many pairs? n . (3 2 n − 1 = 8 d ) People in pair are partners in pairing. “Propose and Reject.” : Either gender proposes. Not both. Induction Step: Prove P ( n + 1 ) Key Property: Improvement Lemma: Rogue Couple in a pairing. 3 2 n + 2 − 1 = 9 ( 3 2 n ) − 1 (by induction hypothesis) A m j and w k who like each other more than their partners If man on string for woman, = ⇒ any future man on string is better. = 9 ( 8 d + 1 ) − 1 Stable Pairing. Stability: No rogue couple! = 72 d + 8 Contradiction: Rogue couple (M,W) Pairing with no rogue couples. = 8 ( 9 d + 1 ) = ⇒ M proposed to W Does stable pairing exist? = ⇒ W ended up with someone she liked better than M . Divisible by 8. No, for roommates problem. Not rogue couple!

  4. Optimality/Pessimal ...Graphs... Graph Algorithm: Eulerian Tour Optimal partner if best partner in any stable pairing. G = ( V , E ) Not necessarily first in list. V - set of vertices. Possibly no stable pairing with that partner. Thm: Every connected graph where every vertex has even degree E ⊆ V × V - set of edges. has an Eulerian Tour; a tour which visits every edge exactly once. Man-optimal pairing is pairing where every man gets optimal partner. Directed: ordered pair of vertices. Algorithm: Thm: TMA produces male optimal pairing, S . Adjacent, Incident, Degree. Take a walk using each edge at most once. First man M to lose optimal partner. In-degree, Out-degree. Property: return to starting point. Better partner W for M . = ⇒ Different stable pairing T . Proof Idea: Even degree. Thm: Sum of degrees is 2 | E | . TMA: M asked W first! There is M ′ who bumps M in TMA. = Edge is incident to 2 vertices. W prefers M ′ . ⇒ Recurse on connected components. Degree of vertices is total incidences. Not first bump. Put together. ⇒ M ′ likes W at least as much as optimal partner. = Property: walk visits every component. Pair of Vertices are Connected: M ′ and W is rogue couple in T . Proof Idea: Original graph connected. If there is a path between them. Thm: TMA = ⇒ woman pessimal. Connected Component: maximal set of connected vertices. Man optimal = ⇒ Woman pessimal. Connected Graph: one connected component. Woman optimal = ⇒ Man pessimal. Graph Coloring. Planar graphs and maps. Six color theorem. Given G = ( V , E ) , a coloring of a G assigns colors to vertices V Planar graph coloring ≡ map coloring. Theorem: Every planar graph can be colored with six colors. where for each edge the endpoints have different colors. Proof: Recall: e ≤ 3 v − 6 for any planar graph where v > 2. From Euler’s Formula. Total degree: 2 e Average degree: ≤ 2 e v ≤ 2 ( 3 v − 6 ) ≤ 6 − 12 v . v There exists a vertex with degree < 6 or at most 5. Remove vertex v of degree at most 5. Notice that the last one, has one three colors. Inductively color remaining graph. Fewer colors than number of vertices. Color is available for v since only five neighbors... Fewer colors than max degree node. and only five colors are used. Four color theorem is about planar graphs! Interesting things to do. Algorithm!

Recommend


More recommend