forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrowthOfFunctions.html
More file actions
567 lines (537 loc) · 17.9 KB
/
Copy pathGrowthOfFunctions.html
File metadata and controls
567 lines (537 loc) · 17.9 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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css"/>
<title>
Design and Analysis of Algorithms: Growth of Functions
</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>
Design and Analysis of Algorithms: Growth of Functions
</h1>
<details>
<summary class="sum1">
A review of Big-O, summations and algebra.
</summary>
<details>
<summary class="sum2">
Different asymptotic notations
</summary>
<details>
<summary class="sum3">
Big O
</summary>
<figure class="left">
<img src="https://cdn.kastatic.org/ka-cs-algorithms/O_fn.png">
</figure>
<table>
<tr>
<th>
Notation
</th>
<th>
Intuition
</th>
<th>
Definition
</th>
</tr>
<tr>
<td>
<em>g(n) = O(f(n))</em>
</td>
<td>
<em>g</em> is bounded above by <em>f</em>
(up to constant factor) asymptotically
</td>
<td>
<em>g(n) <= k*f(n)</em> for some
positive <em>k</em>,
after some <em>n</em>
</td>
</tr>
</table>
</details>
<details>
<summary class="sum3">
Big Θ
</summary>
<figure class="left">
<img src="https://cdn.kastatic.org/ka-cs-algorithms/theta_fn.png">
</figure>
<table>
<tr>
<th>
Notation
</th>
<th>
Intuition
</th>
<th>
Definition
</th>
</tr>
<tr>
<td>
<em>g(n) = Θ(f(n))</em>
</td>
<td>
<em>g</em> is bounded above and below
by <em>f</em> asymptotically
</td>
<td>
<em>k<sub>2</sub>*f(n) <=
g(n) <= k<sub>2</sub>*f(n)</em>
for some positive
<em>k<sub>1</sub></em>,
<em>k<sub>2</sub></em>
</td>
</tr>
</table>
</details>
<details>
<summary class="sum3">
Big Ω
</summary>
<figure class="left">
<img src="https://cdn.kastatic.org/ka-cs-algorithms/Omega_fn.png">
</figure>
<table>
<tr>
<th>
Notation
</th>
<th>
Intuition
</th>
<th>
Definition
</th>
</tr>
<tr>
<td>
<em>g(n) = Ω(f(n))</em>
</td>
<td>
<em>g</em> is bounded below by <em>f</em>
asymptotically
</td>
<td>
<em>g(n) >= k*f(n)</em>
for some positive <em>k</em>
</td>
</tr>
</table>
</details>
<details>
<summary class="sum3">
Little o
</summary>
<table>
<tr>
<th>
Notation
</th>
<th>
Intuition
</th>
<th>
Definition
</th>
</tr>
<tr>
<td>
<em>g(n) = o(f(n))</em>
</td>
<td>
<em>g</em> is dominated by <em>f</em>
asymptotically
</td>
<td>
<em>g(n) < k*f(n)</em> for
every positive <em>k</em>
</td>
</tr>
</table>
</details>
<details>
<summary class="sum3">
Little ω
</summary>
<table>
<tr>
<th>
Notation
</th>
<th>
Intuition
</th>
<th>
Definition
</th>
</tr>
<tr>
<td>
<em>g(n) = ω(f(n))</em>
</td>
<td>
<em>g</em> dominates <em>f</em>
asymptotically
</td>
<td>
<em>g(n) > k*f(n)</em> for
every positive <em>k</em>
</td>
</tr>
</table>
</details>
</details>
<details>
<summary class="sum2">
Why have different notations?
</summary>
<p>
Why not just Big-Θ?
</p>
<p>
From the Khan Academy site:<br>
<blockquote>
We use big-Θ notation to asymptotically bound the
growth of a running time to
within constant factors above and below.
Sometimes we want to bound from only
above. For example, although the worst-case
running time of binary search is Θ(lg n),
it would be incorrect to say that binary search runs in
Θ(lg n) time in all cases.
What if we find the target value upon the first
guess? Then it runs in Θ(1) time.
The running time of binary search is never worse than
Θ(lg n), but it's sometimes better.
It would be convenient to have a form of
asymptotic notation that means
"the running time grows at most this much, but
it could grow more slowly."
We use "big-O" notation for just such occasions.
</blockquote>
</p>
</details>
<details>
<summary class="sum2">
Highest-order term
</summary>
<p>
Graphs and a table on why we only care about the highest-order
</p>
<p>
<img
src="https://cdn.kastatic.org/ka-cs-algorithms/6n2_vs_100n%2B300.png"
height="200" width="300">
<br>
<br>
<img
src="https://cdn.kastatic.org/ka-cs-algorithms/0.6n2_vs_1000n%2B3000.png"
height="200" width="300">
</p>
<table>
<tr>
<th>
<em>n</em>
</th>
<th>
Fast Alpha machine,
<br />
running C code
</th>
<th>
TRA-80
<br />
running interpreted Basic
</th>
</tr>
<tr>
<td>
10
</td>
<td>
.6 microsecs.
</td>
<td>
200 millisecs.
</td>
</tr>
<tr>
<td>
100
</td>
<td>
.6 millisecs.
</td>
<td>
2 secs.
</td>
</tr>
<tr>
<td>
1000
</td>
<td>
.6 secs.
</td>
<td>
20 secs.
</td>
</tr>
<tr>
<td>
10,000
</td>
<td>
10 mins.
</td>
<td>
3.2 mins.
</td>
</tr>
<tr>
<td>
100,000
</td>
<td>
7 days
</td>
<td>
32 mins.
</td>
</tr>
<tr>
<td>
1,000,000
</td>
<td>
19 years
</td>
<td>
5.4 hours
</td>
</tr>
</table>
<p>
(Table from Jon Bentley, <em>Programming Pearls</em>.)
</p>
</details>
<details>
<summary class="sum2">
Meaning of constants in O, Θ and Ω expressions
</summary>
<p>
Let's say we posit a search operating in <em>2n + 3</em> time.<br>
We want to show that this has Θ(n) complexity.<br>
We must find <em>k<sub>1</sub></em>, <em>k<sub>2</sub></em>,
and <em>n<sub>0</sub></em>, so that:
<em>k<sub>1</sub> * n < 2n + 3 < k<sub>2</sub> * n</em>
<br>
One solution is <em>k<sub>1</sub> = 1</em>, <em>k<sub>2</sub> = 3</em>,
and <em>n<sub>0</sub> = 4</em>.
<br>
Let's demonstrate!
</p>
</details>
</details>
<details>
<summary class="sum1">
Algorithmic design in action
</summary>
<details>
<summary class="sum2">
Fibonacci numbers
</summary>
<p>
The problem of computing, given <em>n >= 0</em>,
the <em>n</em><sup>th</sup> Fibonacci number <em>F<sub>n</sub></em>.
</p>
<p>
The Fibonacci discussion is not in the textbook.
One place where it is presented in a nice way similar to what I will do in class is in
section 0.2 of the DasGupta, Papadimitriou, Vazirani <em>Algorithms</em> book.
</p>
<p>
Talked about the obvious recursive algorithm;
how it is very slow, intuitively due to recomputing the same subproblems.
Proved that the recursion tree grows at least as fast as the Fibonacci sequence itself.
</p>
<h4>Sketch of Proof</h4>
<p>
Note that every leaf of the recursion tree returns one. The sum of the value
of all of these leaves is going to be the Fibonacci number we return.
Therefore, there must be at least as many operations as the Fibonacci number
itself.
</p>
<p>
Showed how to speed the recursive algorithm algorithm up by "memoization":
Using a table to store answers for subproblems already computed.
Analyzed the running time and space of the recursive memoized version and of
the iterative algorithm that fills the table going in the forward direction.
Pointed out how to reduce space to constant.
</p>
<p>
<a
href="https://github.com/gcallah/algorithms/blob/master/Python/DivideAndConquer/fibonacci.py">
Naive and faster Fibonacci code.</a>
</p>
<p>
By the way, this is an open-source project: you may contribute!
</p>
<p>
There is a closed-form formula for computing Fibonacci numbers.
</p>
<p>
So why can't we claim we can compute <em>n</em>th Fibonacci number in constant time?
Did not go into details, but this is related to our inability to directly represent
irrational numbers such as square roots, maybe related to precision/rounding,
and generally related to what operations are allowed and what are not, in an algorithm.
</p>
<p>
Using some tricks one can construct <em>n</em>th Fibonacci number
in <em>O(log n)</em> arithmetic operations, where we count additions/subtractions/multiplications
and do not worry about the fact that eventually the numbers get too large to manipulate
in constant time.
</p>
</details>
</details>
<details>
<summary class="sum1">
Data structures
</summary>
<p>
Data structures form a very important part of algorithmic research.
</p>
<p>
However, this course does not focus on data structures.
We will only devote a couple of lectures, total, to this subject.
(There are algorithms courses that spend their entire time on data structures.
We have an advanced data structures expert in the department, Prof. John Iacono.)
</p>
<ul>A quick reminder of basic data structures:
<li>
Arrays
</li>
<li>
Linked list
<br><img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Singly-linked-list.svg/408px-Singly-linked-list.svg.png">
</li>
<li>
Doubly-linked list
<br>
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Doubly-linked-list.svg/610px-Doubly-linked-list.svg.png">
</li>
<li>Stacks
<br>
<img
src="https://upload.wikimedia.org/wikipedia/commons/b/b4/Lifo_stack.png">
</li>
<li>
Queues
<br>
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/405px-Data_Queue.svg.png">
</li>
<li>
Double-ended queues
</li>
<li>
ADT (<i>abstract data type</i>):
<br>the set of operations a data structure promises to provide
and the rules that they have to obey.
</li>
</ul>
<details>
<summary class="sum2">
Detailed case study: Iterables as an ADT.
</summary>
<details>
<summary class="sum3">
Basic operations.
</summary>
<ul>
<li>Get iterator.</li>
<li>Get next value.</li>
<li>Detect end of iterables.</li>
</ul>
</details>
<details>
<summary class="sum3">
Extended operations.
</summary>
<ul>
<li>
Rewind? We may not be able to if the
iterator is exhausted in iterating
through it.
</li>
<li>
Iterate in reverse?
</li>
<li>
Access an arbitrary element? (By index, say.)
</li>
</ul>
</details>
<details>
<summary class="sum3">
Implementations.
</summary>
<ul>
<li>Straight-forward: Array.
</li>
<li>Complex:<a
<a href="https://github.com/gcallah/Indra/blob/master/indra/agent_pop.py">
A population that can be iterated in many ways.
</a>
</li>
</ul>
</details>
</details>
</details>
<details>
<summary class="sum1">
Homework
</summary>
<p>
<ul>
<li>
</li>
<li>
</li>
<li>
</li>
</ul>
</p>
</details>
<a name="note1">* Based on Prof. Boris Aronov's lecture notes. </a>
<br>
<a name="note2">** Material drawn from Khan Academy.</a>
</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>