Algorithms Design and its Applications
description
Transcript of Algorithms Design and its Applications
![Page 1: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/1.jpg)
Algorithms Design and its Applications
Algorithms with numbers
![Page 2: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/2.jpg)
Back Ground
• Number theory was once viewed as a beautiful but largely useless subject in pure mathematics.
• Today number- theoretic algorithms are used widely , due in part to the invention of cryptographic schemes based on large prime numbers.
• The feasibility of these schemes rests on our ability to find large primes easily, while their security rests on our inability to factor the product of large primes .
![Page 3: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/3.jpg)
Size of Cost
• In this lecture, a "large input" typically means an input containing "large integers" rather than an input containing "many integers".
• We measure the size of an input in terms of the number of bits required to represent that input.
• An algorithm with integer inputs a1, a2, ..., ak is a polynomial - time algorithm if it runs in time polynomial in lg a1, lg a2, ..., lg ak, that is, polynomial in the lengths of its binary- encoded inputs.
![Page 4: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/4.jpg)
Basic arithmetic
![Page 5: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/5.jpg)
Bases and logs
• How many digits are needed to represent the number N>=0 in base b?
• How much does the size of a number change when we change the base?
![Page 6: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/6.jpg)
Addition
• The sum of any three single-digit numbers (with base b>=2) is at most two digits long.
• Given two binary number x and y, how long does our algorithm take to add them?
O(n), n is the number of bits of x and y.
![Page 7: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/7.jpg)
Multiplication
• 13 * 11
![Page 8: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/8.jpg)
Multiplication
• 乘法的时间复杂度是多少呢?• 对于长度为 n 的乘数来说,将产生 n 个中间结
果,而对这些中间结果进行相加的次数是 n-1次,从而时间复杂度为 O(n(n-1))=O(n2)
![Page 9: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/9.jpg)
Multiplication
• another way to multiply
![Page 10: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/10.jpg)
Multiplication
![Page 11: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/11.jpg)
Multiplication
• Al Khwarizmi 乘法算法的时间复杂度?• 由于乘数每次都被取半,对于二进制来说,取半
意味着去掉最右边一位数,而乘数的长度为 n ,因此该算法在递归 n 次后终结。每次递归需要进行一次长度为 n 位的加法运算,其时间复杂度为O(n) 。因此 Al Khwarizmi 乘法算法的时间复杂度为 O(n2) 。
![Page 12: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/12.jpg)
Division• To divide an integer x by another integer y≠0 means
to find a quotient q and a remainder r, where x = yq+r and r <y.
![Page 13: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/13.jpg)
Modular arithmetic
![Page 14: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/14.jpg)
Modular Arithmetic Basic
![Page 15: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/15.jpg)
Modular Arithmetic Basic
![Page 16: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/16.jpg)
Modular Addition
• x+y mod N• Since x and y are in the range 0 to N-1, their sum is
between 0 and 2(N-1). If the sum exceeds N-1, subtract off N.
• The overall computation consists of an addition, and possibly a subtraction, of numbers that never exceed 2N. the running time is O(n), where n = log N.
![Page 17: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/17.jpg)
Modular multiplication
• xy mod N• start with regular multiplication, then reduce the
answer modulo N. The product can be as large as (N-1)2, at most 2n bits long since log(n-1)2 = 2log(N-1)≤2n.
• The running time is O(n2).
![Page 18: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/18.jpg)
Modular exponentiation
• 在密码学中,常需计算 xy mod N. 这个的 x,y 和 N均为几百位长的整数。如何快速计算?
• 直接算 xy ,运算结果很大!即便 x 和 y 只有 20位长, xy 也要大概 1 千万位长。
• 为保证中间运算结果不要太大,每步运算都模 N.
![Page 19: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/19.jpg)
Modular exponentiation
• Simple idea: repeatedly multiplying by x modulo N.
problem: if y is 500 bits long, we need to perform y -1 ≈ 2500 multiplications!
![Page 20: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/20.jpg)
Modular exponentiation
• better idea: starting with x and squaring repeatedly modulo N, we get
we need to perform log y multiplications, ach takes O(log2N) to compute.
• To determine xy mod N, we simply multiply together and appropriate subset of these powers, those corresponding to 1’s in the binary representation of y.
• A polynomial-time algorithm is within reach!
![Page 21: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/21.jpg)
Modular exponentiation
![Page 22: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/22.jpg)
sicily 1294. 高级机密 • 信息加密。• 目前比较流行的编码规则称为 RSA ,是由美国麻
省理工学院的三位教授发明的。这种编码规则是基于一种求密取模算法的:对于给出的三个正整数 a,b,c ,计算 a 的 b 次方除以 c 的余数。
• 题目要求:计算 ab mod c
![Page 23: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/23.jpg)
sicily 1294. 高级机密问题分析
• 不好的算法:– 先求出 a的 b次方,再模 c。但题目给出的a,b,c的范围比较大,要算出 ab 要用到高精度乘法,然后模 c还要用到高精度除法;
• 较好的算法:– 利用同余的性质,
xy mod c = x * (y mod c) mod c
![Page 24: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/24.jpg)
sicily 1294. 高级机密代码
d = 1; for (i = 1; i <= b; ++i) { d = d * a % c; } cout << d;
![Page 25: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/25.jpg)
Euclid’s algorithm for greatest common divisor
• Euclid’s ruleIf x and y are positive integer with x≥y, then gcd(x,y)= gcd(x mod y, y)
• Proof. 因为 gcd(x,y) 能整除 x 和 y ,因此整除 x-y ,即是 x-y 的因子,因此 gcd(x,y)≤ gcd(x-y, y).而反过来推,同理可得 gcd(x-y, y) ≤ gcd(x,y) 。故 gcd(x,y)=gcd(x-y, y) 。由此显然可得结论。
![Page 26: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/26.jpg)
Euclid’s algorithm for greatest common divisor
![Page 27: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/27.jpg)
Euclid’s algorithm for greatest common divisor
• This means that after any two consecutive round, both a and b, are at the very least halved in value – the length of each decreases by at least one bit. If they are initially n-bits integers, then the base case will be reached within 2n recursive calls. And since each call involves a quadratic-time division, the total time is O(n3)
![Page 28: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/28.jpg)
An extension of Euclid algorithm
只要找到两个整数 x 和 y ,使得 ax+by=d ,且 d 是a 和 b 的因子,则 d 就是 a 和 b 的最大公因子;如果 d 是 a 和 b 的最大公因子,则 d 一定可以表示为ax+by 形式。只要对欧几里得算法稍加扩展,即可找到所需的系数 x 和 y 。
![Page 29: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/29.jpg)
An extension of Euclid algorithm
![Page 30: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/30.jpg)
An extension of Euclid algorithm
• LemmaFor any positive integers a and b, the extended Euclid algorithm returns integers x, y, and d such that gcd(a,b) = d = ax+by
• Proof.
对 b做归纳假设。当 b=0, 验证可知算法正确。 算法调用 gcd(b,a mod b) 来计算 gcd(a,b) 。由于 a mod b < b , 由归纳假设知返回结果是正确的 .
![Page 31: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/31.jpg)
Modular division
![Page 32: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/32.jpg)
减法求最大公约数于大整数而言,取模运算(其中用到除法)是非常昂贵的开销,将成为整个算法的瓶颈。有没有办法能够不用取模运算呢?如果一个数能够同时整除 x和 y,则必能同时整除 x-y和y;而能够同时整 x-y和 y的数也必能同时整除 x和 y,即 x和 y的公约数与 x-y和 y的公约数是相同的,其最大公约数也是相同的,即 f( x, y) = f( x-y, y),那么就可以不再需要进行大整数的取模运算,而转换成简单得多的大整数的减法。实例: f( 42, 30 ) =f( 30, 12 ) =f( 12, 18 ) = f( 18, 12 ) = f( 12, 6 ) = f( 6, 6 ) = f( 6, 0 ) = 6 不足之处。最大的瓶颈就是迭代的次数比之前的算法多了不少,如果遇到( 10 000 000 000 000, 1 )
![Page 33: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/33.jpg)
减法求最大公约数代码
BigInt gcd(BigInt x, BigInt y){ if(x < y) return gcd(y, x); if(y == 0) return x; else return gcd(x - y, y);}
![Page 34: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/34.jpg)
求最大公约数算法三算法一 ( 欧几里得算法 ) 的问题在于计算复杂的大整数除法运算,而算法二虽然将大整数的除法运算转换成了减法运算,降低了计算的复杂度,但它的问题在于减法的迭代次数太多,如果遇到( 10 000 000 000 000, 1 )的情况就很糟糕。
能否结合算法一和算法二从而使其成为一个最佳的算法呢?
![Page 35: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/35.jpg)
求最大公约数算法三记 x和 y的最大公约数为 f( x, y)。若 x, y均为偶数, f( x, y) = 2 * f( x/2, y/2 ) = 2 * f( x>>1, y>>1 )若 x为偶数, y为奇数, f( x, y) = f( x/2, y) = f( x>>1, y)若 x为奇数, y为偶数, f( x, y) = f( x, y/2 ) = f( x, y>>1 )若 x, y均为奇数, f( x, y) = f( x, x - y),那么在 f( x, y) = f( x, x - y)之后,( x - y)是一个偶数,下一步一定会有除以 2 的操作。最坏情况下的时间复杂度是 O( log2 ( max ( x, y))。
![Page 36: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/36.jpg)
求最大公约数算法三示例:f( 42, 30 ) = f( 1010102, 111102 )= 2 * f( 101012, 11112 ) = 2 * f( 11112, 1102 )= 2 * f( 11112, 112 )= 2 * f( 11002, 112 )= 2 * f( 112, 112 ) = 2 * f( 02, 112 )= 2 * 112
= 6
![Page 37: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/37.jpg)
求最大公约数算法三BigInt gcd(BigInt x, BigInt y){ if(x < y) return gcd(y, x); if(y == 0) return x; else { if(IsEven(x)){ if(IsEven(y)) return (gcd(x >> 1, y >> 1) << 1); else return gcd(x >> 1, y); } else { if(IsEven(y)) return gcd(x, y >> 1); else return gcd(y, x - y); } }}
![Page 38: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/38.jpg)
同余• 同余
– 设 m 是正整数, a,b 是整数,如果 m|(a-b) ,则称 a 和 b 关于模 m同余,记作 a≡b(mod m) 或者说,如果 a,b 除以 m 的余数相等,则称 a 和 b 关于模 m 同余
• 同余的性质– a≡a(mod m)– 如果 a≡b(mod m) ,则 b≡a(mod m)– 如果 a≡b(mod m) 且 b≡c(mod m) , a≡c(mod m)– 如果 a≡b(mod m) 且 c≡d(mod m) ,则 a±c≡b± d(mod m) ,
ac≡bd(mod m)
![Page 39: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/39.jpg)
同余• 同余的性质 (cont.)
– 如果 a≡b(mod m) ,则 an≡bn(mod m) , n N∈– 如果 ac≡bc(mod m) ,则 a≡b(mod (m/gcd(c,m))– 如果 a≡b(mod m) 且 d|m ,则 a≡b(mod d)– 如果 a≡b(mod m) ,则 ad≡bd(mod m)– 如果 a≡b(mod mi) , i=1,2,…,n , l=lcm(m1,m2,…,mn) ,则
a≡b(mod l)– 如果 p 为素数,则 ap ≡ a(mod p) ;如果 gcd(a,p)=1 ,则 ap-1 ≡
1(mod p)
![Page 40: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/40.jpg)
Primality Testing
![Page 41: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/41.jpg)
筛法求素数2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
![Page 42: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/42.jpg)
筛法求素数代码
const int MAX = 10000;bool prime[MAX + 1];void searchprime() { memset(prime, true, sizeof(prime)); prime[1] = false; for (int i = 2; i*i<= MAX; ++i) { if (prime[i]) { int j = i * 2; while (j <= MAX) { prime[j] = false; j += i; } } }}
![Page 43: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/43.jpg)
代码(筛法求素数)for (int i = 2; i <= (int) floor(sqrt(MAX)); ++i) { if (prime[i]) { int j = i * 2; while (j <= MAX) { prime[j] = false; j += i; } } }}
![Page 44: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/44.jpg)
Fermat’s little theorem
![Page 45: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/45.jpg)
Fermat’s little theorem
• Proof.
![Page 46: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/46.jpg)
Algorithm for testing primality
![Page 47: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/47.jpg)
Algorithm for testing primality
![Page 48: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/48.jpg)
Algorithm for testing primality
![Page 49: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/49.jpg)
An algorithm for testing primality, with low error probability
![Page 50: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/50.jpg)
Carmichael numbers
• 561 = 3*11*17, not a prime.• fool the Fermat test, because a560 ≡1 (mod 561) for all
values of a relatively prime to 561.• Rabin and Miller algorithm.
![Page 51: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/51.jpg)
Generating random primes
![Page 52: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/52.jpg)
Generating random primes
• Q: what is the probability that the output of the algorithm is really prime?
• A: suppose we perform the test with base a=2 for all numbers N≤25∙109.
![Page 53: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/53.jpg)
RSA
• RSA 基本原理• 选定一个数 N ,再选择一个 N 到 N 的双射函数 f
作为加密密钥(公钥),该函数的逆函数作为解密密钥(密钥)。 f 的选定必须使得其逆函数无法从 f 推出。
![Page 54: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/54.jpg)
RSA
![Page 55: Algorithms Design and its Applications](https://reader035.fdocuments.in/reader035/viewer/2022081503/56814986550346895db6ce32/html5/thumbnails/55.jpg)
RSA