forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedAlgorithms.html
More file actions
360 lines (316 loc) · 9.7 KB
/
RandomizedAlgorithms.html
File metadata and controls
360 lines (316 loc) · 9.7 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css"/>
<title>
Randomized Algorithms
</title>
</head>
<body>
<div id="header">
<div id="logo">
<img src="graphics/Julia.png">
</div>
<div id="user-tools">
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="feedback.html">Feedback</a>
</div>
</div>
<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>
<details>
<summary class="sum1">
The hiring problem
</summary>
<details>
<summary class="sum2">
Worst-case analysis
</summary>
<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>
</details>
<details>
<summary class="sum2">
Probabilistic anaysis
</summary>
<p>
We use probability theory to find an <em>average-case running
time</em>.
<br>
<br>
<a href="Probability.html">Probability basics</a>.
</p>
</details>
<details>
<summary class="sum2">
A randomized algorithm
</summary>
<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>
</details>
</details>
<details>
<summary class="sum1">
Indicator random variables
</summary>
<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>
<li>X = 0 if A does not happen</li>
</ul>
<details>
<summary class="sum2">
Useful properties of indicator variables:
</summary>
<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>
</details>
<details>
<summary class="sum2">
Analysis of hiring problem using indicator random variables
</summary>
<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>
</details>
</details>
<details>
<summary class="sum1">
Randomized algorithms
</summary>
<p>
</p>
<details>
<summary class="sum2">
Randomly permuting arrays
</summary>
<p>
</p>
</details>
</details>
<details>
<summary class="sum1">
Probabilistic analysis and further uses of indicator random
variables
</summary>
<details>
<summary class="sum2">
The birthday paradox
</summary>
<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>
</details>
<details>
<summary class="sum2">
Balls and bins
</summary>
</details>
<details>
<summary class="sum2">
Streaks
</summary>
</details>
<details>
<summary class="sum2">
The on-line hiring problem
</summary>
<p>
</p>
</details>
</details>
<details>
<summary class="sum1">
Source Code
</summary>
<p>
</p>
</details>
</body>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-97026578-2', 'auto');
ga('send', 'pageview');
</script>
</html>