Download presentation
Presentation is loading. Please wait.
1
Randomized QS. Ch5, CLRS Dr. M. Sakalli, Marmara University Picture 2006, RPI
2
2-1 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes Randomized Algorithm oRandomize the algorithm so that it works well with high probability on all inputs oIn this case, randomize the order that candidates arrive oUniversal hash functions: randomize selection of hash function to use
3
2-2 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes Probabilistic Analysis and Randomized Algorithms oHiring problem n candidates interviewed for a position One interview each day, if qualified hire the new, fire the current employed. Best = Meet the most qualified one at the 1 st interview. for k = 1 to n Pay c i interview cost;. if candidate(k) is better Sack current, best k; // sack current one and hire(k); Pay c h hiring (and sacking) cost; oWorst-case cost is increased quality: nc h + nc h, c h > c i, then O(nc h ) oBest-case, the least cost: ((n-1) c i + c h ), prove that hiring problem is (n) oSuppose applicants arriving in a random qualification order and suppose that the randomness is equally likely to be any one of the n! of permutations 1 through n. oA uniform random permutation.
4
2-3 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes Probabilistic Analysis and Randomized Algorithms oAn algorithm is randomized, not just if its behaviors is controlled by but if the output is generated by a random- number generator. oC rand(a,b), with boundaries are inclusive.. Equally likely (probable) outputs of X = {X i } for i=1:n. oExpected value: E[X] = Σ i n x i * (probability density of each x) = E[X] = Σ i n x i Pr{x=x i } oIn the hiring case, oDefining an indicator, Indicator random variable, X i = I { indicator of candidate if hired (or of coin), 1(H) or 0(T)}
5
2-4 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes oH and T, three fair coins, head 3$s, every tail 2$s, and expected value of earnings. oHHH= 9$s, 1/8, oHHT= 4$s, 3/8, oHTT= -1$s, 3/8, oTTT= -6$s, 1/8 oE[earnings]= 9/8+12/8-3/8-6/8=12/8=1.5
6
2-5 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes Probabilistic Analysis and Randomized Algorithms oLemma 1. Given a sample space S, and an event e in the sample space, let X e =I(e) be indicator of occurrence of e, then, E[X e ]=Pr{e}. Proof: Fr the definition of expected value, E[X A ] = Σ e n x e Pr{x=e} = 1* Pr{X e } + 0 * Pr{notX e } = Pr{X e }, where note=S-e. oIn the binary case, equally distributed, uniform distribution, E[e] = Pr{X e } = 1/2.
7
2-6 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes oX = Σ j=1 n {x i } oExpected value of a candidate been hired is the probability of candidate hired. E[x j ]= Pr{if candidate(i) is hired}, Candidate i is hired if better than previous i-1 candidates. The probability of being better is 1/i. Then, E[x i ] = Pr{x i }= 1/i oExpected value of hiring (the average number of hired ones out of n arriving candidates in random rank) E[X]. Uniform distribution, equally likely, 1/i.
8
2-7 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes oExpected value of hiring (the average number of hired ones out of n arriving candidates in random rank) E[X]. Uniform distribution, equally likely, 1/i. oE[X] = E[Σ j=1 n I i Pr{x i }], I i ={1, 0} indicator random value here. oE[X] = Σ i n E[x i ], from linearity of expected value.. oΣ i=1:(n) (1/i) = 1+1/2+1/3…, harmonic number (divergent).. Int 1 to (n+1), (1/x)=ln(n+1)<=ln(n)+O(1) oΣ i=2:(n) (1/i) = 1/2+1/3….. Int 1 to (n+1),(1/x) =ln(n)<=ln(n) oΣ i=1:(n) (1/i) = 1+1/2+1/3….. ln(n)+1 oE(X) = ln(n)+O(1), oExpected value of all hirings.. Upper boundary is ln(n). oLemma5.2: When candidates presented in random, the cost of hiring is O(c h lgn). Proof from Lemma5.1. oHow to randomize.. Some random outputs of permutations will not be random. Would it matter?..
9
2-8 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes oIn the case of dice, Pr{heads}= 1/2, in which case, for n tries, E[X] = E[Σ j=1 n {1/2}] =n/2. oBiased Coin Suppose you want to output 0 and 1 with the probabilities of 1/2 You have a coin that outputs 1 with probability p and 0 with probability 1-p for some unknown 0 < p < 1 Can you use this coin to output 0 and 1 fairly? What is the expected running time to produce the fair output as a function of p? oLet S i be the probability that we successfully hire the best qualified candidate AND this candidate was the ith one interviewed oLet M(j) = the candidate in 1 through j with highest score oWhat needs to happen for S i to be true? Best candidate is in position i: B i No candidate in positions k+1 through i-1 are hired: O i These two quantities are independent, so we can multiply their probabilities to get S i
10
2-9 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes oCharacterizing the running time of a randomized algorithm. oE[T(n)] = Σ j=1 n t i Pr{t i } void qksrt( vector & data) { RandGen rand; qksrt(data, 0, data.lngth() - 1, rand ); } void qksrt( vector & dt, int srt, int end, RandGen & rand ) { if( start < end ) { int bnd = partition(dt, srt, end, rand.RandInt(srt, end ) ); qksrt(dt, start, bnd, rand); qksrt(dt, start, bnd, rand); qksrt(dt, bnd + 1, end, rand ); qksrt(dt, bnd + 1, end, rand );}}
11
2-10 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes owe are equally likely to get each possible split, since we choose the pivot at random. oExpress the expected running time as: oT(0) = 0; oT(n) = Σ k=0 n-1 1/n[T(k)+T(n-k+1)+n]; oT(n) = n + Σ k=0 n-1 1/n[T(k)+T(n-k+1)]; oBad split, choosing from 1 st or 4 th quarter, that is i k i+ ¼[j-i+1] or j - ¼[j-i+1] k j oGood split i + ¼[i-j+1] k j - ¼[j-i+1]
12
2-11 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes oMixing good and bad coincidence with equal likelihood. oT(n) = n + Σ k=0 n-1 1/n[T(k)+T(n-k+1)]; oT(n) = n + (2/n) Σ j=n/2 n-1 [T(k)+T(n-k+1)]; = n + (2/n){Σ k=n/2 3n/4 [T(k)+T(n-k+1)] + Σ k=3n/4 n-1 [T(k)+T(n-k+1)]}; o n + (2/n){ Σ k=n/2 3n/4 [T(3n/4)+T(n/4)] + Σ k=3n/4 n-1 [T(n-1)+T(0)]}; o n + (2/n)(n/4){[T(3n/4)+T(n/4)] + [T(n-1)]}; o n +(1/2){[T(3n/4)+T(n/4)] + [T(n-1)]}; oProve that for all n T(n) cnlog(n), T(n) is the statement obtained above. Inductive proof, for n=0, and n=n, oProbability of many bad splits is very small. With high probability the list is divided into fractional pieces which is enough balance to get asymptotic n log n running time. o n + (1/2)[c(3n/4) log(3n/4) + c(n/4) log(n/4)] +(1/2)c(n-1)log(n-1)
13
2-12 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes oMIT notess oL(n)= 2U(n/2) + Θ(n) lucky oU(n)= L(n –1) + Θ(n) unlucky oL(n)= 2U(n/2 –1) + 2Θ(n/2)+ Θ(n) oL(n)= 2U(n/2 –1) + Θ(n) oL(n)= Θ(nlogn) oAnd there is more there..
14
2-13 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes Computing S oB i = 1/n oO i = k/(i-1) oS i = k/(n(i-1)) oS = i>k S i = k/n i>k 1/(i-1) is probability of success ok/n (H n – H k ): roughly k/n (ln n – ln k) oMaximized when k = n/e oLeads to probability of success of 1/e
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.