Date post: | 22-Dec-2015 |
Category: |
Documents |
View: | 272 times |
Download: | 0 times |
1
高等演算法 -Introduction
1. Analysis2. Basic arithmetic3. Modular arithmetic4. GCD5. Primality testing6. Cryptography
2
Algorithm Algorithm : a specified set of simple
instructions to be followed to
solve a problem.
require how much in the way of
resoureces : time or space.
以決定該 algorithm 是否 useful.
1. Analysis
3
Analysis Note that1. Input size 2. the number of basic computer
steps
1. Analysis
4
FibonacciFibonacci number: 0, 1, 1, 2, 3, 5, 8
Rule:
0n if 0
1n if 1
1 n if FF
F2-n1-n
n
1. Analysis
5
FibonacciExponential
function fib1(n) if n=0: return 0 if n=1: return 1return fib1(n-1) + fib1(n-2)
polynomial
function fib2(n) if n=0: return 0 create an array f[0…n] f[0]=0, f[1]=1 for i=2…n: f[i]=f[i-1]+f[i-2]return f[n]T(n)=T(n-1)+T(n-2)+3
T(n) ≧FnT(n) = n-1
1. Analysis
6
Fibonacci
polynomial
T(n) = O(logn)
F
F
1 1
1 0
F
F
.... F
F
1 1
1 0
F
F
1
0
n
1n
n
1
0
2
1
1. Analysis
兩個 22matrix 相乘 4 加法 8 乘法
7
Fibonacci
1 1
1 0
1 1
1 0 ....
1 1
1 0
1 1
1 0
1 1
1 0
1 1
1 0
1. Analysis
T(n) = O(logn)
8
Fibonacci
1. Analysis
公式解
nn
2
51
5
1
2
51
5
1Fn
9
Big-O Def : T(N)=O(f(N)) if there are positive constant c and n0 such that T(N) cf(N) when N n0
例: T(N)=1000N, f(N)=N2 n0=10 c=100 1000N cN2 ∴1000N=O(N2)
1. Analysis
10
Def :
Def :
Def :
0nNwhen)N(cf)N(T
))N(f()N(T
))N(f()N(Tand
))N(f(O)N(Tiff))N(f()N(T
))N(f()N(Tand
))N(f(O)N(Tif))N(f(o)N(T
Big-O
1. Analysis
11
)(log)(),()(
)()(),()(),()(2)(( 2342
NNTNNT
NONTNONTNONTNNT
)N(Tonboundloweranis)N(f))N(f()N(T
)N(Tonboundupperanis)N(f))N(f(O)N(T
Big-O
o
O
))(()(
))(()(
NfoNT
NfONT
不可寫
1. Analysis
12
如何判斷 function 成長速度 1. 基本 function :
2.
例如: error
Nk322 2NNNNlogNNNlogNlogc
))()(()()(.
))(()),((max()()(.
))(()())(()(
21
21
21
NgNfONTNTb
NgONfONTNTathen
NgONTandNfONTif
)()(
)2()(2
2
NONT
NNONT
1. Analysis
13
NlogN
NlogN
1. Analysis
14
如何判斷 function 成長速度 3. 利用 limit 比較 f(N) 與 g(N) 計算
例如:比較 NlogN 與 N1.5
?)N(g/)N(flimn
))N(f(o)N(g
))N(g()N(f0c
))N(g(o)N(f0
0N
NlogNlim
5.1n
1. Analysis
15
2. Basic arithmeticAddition:
Input:x and y are each n bits long;
bit complexity: O(n)
16
2. Basic arithmeticMultiplication :
bit complexity: O(n2)
13 × 11; x = 1101 and y = 1011.
If x and y are both n bits, then there are n intermediate rows, with lengths of up to 2n bits.
17
2. Basic arithmeticMultiplication :
Khwarizmi’s method 13 × 11
bit complexity: O(n2)
13)13*2)(5(
)13*2)(2
15()13*2)(
2
11(
13*11
偶數
( 高度 n)
18
2. Basic arithmeticMultiplication :
Karatsuba’s method
bit complexity: O(n1.585)
Schonhage-Strassen’s method (based on FFT)
bit complexity: O(nlognloglogn)
A = 2n/2A1 + A0 B = 2n/2B1 + B0
A*B = 2nA1B1 + 2n/2(A0B1 + A1B0)+ A0B0
= (2n + 2n/2) A1B1 + 2n/2(A1- A0)(B0- B1)+ (2n +1)A0B0
T(n) 3T(n/2) + c*n
T(n) 3c’nlog23
19
2. Basic arithmeticDivision:
x = yq + r and r < y.
Division of a 2n-bit integer by an n-bit integer can be performed using O(n2) bit operations
There is an algorithm to find the quotient q = [x/y], when the 2n-bit integer x is divided by the integer y having no more than n bits, using O(M(n)), where M(n) is the number of bit operations needed to multiply two n-bit integers.
bit complexity: O(nlognloglogn)
20
3. Modular arithmetic
x ≡ y (mod N) ⇐⇒ N divides (x − y).
21
3. Modular arithmetic
22
Modular addition
Since x and y are each in the range 0 to N − 1, their sum is between 0 and 2(N − 1). If the sumexceeds N − 1, we merely need to subtract off N to bring it back into the required range. The overall computation therefore consists of an addition, and possibly a subtraction, of numbers that never exceed 2N. Its running time is linear in the sizes of these numbers, in other words O(n), where n = log N is the size of N;
x+y mod N
3. Modular arithmetic
23
Modular multiplication
The product can be as large as (N − 1)2, but this is still at most 2n bits long since log(N − 1)2 = 2 log(N − 1) ≤ 2n.To reduce the answer modulo N, we compute the remainder upon dividing it by N, using our quadratic-time division algorithm. Multiplication thus remains a quadraticoperation. O(n2)
x.y mod N
3. Modular arithmetic
24
Modular exponentiation
xy mod N
O(y) modular multiplications
is clearly exponential in the size of y
O(logy) modular multiplications; bit complexity: O(n3)
3. Modular arithmetic
25
Modular exponentiation
3. Modular arithmetic
Square and multiply
3723 mod 55 = 53
3710111 = ((((((((371)2)370)2)371)2)371)2)371) mod 55
(..((371)2mod 55)370)2mod 55)371mod 55 )2mod 55 )371mod 55)2mod 55)371) mod 55 =53
26
5. Primality testing
But, 341 = 11 · 31 is not prime, and yet 2340 ≡ 1 mod 341 (a=2)
27
5. Primality testingCarmichael Numbers
28
5. Primality testingLemma: If aN−1 ≡ 1 mod N for some a relatively prime to N, then it must hold forat least half the choices of a < N.
If N is prime, then aN−1 ≡ 1 mod N for all a < N.If N is not prime, then aN−1 ≡ 1 mod N for at most half the values of a < N.
29
5. Primality testing
Pr(Algorithm 1.8 returns yes when N is not prime) ≤1/2k
30
5. Primality testingGiven a number N, prove that N is a prime.
Sieve of Eratosthenes
Miller 1975 (using Riemann hypothesis)
Adleman, Pomerance and Rumely 1983
Agrawal, Kayal and Saxena 2002
Lenstra and Pomerance 2003
)N(O
))Nlog((O 5
))Nlog((O Nlogloglogc
))Nlog((O 12
))Nlog((O 6
31
6. Cryptography
1. 張三選 2 個大質數 p 和 q ( 至少 100 位數 ) ,令 N = p • q
2. 再計算 Ø(N)=(p-1)(q-1) ,並選 1 個與 Ø(N) 互質數 e
Ø(N) 為 Euler‘s Totient 函數,其意為與 N 互質之個數3. (e,N) 即為張三的公開金鑰
加密法為 C = Me mod N
4. 張三選 1 個數 d ,滿足 e • d mod Ø(N) = 15. d 即為張三的解密金鑰 ( 亦稱私有金鑰或祕密金鑰 )
解密法為 M = Cd mod N RSA 之安全性取決於質因數分解之困難度 要將很大的 N 因數分解成 P 跟 Q 之相乘,是很困難的
RSA
32
6. Cryptography
1. 張三選 p=5 , q=11 ; 此時 N = p • q = 5 x 11 = 552. 張三選出 1 個與 ( p-1 ) x ( q-1 ) = ( 5-1 )( 11-1 )
= 4 x 10 = 40互質數 e=7
3. ( e, N) = (7,55) 即為張三的公開金鑰4. 張三選 1 個數 d=7 當作解密金鑰,
滿足 e • d 1 mod 40 ( 7 x 23 1 mod 40 )
令明文 M = 53加密 : C = Me mod N = 537 mod 55 = 37解密 : M = Cd mod N = 3723 mod 55 = 53