forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GettingStarted.html
470 lines (425 loc) · 16.8 KB
/
GettingStarted.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
<!DOCTYPE html>
<html>
<!-- THIS FILE WAS GENERATED BY A SCRIPT: DO NOT EDIT IT! -->
<head>
<meta charset="UTF-8">
<link href="style.css" rel="stylesheet" type="text/css"/>
<title>
Design and Analyis of Algorithms: Getting Started
</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 Analyis of Algorithms: Getting Started
</h1>
<details>
<summary class="sum1">
2.1 Insertion Sort
</summary>
<details>
<summary class="sum2">
How Insertion Sort Works
</summary>
<ul>
<li>
<b>Input</b>:
A sequence of <i>n</i> numbers
⟨
<i>a</i><sub>1</sub>,
<i>a</i><sub>2</sub>, . . .
<i>a</i><sub><i>n</i></sub>
⟩
</li>
<li>
<b>Output</b>: A permutation (reordering)
⟨
<i>a</i>'<sub>1</sub>,
<i>a</i>'<sub>2</sub>, . . .
<i>a</i>'<sub><i>n</i></sub>
⟩
such that
<i>a</i>'<sub>1</sub> ≤
<i>a</i>'<sub>2</sub> ≤ . . .
≤ <i>a</i>'<sub><i>n</i></sub>.
</li>
</ul>
<p>
Insertion sort animation:
</p>
<canvas id="canvas" width="800" height="200"
style="border:1px solid #000000"></canvas>
<script src="Algocynfas/fabric.js"></script>
<script src="Algocynfas/common_primitives.js">
</script>
<script src="Algocynfas/list_primitives.js"></script>
<script src="Algocynfas/insertion_sort.js"></script>
<script>
var canvas = new fabric.Canvas('canvas');
var list_to_sort = [10, 1, 56, 34,
97, 88, 23, 12, 5];
sort(list_to_sort)
async function sort(list_to_sort) {
await window.insertionSort(list_to_sort);
}
</script>
<p>
<br />
<b>Pseudo-code</b>:
We employ this instead of "real"
code. It keeps the "code" language independent,
and allows us to express ideas in the simplest way
possible.
</p>
</details>
<details>
<summary class="sum2">
Loop Invariants
</summary>
<p>
A key idea in proving correctness. We must show
the algorithm is correct at:
</p>
<ol>
<li>
<b>Initialization</b>: The invariant is true
before the loop starts.
</li>
<li>
<b>Maintenance</b>: The invariant is true
at the "top" of the loop each time through.
</li>
<li>
<b>Termination</b>: The invariant is true
when the loop terminates.
</li>
</ol>
</details>
</details>
<details>
<summary class="sum1">
2.2 Analyzing algorithms
</summary>
<details>
<summary class="sum2">
Running time
</summary>
<p>
We surely do not want to talk about actual time
in nanoseconds, as that depends on too
many external things: what other
processes are running, details of the
hardware, compiler switches, etc, etc.
So instead we come up with a very
primitive <em>abstract machine</em> (RAM=the
Random Access Machine: CPU + directly
addressable memory) which we analyze instead
of a real computer. Then primitive
operations are memory reads/writes and operations
in the CPU, such as address arithmetic,
additions, multiplications, divisions,
etc. Our idea of "running time" is then
simply the number of operations
performed when the program runs.
</p>
</details>
<details>
<summary class="sum2">
More trouble
</summary>
<p>
Even for inputs of a fixed size
(say, your favorite program
sorting 10 numbers), different specific
inputs will produce different performance.
For example, a clever algorithm may
notice that the input is already sorted
and not try sorting it again. So, we
distinguish between best- and worst-case
performance (minimum and maximum number
of operations performed by the algorithm,
over all possible legal inputs of a given size).
(There is also the concept of
"average-case," but it is tricky,
as it requires a definition of what average
means: technically, you need to specify
a <i>distribution</i> of the inputs.
We will mostly stay away from average-case
analysis and focus on the worst case;
we will occasionally look at the best case too.)
</p>
</details>
<details>
<summary class="sum2">
Generalized statement of running time
</summary>
<p>
What we really want, is not the running time
(as the number of operations,
say, in the worst case), for a specific
input size, but as a function of the
input size, over all sizes. For example,
it may be that a given sorting
algorithm requires <em>4n<sup>2</sup> + 4n + 24</em>
operations to sort <em>n</em> items.
</p>
</details>
<details>
<summary class="sum2">
Asymptotic behavior
</summary>
<p>
In order to make life easier, we will not
be focusing on the exact value of
such function, but on its <i>asymptotic</i> behavior.
</p>
<figure>
<img
src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/Comparison_computational_complexity.svg/250px-Comparison_computational_complexity.svg.png">
</figure>
</details>
<details>
<summary class="sum2">
Highest power of n
</summary>
<p>
In particular, we want to focus on the
highest power of n in any
function expressing the run time
of an algorithm. That is because, as
input size grows, that power will
dwarf all others in significance.
Consider the contribution of <em>n<sup>2</sup></em>
and <em>n</em> in the runtime of the
sorting algorithm mentioned above
for various values of <em>n</em>:
</p>
<table>
<tr>
<th>
n
</th>
<th>
n<sup>2</sup>
</th>
</tr>
<tr>
<td>10</td>
<td>100</td>
</tr>
<tr>
<td>100</td>
<td>10,000</td>
</tr>
<tr>
<td>1000</td>
<td>1,000,000</td>
</tr>
<tr>
<td>10,000</td>
<td>100,000,000</td>
</tr>
<tr>
<td>100,000</td>
<td>10,000,000,000</td>
</tr>
</table>
<p>
Now consider that Amazon is processing
tens of millions of transactions
per day: of what significance is the
<em>n</em> factor in the performance
of their servers, if they are using an algorithm with an
<em>n<sup>2</sup></em> factor involved to sort them?
</p>
</details>
<details>
<summary class="sum2">
Quiz
</summary>
<ol>
<li>
In a theoretical analysis of algorithms, we want to discuss running time in nanoseconds.
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q1" value="a">
False
</li>
<li>
<input type="radio" name="q1" value="b">
True
</li>
</ol>
<li>
We will generally consider:
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q2" value="a">
best-case running time
</li>
<li>
<input type="radio" name="q2" value="b">
worst-case running time
</li>
<li>
<input type="radio" name="q2" value="c">
memory usage
</li>
<li>
<input type="radio" name="q2" value="d">
average-case running time
</li>
</ol>
<li>
We will mostly concern ourselves with an algorithm's behavior
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q3" value="a">
for small inputs
</li>
<li>
<input type="radio" name="q3" value="b">
egregiously
</li>
<li>
<input type="radio" name="q3" value="c">
asymptotically
</li>
<li>
<input type="radio" name="q3" value="d">
exponentially
</li>
</ol>
<li>
In analyzing an algorithm in terms of <i>n</i>, the number of inputs, we would like to focus on:
</li>
<ol type="a" class="nested">
<li>
<input type="radio" name="q4" value="a">
the lowest power of <i>n</i>
</li>
<li>
<input type="radio" name="q4" value="b">
the highest power of <i>n</i>
</li>
<li>
<input type="radio" name="q4" value="c">
the logarithm of <i>n</i>
</li>
<li>
<input type="radio" name="q4" value="d">
all of the above
</li>
</ol>
</ol>
<details>
<summary class="sum3">
Answers
</summary>
<p>
1. a; 2. b; 3. c; 4. b;
</p>
</details>
</details>
</details>
<details>
<summary class="sum1">
Running code in your browser
</summary>
<p>
We will try to provide you with many opportunities to try
out these algorithms as possible.
Here is Python code for a swap routine, which our textbook
uses periodically:
</p>
<pre>
l = [1, 2, 3]
def swap(l, i, j):
temp = l[i]
l[i] = l[j]
l[j] = temp
</pre>
<p>
Paste that code into the Python console below:
</p>
<div class="python-console">
<iframe style="width: 640; height: 480;"
name="embedded_python_anywhere"
src="https://www.pythonanywhere.com/embedded3/" scrolling="yes">
</iframe>
<figcaption>
Python console
</figcaption>
</div>
<p>
Hit return. Then type 'swap(l, 0, 2)' and hit return.
<br />
Then type 'l'.
<br />
<br />
You should see '[3, 2, 1]': you swapped element 0 and
2!
</p>
<p>
Now type in the following:
</p>
<pre>
def bubble_sort(l):
for i in range(0, len(l) - 1):
for j in range(len(l) - 1, i, -1):
if l[j] < l[j - 1]:
print("Swapping " + str(l[j]) + " and "
+ str(l[j - 1]))
swap(l, j, j - 1)
return l
</pre>
</details>
<details>
<summary class="sum1">
Source Code
</summary>
<p>
<a href="https://github.com/gcallah/algorithms/tree/master/Java/GettingStarted">Java</a><br>
<a href="https://github.com/gcallah/algorithms/tree/master/Ruby/GettingStarted">Ruby</a><br>
<a href="https://github.com/gcallah/algorithms/tree/master/Go/GettingStarted">Go</a><br>
<a href="https://github.com/gcallah/algorithms/tree/master/C++/GettingStarted">C++</a><br>
<a href="https://github.com/gcallah/algorithms/tree/master/Python/GettingStarted">Python</a><br>
<a href="https://github.com/gcallah/algorithms/tree/master/Clojure/GettingStarted">Clojure</a><br>
</p>
</details>
<details>
<summary class="sum1">
For Further Study
</summary>
<ul>
</ul>
</details>
<details>
<summary class="sum1">
Homework
</summary>
</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>