-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathalgorithms.html
520 lines (508 loc) · 17.6 KB
/
algorithms.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Algorithms</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/reveal.css">
<link id="theme" rel="stylesheet" href="css/theme/black.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/monokai.css">
<script src="js/diagrams.js"></script>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="center">
<h1>Algorithms</h1>
</section>
<section>
<section>
<h2>Motivation</h2>
<h5>Why we need algorithms, and what they are.</h5>
</section>
<section>
<h3>Algorithms: Why</h3>
<p>Once we solve a given problem, how can we tell people about our solution?</p>
<p class="fragment">
Consider baking; you transmit recipes, detailing how much of ingredient $x$ at what time.
</p>
<p class="fragment">
Consider assembling furniture; you send blueprints for each step, including where screw $y$ goes and
when it should be installed.
</p>
<p class="fragment">
Similarly to these scenarios, when we solve a computational problem, we want to transmit the solution to
others who may make use of it. In computer science, our recipes for success are called <em>algorithms</em>
</p>
</section>
<section>
<h3>Algorithms: How</h3>
<p>
To convey algorithms to others, we use <em>pseudocode</em>, which is intended to be read by humans, not
executed by computers.
</p>
<p class="fragment">
In this class, most pseudocode will bear a striking resemblance to Python, a simple,
high-level, interpreted language
</p>
<p class="fragment">
<pre class="fragment"><code class="python" data-trim="">
def algorithm(x: type, y: type) -> output:
step 1
step 2
step 3
</code></pre>
</p>
</section>
<section>
<h3>Algorithms: Implementing</h3>
<p>We implement algorithms as functions.</p>
</section>
<section>
<h3>Conclusions</h3>
<p>In this class we will study the creation, implementation, and analysis of algorithms extensively.</p>
<p class="fragment">
Some questions we will answer:
<ul>
<li class="fragment">How fast can we sort things?</li>
<li class="fragment">How fast can we search for substrings in a given text?</li>
<li class="fragment">How fast can we determine if an element is present in a given collection?</li>
</ul>
</p>
</section>
</section>
<section>
<section class="center">
<h2>Searching</h2>
<h5>How to find things</h5>
</section>
<section>
<h3>Searching: Unordered Arrays of Integers</h3>
<p class="fragment">Problem: Given an unordered array of elements, how can you tell if $x \in A$</p>
<p class="fragment">Solution: Traverse the entirety of the array, and check each element.</p>
</section>
<section>
<h3>Searching: Ordered Arrays of Integers</h3>
<p class="fragment">Problem: Assume $A$ is now sorted, create an algorithm to tell if $x \in A'$</p>
<p class="fragment">
<pre>
<code class="python" data-trim>
def binary_search(x: int, A: List[int]) -> bool:
if A == []:
return False
elif A[len(A) // 2] == x:
return True
elif A[len(A) // 2] < x:
return binary_search(x, A[len(A) // 2:])
else:
return binary_search(x, A[:len(A) // 2])
</code>
</pre>
</p>
</section>
<section>
<h3>Comparing our two approaches</h3>
<p>From our previous two approaches, which do you think is faster? Why?</p>
<p class="fragment">Linear search has to look at all $n$ elements.</p>
<p class="fragment">Binary search cuts the search space in half at each call</p>
<p class="fragment">How many elements must ordered search examine?</p>
<p class="fragment">$\log_2 n$</p>
<p class="fragment">
<strong>Which would you prefer? $n$ or $\log_2 n$</strong>
</p>
</section>
</section>
<section>
<section>
<h2>Algorithm Analysis</h2>
<h5>How to prove one algorithm is faster than another</h5>
</section>
<section>
<h3>Motivation</h3>
<p>We need some way to decide which algorithms are better than others</p>
<p class="fragment">What are some ways we can <em>measure runtimes?</em></p>
<ul>
<li class="fragment">Empirically</li>
<li class="fragment">Analytically</li>
</ul>
<aside class="notes">
empirically is like using a stopwatch
analytically is analyzing mathematical operations to create a function of runtime over all inputs.
</aside>
</section>
<section>
<h3>Empirical Analysis</h3>
<p>The stopwatch approach</p>
<p class="fragment">Vary your inputs, timing each execution, then plot the results.</p>
<p class="fragment column">
Pros:
<ul>
<li class="fragment">Easy</li>
<li class="fragment">Quick</li>
<li class="fragment">Good for comparing similar algorithms</li>
</ul>
</p>
<p class="fragment column">
Cons:
<ul>
<li class="fragment">Hardware dependant</li>
<li class="fragment">Doesn't Generalize</li>
<li class="fragment">Have to implement the algorithm</li>
<li class="fragment">Can take months to exhaustively time</li>
<li class="fragment">Need input data</li>
</ul>
</p>
</section>
<section>
<h3>Asymptotic Analysis</h3>
<p>Framing runtime as a function of an algorithms' inputs</p>
<p class="fragment">How? <span class="fragment">Counting basic operations</span></p>
<pre class="fragment"><code class="python" data-trim>
def search(A: List[int], key: int) -> bool:
for element in A:
if element == key:
return True
return False
</code></pre>
<p class="fragment">For each of the $n$ elements, search performs 1 operation, so $T(n) = n$</p>
</section>
<section>
<h3>Asymptotic Analysis: Bounds</h3>
<p>There are two primary ways to describe an algorithms asymptotic performance</p>
<p class="fragment">
<ul>
<li>Upper Bound: the algorithm can do no <strong>worse</strong> than this</li>
<li>Lower Bound: the algorithm can do no <strong>better</strong> than this</li>
</ul>
</p>
</section>
<section>
<h3>Big-Oh, Omega, and Theta</h3>
<ul>
<li class="fragment">
$O(?)$
<ul>
<li>Pronounced "Big-Oh"</li>
<li>Represents the upper bound</li>
<li>May not always be <em>tight</em></li>
</ul>
</li>
<li class="fragment">
$\Omega(?)$
<ul>
<li>Pronounced "Big-Omega"</li>
<li>Represents the lower bound</li>
<li>May not always be <em>tight</em></li>
</ul>
</li>
<li class="fragment">
$\Theta(?)$
<ul>
<li>Pronounced "Big-Theta"</li>
<li>Used when $O(T(n)) == \Omega(T(n))$ for a given algorithm</li>
<li>Is the strongest promise, since the upper and lower bounds must be equal</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>$O(?)$, $\Omega(?)$, $\Theta(?)$</h3>
<pre><code class="python" data-trim>
def f1(A: List[int], sum: int) -> bool:
for x in A:
for y in A:
if x + y == sum:
return True
return False
</code></pre>
<p class="fragment">$O(n^2), \Omega(1)$</p>
</section>
<section>
<h3>$O(?)$, $\Omega(?)$, $\Theta(?)$</h3>
<pre><code class="python" data-trim>
def f2(x: int) -> int:
product = 1
for i in range(1, x):
product *= i
return product
</code></pre>
<p class="fragment">$O(n), \Omega(n), \Theta(n)$</p>
</section>
<section>
<h3>Visual Growth</h3>
<canvas id="visualGrowth"></canvas>
</section>
<section>
<h3>Dominant Terms</h3>
<p>
Asymptotic analysis can be thought of as a limit problem.
$$O(kn^2) \rightarrow \lim_{n \rightarrow \infty} O(kn^2) = O(n^2)$$
As $n \rightarrow \infty$, we know that $k$ becomes irrelevant
</p>
<p class="fragment">Thus we will normally discuss "classes" of algorithms</p>
</section>
<section>
<h3>Ordering Algorithms by Speed</h3>
<table>
<thead>
<th>Nodation</th>
<th>Name</th>
</thead>
<tbody>
<tr>
<td>$O(1)$</td>
<td>Constant Time</td>
</tr>
<tr>
<td>$O(\log n)$</td>
<td>Logarithmic</td>
</tr>
<tr>
<td>$O(n)$</td>
<td>Linear</td>
</tr>
<tr>
<td>$O(n \cdot \log n)$</td>
<td>Linearithmic</td>
</tr>
<tr>
<td>$O(n^c)$</td>
<td>Polynomial</td>
</tr>
<tr>
<td>$O(c^n)$</td>
<td>Exponential</td>
</tr>
<tr>
<td>$O(n!)$</td>
<td>Factorial</td>
</tr>
</tbody>
</table>
</section>
</section>
<section>
<section>
<h2>Recursion</h2>
<h5>How we can analyze the runtime of recursive programs</h5>
</section>
<section>
<h3>Recursive Problem Solving</h3>
<p>Breaking down a large problem into a smaller one.</p>
<pre><code class="python" data-trim>
def sum(A: List[int]) -> int:
sum = 0
for element in A:
sum += element
return sum
def sum(A: List[int]) -> int:
if A == []: return 0
else: return A[0] + sum(A[1:])
</code></pre>
<p class="fragment">Base Case; Recursive Call</p>
</section>
<section>
<h3>Select the best base case</h3>
<pre><code class="python" data-trim>
def sum(A: List[int]) -> int:
if len(A) == 2: return A[0] + A[1]
else: return A[0] + sum(A[1:])
def sum(A: List[int]) -> int:
if len(A) == 1: return A[0]
else: return A[0] + sum(A[1:])
def sum(A: List[int]) -> int:
if len(A) == 0: return A[0]
else: return A[0] + sum(A[1:])
</code></pre>
</section>
<section>
<h3>Recursive Definitions</h3>
<pre><code class="python" data-trim>
def factorial(n: int) -> int:
if n <= 1: return 1
else: return n * fact(n - 1)
</code></pre>
<p>Factorial of n = $$n! = (n - 1)! \cdot n, \forall n > 1; 1! = 0! = 1$$</p>
</section>
<section>
<h3>Recurrence Relations</h3>
<p>Factorial of n = $$T(n) = T(n - 1) + 1; T(0) = T(1) = 1$$</p>
<p class="fragment">Again, we specify the base case, by saying the time to compute $n = 0$ or $n = 1$ takes 1
operation.</p>
</section>
<section>
<h3>Unrolling a Recurrence Relation</h3>
<p>
$$
\begin{align}
T(n) &= T(n - 1) + 1; T(0) = T(1) = 1 \\
T(n - 1) &= T(n - 2) + 1 \\
T(n) &= (T(n - 2) + 1) + 1 \\
T(n - 2) &= T(n - 3) + 1 \\
T(n) &= ((T(n - 3) + 1) + 1) + 1 \Rightarrow T(n - 3) + 3 \\
&= T(n - k) + k \textit{ (Substitute k)} \\
n - k &= 0 \textit{ (Solve the base case)} \\
n &= k \\
T(n) &= T(n - n) + n \Rightarrow n \Rightarrow O(n)
\end{align}
$$
</p>
</section>
<section>
<h3>Unrolling a Recurrence Relation: 2</h3>
<p>
$$
\begin{align}
T(n) &= n + T(n - 1) \\
&= n + (n - 1 + T(n - 2)) \\
&= n + (n - 1 + (n - 2 + T(n - 3))) \\
&= n + (n - 1 + (n - 2 + (n - 3 + T(n - 4)))) \\
&= n + (n - 1 + (n - 2 + (n - 3 + (n - 4 + (... + 1)))) \\
&= \sum_{i = 1}^n i \Rightarrow \frac{n(n + 1)}{2}
\end{align}
$$
</p>
</section>
<section>
<h3>Unrolling a Recurrence Relation: 3</h3>
<p>
\begin{align}
T(n) &= 2T(n - 1) + 3; T(1) = 1 \\
&= 2(2T(n - 2) + 3) + 3 \\
&= 2(2(2T(n - 3) + 3) + 3) + 3 \\
&= 2(2^2 T(n - 3) + 2 \cdot 3 + 3) + 3 \\
&= 2^3 T(n - 3) + 2^2 \cdot 3 + 2 \cdot 3 + 3 \\
&= 2^k T(n - k) + \sum_{i = 0}^{k - 1} 2^i \cdot 3 \\
T(n) &= 2^{n - 1} + \sum_{i = 0}^{n - 2} 2^i \cdot 3 \\
T(n) &= 2^{n - 1} + 3 (2^{n - 1} - 1) = O(2^n)
\end{align}
</p>
</section>
<section>
<h3>Summation Identities</h3>
</section>
</section>
</div>
</div>
<script src="js/reveal.js">
</script>
<script>
Reveal.initialize({
dependencies: [{
src: 'plugin/markdown/marked.js'
},
{
src: 'plugin/markdown/markdown.js'
},
{
src: 'plugin/notes/notes.js',
async: true
},
{
src: 'plugin/highlight/highlight.js',
async: true
},
{
src: 'plugin/math/math.js',
async: true
},
{
src: 'plugin/menu/menu.js',
async: true
},
{
src: 'plugin/chalkboard/chalkboard.js',
async: true
}
],
hash: true,
center: false,
fragmentInURL: true,
keyboard: {
// toggle notes canvas when 'c' is pressed
67: function () {
RevealChalkboard.toggleNotesCanvas()
},
// toggle chalkboard when 'b' is pressed
66: function () {
RevealChalkboard.toggleChalkboard()
},
// clear chalkboard when 'DEL' is pressed
46: function () {
RevealChalkboard.clear()
},
// reset chalkboard data on current slide when 'BACKSPACE' is pressed
8: function () {
RevealChalkboard.reset()
},
// download recorded chalkboard drawing when 'd' is pressed
68: function () {
RevealChalkboard.download()
},
},
chalkboard: {
toggleChalkboardButton: {
left: "80px"
},
toggleNotesButton: {
left: "130px"
},
color: ['rgba(255, 255, 255, 1)', 'rgba(255, 255, 255, 1)']
},
menu: {
titleSelector: 'h2',
hideMissingTitles: true,
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"
integrity="sha256-xKeoJ50pzbUGkpQxDYHD7o7hxe0LaOGeguUidbq6vis=" crossorigin="anonymous"></script>
<script>
var range = [...Array(10).keys()];
var ctx = document.getElementById("visualGrowth");
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: range,
datasets: [{
data: range.map(x => 5 * x),
label: "x",
backgroundColor: "#ffffff",
borderColor: "#ffffff",
fill: false,
},
{
data: range.map(x => x ** 2),
label: "x^2",
backgroundColor: "#ffffff",
borderColor: "#ffffff",
fill: false,
}
]
},
options: {
scales: {
xAxes: [{
// type: 'linear',
// ticks: {
// // stepSize: 10,
// }
}]
}
}
});
</script>
</body>
</html>