forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRand.html
311 lines (267 loc) · 7.9 KB
/
Rand.html
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css"/>
<title>
Randomized Algorithms
</title>
</head>
<body>
<h1>
Randomized Algorithms
</h1>
<center>
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/6sided_dice.jpg/300px-6sided_dice.jpg">
</center>
<br>
<h2>
The hiring problem
</h2>
<p>
<a
href="https://github.com/gcallah/algorithms/blob/master/python/randomized_algorithms.py">
Source code here.
</a>
</p>
<h3>
Worst-case analysis
</h3>
<p>
The worst case: the employment agency sets us up, and sends us
candidates in worst-to-best order, so we hire every single one.
</p>
<h3>
Probabilistic anaysis
</h3>
<p>
We use probability theory to find an <em>average-case running
time</em>.
<br>
<br>
<a href="Probability.html">Probability basics</a>.
</p>
<h3>
Randomized algorithms
</h3>
<p>
We best the employment agency by shuffling their list of candidates,
and seeing them in random order: we get a much lower average cost, as we shall
prove.
</p>
<p>
When we randomize the algorithm we speak of an <em>expected
running time</em>.
</p>
<h2>
Indicator random variables
</h2>
<p>
(Note: <a href="https://en.wikipedia.org/wiki/Random_variable">
random variables</a> are actually functions.)
<br>
<br>
X is an <em>indicator variable</em> for an event A if:
<br>
</p>
<ul>
<li>X = 1 if A happens
<li>X = 0 if A does not happen
</ul>
<h3>
Useful properties of indicator variables:
</h3>
<p>
If X is an indicator variable for A, then
<br> <br>
<em>
Exp[X] = Prob[X=1] = Prob[A happens]
</em>
.
<br> <br>
This easily follows from the definition:
<br> <br>
<em>
Exp[X] = 1 * Prob[X=1] + 0 * Prob[X=0] = Prob[X=1] = Prob[A happens]
</em>
<br>
<br>
<strong>Example:</strong>
<br>
<br>
We throw a fair die 100 times What is the expected number
of times we get 1 or 6?
<br>
<br>
Let X<sub>i</sub> be the indicator variable of the event "in
i<sup>th</sup> throw we got 1 or 6".
Then notice that the number of times we get 1 or 6 is:
<br>
<br>
<img
src=
"graphics/Lec3Eq1.gif">
<br>
<br>
and we want the expectation of this. By linearity of expectations:
<br>
<br>
<img
src=
"graphics/Lec3Eq2.gif">
<br>
<br>
and by the indicator variable properties:
<br> <br>
<img
src=
"graphics/Lec3Eq3.gif">
<br> <br>
To summarize:
<br>
<br>
<img
src=
"graphics/Lec3Eq4.gif">
<br>
<br>
So we expect to get 1 or 6 about 33.33 times.
</p>
<h3>
Analysis of hiring problem using indicator random variables
</h3>
<p>
The first candidate we always hire. The second candidate will
be better half the time, the third one will be better than the
first two one third of the time, and so on, so we get the
series:
<br>
<em>1 + 1/2 + 1/3 + 1/4...</em>
<br>
(Or <em>y = 1/x</em>.)
<br>
<br>
So:
<br>
<br>
<img
src=
"graphics/RandEq1.gif">
<br>
<br>
<img
src=
"graphics/RandEq2.gif">
<br>
<br>
<img
src=
"graphics/RandEq3.gif">
<br>
(By linearity of expectations.)
<br>
<br>
<img
src=
"graphics/RandEq4.gif">
<br>
<br>
<img
src=
"graphics/RandEq5.gif">
<br>
<br>
Why is the last step true? <a
href="https://www.khanacademy.org/math/ap-calculus-ab/indefinite-integrals-ab/indefinite-integrals-of-common-functions-ab/v/antiderivative-of-x-1">
Video here.</a>
<br>
<br>
Basically, the anti-derivative of <em>1/x</em> is ln <em>x</em>:
<br>
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/Integral_Test.svg/250px-Integral_Test.svg.png">
</p>
<p>
And what is that <em>O(1)</em> about?
<br>It means that the sum - ln n is always bounded by a constant.
</p>
<h2>
Randomized algorithms
</h2>
<p>
</p>
<h3>
Randomly permuting arrays
</h3>
<p>
</p>
<h2>
Probabilistic analysis and further uses of indicator random
variables
</h2>
<h3 id="birthday">
The birthday paradox
</h3>
<p>
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Birthday_candles.jpg/250px-Birthday_candles.jpg">
<br>
<br>
<br>
Probability that two people have the same birthday: 1/365
<br>
<br>
To generalize the problem, and see it doesn't just apply to
birthdays, let's call the # of days in the year <em>n</em>.
<br>
<br>
Let's set up an indicator random variable so that
<em>X<sub>ij</sub></em> is:
1 if persons i and j have the same birthday.
<br>
0 if the don't.
<br>
<br>
Let <em>X</em> count the number of pairs of people who
have the same birthday.
<br> <br>
<em>k</em> is the number of people in the room.
<br> <br>
<img
src=
"graphics/RandEq6.gif">
<br> <br>
Take expectations and apply linearity of expectation:
<br> <br>
<img
src=
"graphics/RandEq7.gif">
<br> <br>
<img
src=
"graphics/RandEq8.gif">
<br> <br>
<img
src=
"graphics/RandEq9.gif">
<br> <br>
<img
src=
"graphics/RandEq10.gif">
<br>
<br>
So we are looking at <em>Θ(n<sup>1/2</sup>)</em>,
asymptotically speaking.
</p>
<h3>
Balls and bins
</h3>
<h3>
Streaks
</h3>
<h3>
The on-line hiring problem
</h3>
<p>
</p>
</body>
</html>