summer term 2010 robert elsässerrobert...

41
10 Randomized algorithms Summer Term 2010 Robert Elsässer Robert Elsässer

Upload: others

Post on 24-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

10 Randomized algorithms

Summer Term 2010

Robert ElsässerRobert Elsässer

Page 2: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Randomized algorithms

• Classes of randomized algorithmsClasses of randomized algorithms

• Randomized Quicksort

• Randomized primality test

• Cryptography

18.05.2010 Theory 1 - Randomized algorithms 2

Page 3: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

1. Classes of ramdomized algorithms

• Las Vegas algorithmsg galways correct; expected running time (“probably fast”)

E l d i d Q i k tExample: randomized Quicksort

• Monte Carlo algorithms (mostly correct): probably correct; guaranteed running time

Example: randomized primality test

18.05.2010 Theory 1 - Randomized algorithms 3

Page 4: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

2. Quicksort

Unsorted range A[l, r] in array Ag [ , ] y

A[l … r-1] p

pA[l...m – 1] A[m + 1...r]

Quicksort Quicksort

18.05.2010 Theory 1 - Randomized algorithms 4

Page 5: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Quicksort

Algorithm: Quicksortg Q

Input: unsorted range [l, r] in array AO t t t d [l ] i AOutput: sorted range [l, r] in array A1 if r > l2 then choose pivot element p = A[r]3 m = divide(A, l , r)

/* Divide A according to p:A[l] A[m – 1] ≤ p ≤ A[m + 1] A[r]A[l],....,A[m – 1] ≤ p ≤ A[m + 1],...,A[r]

*/4 Quicksort(A, l , m - 1)

Q i k t (A + 1 )Quicksort (A, m + 1, r)

18.05.2010 Theory 1 - Randomized algorithms 5

Page 6: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

The divide step

l rl r

rl rl

18.05.2010 Theory 1 - Randomized algorithms 6

Page 7: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

The divide step

l r

18.05.2010 Theory 1 - Randomized algorithms 7

Page 8: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

The divide step

18.05.2010 Theory 1 - Randomized algorithms 8

Page 9: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

The divide step

divide(A, l , r):

returns the index of the pivot element in Acan be done in time O(r – l)

18.05.2010 Theory 1 - Randomized algorithms 9

Page 10: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Worst case input

n elements:n elements:

Running time: (n-1) + (n-2) + … + 2 + 1 = n·(n-1)/2

18.05.2010 Theory 1 - Randomized algorithms 10

Page 11: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

3. Randomized Quicksort

Algorithm: Quicksortg QInput: unsorted range [l, r] in array AOutput: sorted range [l, r] in array A1 if > l1 if r > l2 then randomly choose a pivot element p = A[i] in range [l, r]3 swap A[i] and A[r]4 m = divide(A, l, r)

/* Divide A according to p:A[l] A[m – 1] ≤ p ≤ A[m + 1] A[r]A[l],....,A[m – 1] ≤ p ≤ A[m + 1],...,A[r]

*/5 Quicksort(A, l, m - 1)6 Quicksort(A, m + 1, r)

18.05.2010 Theory 1 - Randomized algorithms 11

Page 12: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Analysis 1

n elements; let Si be the i-th smallest element; i

S1 is chosen as pivot with probability 1/n: Sub-problems of sizes 0 and n-1

Sk is chosen as pivot with probability 1/n:Sub-problems of sizes k-1 and n-k

Sn is chosen as pivot with probability 1/n:Sub-problems of sizes n-1 and 0

18.05.2010 Theory 1 - Randomized algorithms 12

p

Page 13: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Analysis 1

Expected running time:p g

∑−

Θ+−−+=1

0)()))1(())(((1))((

n

knknTEkTE

nnTE

=0kn

∑−

Θ+=1

0)())((2 n

knkTE

n =0kn

18.05.2010 Theory 1 - Randomized algorithms 13

Page 14: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

)())((2))((1

nkTEnTEn

Θ+= ∑−

2nforlgs tenoughlargecchoose:proof0 some and 2for lg)]([ :Claim

0

ncnT(n)cnncnnTE

n k

≤>≥≤

∑=

)(lg2))((

2nfor lgs.t.enough largecchoose:proof1

nkcknTE

ncnT(n)n

Θ+≤

=≤

∑−

))( into T(1) and T(0) (include)(lg2 1

2

0

nnkknc

nn

k

k

ΘΘ+≤ ∑−

=

2n k=

18.05.2010 Theory 1 - Randomized algorithms 14

Page 15: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

∑−

Θ+≤1

)(lg2))((n

nkkcnTE

21 2 nnn−

∑=

Θ+≤2

)(lg))((k

nkkn

nTE

)8

lg2

lg(1

2

nnnkkn

k−≤∑

=

(Proof as exercise!)

)()lg(2))((22

nnnncnTE Θ+≤

))((lg

)()8

lg2

())((

ncnncn

nnn

nTE

Θ−−≤

Θ+−≤

18.05.2010 Theory 1 - Randomized algorithms 15

))(4

(lg nncn Θ−−≤

Page 16: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

))(4

(lg))(( ncnncnnTE Θ−−≤

18.05.2010 Theory 1 - Randomized algorithms 16

Page 17: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

))(4

(lg))(( ncnncnnTE Θ−−≤

desired Should be >=0Should be > 0

18.05.2010 Theory 1 - Randomized algorithms 17

Page 18: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

))(4

(lg))(( ncnncnnTE Θ−−≤

desired Should be >=0Should be > 0

E(T(n)) ≤ cn lg n where we choose c large enough s.t. cn/4 dominate Θ(n)

18.05.2010 Theory 1 - Randomized algorithms 18

Page 19: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

4. Primality test

Definition:An integer p ≥ 2 is prime iff (a | p a = 1 or a = p).

Al ith d t i i ti i lit t t ( i )Algorithm: deterministic primality test (naive)Input: integer n ≥ 2Output: answer to the question: Is n prime?

if n = 2 then return trueif n even then return falsefor i = 1 to √ n/2 dofor i = 1 to √ n/2 do

if 2i + 1 divides nthen return false

return true

Complexity: Θ(√n)

18.05.2010 Theory 1 - Randomized algorithms 19

p y ( )

Page 20: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Primality test

Goal:Randomized method• Polynomial time complexity (in the length of the input)

If i “ t i ” th i t i• If answer is “not prime”, then n is not prime• If answer is “prime”, then the probability that n is not prime is at most

p>0

k iterations: probability that n is not prime is at most pk

18.05.2010 Theory 1 - Randomized algorithms 20

Page 21: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Primality test

Observation:Each odd prime number p divides 2p-1 – 1.

E l 17 216 1 65535 17 * 3855Examples: p = 17, 216 – 1 = 65535 = 17 * 3855p = 23, 222 – 1 = 4194303 = 23 * 182361

Simple primality test:1 Calculate z = 2n-1 mod n2 if z = 12 if z = 13 then n is possibly prime4 else n is definitely not prime

Advantage: This only takes polynomial time

18.05.2010 Theory 1 - Randomized algorithms 21

Page 22: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Simple primality test

Definition:n is called pseudoprime to base 2, if n is not prime and

2n-1 mod n = 1.

Example: n = 11 * 31 = 341

2340 mod 341 = 12 mod 341 = 1

18.05.2010 Theory 1 - Randomized algorithms 22

Page 23: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Randomized primality test

Theorem: (Fermat‘s little theorem)( )If p prime and 0 < a < p, then

ap-1 mod p = 1.

Definition:n is pseudoprime to base a, if n not prime and

an-1 mod n = 1.

Example: n = 341, a = 33340 mod 341 = 56 ≠ 1

18.05.2010 Theory 1 - Randomized algorithms 23

Page 24: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Randomized primality test

Algorithm: Randomized primality test 1g p y

1 Randomly choose a ∈ [2, n-1]2 C l l t n 1 d2 Calculate an-1 mod n3 if an-1 mod n = 14 then n is possibly prime5 else n is definitely not prime

Prob(n is not prim, but an-1 mod n = 1 ) ?

18.05.2010 Theory 1 - Randomized algorithms 24

Page 25: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Carmichael numbers

Problem: Carmichael numbers

Definition: An integer n is called Carmichael number if

an-1 mod n = 1

for all a with GCD(a, n) = 1. (GCD = greatest common divisor)

Example: Smallest Carmichael number: 561 = 3 * 11 * 17

18.05.2010 Theory 1 - Randomized algorithms 25

Page 26: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Randomized primality test 2

Theorem:If p prime and 0 < a < p, then the only solutions to the equation

a2 mod p = 1

are a = 1 and a = p – 1.

Definition:a is called non-trivial square root of 1 mod n, if

a2 mod n = 1 and a ≠ 1 n 1a2 mod n = 1 and a ≠ 1, n – 1.

Example: n = 35 p62 mod 35 = 1

18.05.2010 Theory 1 - Randomized algorithms 26

Page 27: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Fast exponentiation

Idea: During the computation of an-1 (0 < a < n randomly chosen), test whether

there is a non-trivial square root 1 mod n.

Method for the computation of an:

Case 1: [n is even]an = an/2 * an/2

Case 2: [n is odd]an = a(n-1)/2 * a(n-1)/2 * a

18.05.2010 Theory 1 - Randomized algorithms 27

Page 28: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Fast exponentiation

Example:pa62 = (a31)2

a31 = (a15)2 * a15 ( 7)2 *a15 = (a7)2 * a

a7 = (a3)2 * aa3 = (a)2 * a

Complexity: O(log2an log n)

18.05.2010 Theory 1 - Randomized algorithms 28

Page 29: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Fast exponentiation

boolean isProbablyPrime;y

power(int a, int p, int n) {/* t p d d h k d i th/* computes ap mod n and checks during the

computation whether there is an x with

x2 mod n = 1 and x ≠ 1, n-1 */

if (p == 0) return 1;x = power(a, p/2, n)x power(a, p/2, n)result = (x * x) % n;

18.05.2010 Theory 1 - Randomized algorithms 29

Page 30: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Fast exponentiation

/* check whether x2 mod n = 1 and x ≠ 1, n-1 */,if (result == 1 && x != 1 && x != n –1 )

isProbablyPrime = false;

if (p % 2 == 1) result = (a * result) % n;

return result; }}

Complexity: O(log2n log p)

18.05.2010 Theory 1 - Randomized algorithms 30

Page 31: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Randomized primality test 2

primalityTest(int n) {/* carries out the randomized primality test for

a randomly selected a */

a = random(2, n-1);

isProbablyPrime = true;

result = power(a, n-1, n);

if ( lt ! 1 || !i P b bl P i )if (result != 1 || !isProbablyPrime)return false;

elsereturn true;return true;

}

18.05.2010 Theory 1 - Randomized algorithms 31

Page 32: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Randomized primality test 2

Theorem:

If n is not prime, there are at most

integers 0 < a < n, for which the algorithm primalityTest fails.

18.05.2010 Theory 1 - Randomized algorithms 32

Page 33: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Application: cryptosystems

Traditional encryption of messages with secret keysTraditional encryption of messages with secret keysDisadvantages:1. The key k has to be exchanged between A and B before the

transmission of the messagetransmission of the message.2. For messages between n parties n(n-1)/2 keys are required.

Advantage:Encryption and decryption can be computed very efficiently.

18.05.2010 Theory 1 - Randomized algorithms 33

yp yp p y y

Page 34: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Desired properties of cryptographic systems

- confidential transmission- integrity of data

th ti it f th d- authenticity of the sender- reliable transmission

18.05.2010 Theory 1 - Randomized algorithms 34

Page 35: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Public-key cryptosystems

Diffie and Hellman (1976)( )

Idea: Each participant A has two keys:

1. a public key PA accessible to every other participant

2. a private (or: secret) key SA only known to A.

18.05.2010 Theory 1 - Randomized algorithms 35

Page 36: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Public-key cryptosystems

D = set of all legal messages,D set of all legal messages, e.g. the set of all bit strings of finite length

DDSP AA →:,

Three conditions:1 P and S can be computed efficiently1. PA and SA can be computed efficiently

2. SA(PA(M)) = M and PA(SA(M)) = M (PA is the inverse function of SA and vice-versa)(PA is the inverse function of SA and vice versa)

3. SA cannot be computed from PA (without unreasonable effort)

18.05.2010 Theory 1 - Randomized algorithms 36

Page 37: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Encryption in a public-key cryptosystem

A sends a message M to B.g

Dear Bob,

I just

Dear Bob,

I just

#*k- + ;}?,

@-) #$<9

checked

the new...

checked

the new...

{o7::-&$3

(-##!]?8... ......

18.05.2010 Theory 1 - Randomized algorithms 37

Page 38: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Encryption in a public-key cryptosystem

1 A accesses B’s public key PB (from a public directory or directly from B)1. A accesses B s public key PB (from a public directory or directly from B).

2. A computes the encrypted message C = PB(M) and sends C to B.

3. After B has received message C, B decrypts the message with his own private key SB: M = SB(C)

18.05.2010 Theory 1 - Randomized algorithms 38

Page 39: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

Generating a digital signature

A sends a digitally signed message M´ to B:A sends a digitally signed message M to B:

1. A computes the digital signature σ for M´ with her own private key: σ = SA(M´)

2. A sends the pair (M´,σ) to B.

3 Aft i i (M´ ) B ifi th di it l i t3. After receiving (M ,σ), B verifies the digital signature:

PA(σ) = M´A( )

σ can by verified by anybody via the public PA.

18.05.2010 Theory 1 - Randomized algorithms 39

Page 40: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

RSA cryptosystems

R. Rivest, A. Shamir, L. AdlemanR. Rivest, A. Shamir, L. Adleman

Generating the public and private keys:

1. Randomly select two primes p and q of similar size, each with l+1 bits (l ≥ 500)each with l+1 bits (l ≥ 500).

2. Let n = p·q

3. Let e be an integer that does not divide (p - 1)·(q - 1).

4. Calculate d = e-1 mod (p - 1)(q - 1)(p )(q )

i.e.:d · e ≡ 1 mod (p - 1)(q - 1)

18.05.2010 Theory 1 - Randomized algorithms 40

(p )(q )

Page 41: Summer Term 2010 Robert ElsässerRobert Elsässerac.informatik.uni-freiburg.de/lak_teaching/ss_10/theory1/... · 2010-05-18 · Analysis 1 n elements; let S i be the i-th smallest

RSA cryptosystems

5. Publish P = (e, n) as public key5. Publish P (e, n) as public key

6. Keep S = (d, p, q) as private key

Divide message (described in a binary string) in blocks of size 2·l. 2 lInterpret each block M as a binary number: 0 ≤ M < 22·l

P(M) = Me mod n S(C) = Cd mod nP(M) M mod n S(C) C mod n

18.05.2010 Theory 1 - Randomized algorithms 41