What's an algorithm? - David J. Malan

人腦也可以執行演算法 - David J. Malan

2,570,249 views ・ 2013-05-20

TED-Ed


請雙擊下方英文字幕播放視頻。

00:00
Translator: Andrea McDonough Reviewer: Jessica Ruby
0
0
7000
譯者: Jephian Lin 審譯者: Michelle Ho
00:15
What's an algorithm?
1
15483
1348
演算法(algorithm) 是什麼?
00:16
In computer science,
2
16831
831
在電腦科學裡,
00:17
an algorithm is a set of instructions
3
17662
1754
演算法指的是一組 一步接著一步、
00:19
for solving some problem, step-by-step.
4
19416
2689
用來解決問題的指令。
00:22
Typically, algorithms are executed by computers,
5
22105
2272
通常,演算法是電腦在執行的,
00:24
but we humans have algorithms as well.
6
24377
2167
但是我們人類也是可以有演算法。
00:26
For instance, how would you go about counting
7
26544
1853
比方說,你會怎麼去數
00:28
the number of people in a room?
8
28397
1820
一個房間裡的人數?
00:30
Well, if you're like me,
9
30217
1215
嗯,如果你跟我一樣,
00:31
you probably point at each person,
10
31432
1496
也許你會指著每個人,
00:32
one at a time,
11
32928
960
一次一個,
00:33
and count up from 0:
12
33888
1837
然後從 0 開始:
00:35
1, 2, 3, 4 and so forth.
13
35725
2873
1、2、3、4…… 一直下去。
00:38
Well, that's an algorithm.
14
38598
1113
這就是個演算法。
00:39
In fact, let's try to express it
15
39711
1134
事實上,我們試著用
00:40
a bit more formally in pseudocode,
16
40845
2229
更正式一點的「虛擬碼」來表示,
00:43
English-like syntax
17
43074
831
00:43
that resembles a programming language.
18
43905
2124
它跟英文語法很像、
同時也很類似程式語言。
00:46
Let n equal 0.
19
46029
1767
令 N = 0。
00:47
For each person in room, set n = n + 1.
20
47796
4792
每指到房間裡的一個人, 就將 N 設為 N + 1。
00:52
How to interpret this pseudocode?
21
52588
1497
這段虛擬碼要怎麼解釋呢?
00:54
Well, line 1 declares, so to speak,
22
54085
1836
第一行是在說
00:55
a variable called n
23
55921
1416
N 是一個變數
00:57
and initializes its value to zero.
24
57337
2379
然後將它設成 0 作準備 (又稱初使化)。
00:59
This just means that at the beginning of our algorithm,
25
59716
2335
這意思是我們的演算法,
01:02
the thing with which we're counting
26
62051
1584
也就是我們的計數
01:03
has a value of zero.
27
63635
1668
會從 0 開始。
01:05
After all, before we start counting,
28
65303
1336
畢竟,在我們開始計數之前,
01:06
we haven't counted anything yet.
29
66639
1669
我們沒有數到任何東西。
01:08
Calling this variable n is just a convention.
30
68308
2248
把這變數叫作 N 只是方便起見。
01:10
I could have called it almost anything.
31
70556
2005
我也可以把它叫作別的名字。
01:12
Now, line 2 demarks the start of loop,
32
72561
2086
現在,第二行設定 「迴圈」的範圍,
01:14
a sequence of steps that will repeat some number of times.
33
74647
3044
它的意思是有幾個步驟 我們會重覆好幾次。
01:17
So, in our example, the step we're taking
34
77691
1794
所以在我們的例子之中, 這個重覆的步驟
01:19
is counting people in the room.
35
79485
1734
就是「數」這房間的人數。
01:21
Beneath line 2 is line 3,
36
81219
1763
第二行下面有第三行,
01:22
which describes exactly how we'll go about counting.
37
82982
2511
它敘述的就是我們要計數的方法。
01:25
The indentation implies that it's line 3
38
85493
2250
前端的縮排表示 要重覆的部份就是第三行。
01:27
that will repeat.
39
87743
1222
前端的縮排表示 要重覆的部份就是第三行。
01:28
So, what the pseudocode is saying
40
88965
1219
所以,整段虛擬碼就是在說
01:30
is that after starting at zero,
41
90184
2066
N 從 0 開始,
01:32
for each person in the room,
42
92250
1710
之後每指到一個人,
01:33
we'll increase n by 1.
43
93960
2218
N 就加 1。
01:36
Now, is this algorithm correct?
44
96178
2329
好了,這演算法正確嗎?
01:38
Well, let's bang on it a bit.
45
98507
1608
我們稍微試一下。
01:40
Does it work if there are 2 people in the room?
46
100115
2826
如果房間裡有兩個人 它是對的嗎?
01:42
Let's see.
47
102941
780
咱們來看看。
01:43
In line 1, we initialize n to zero.
48
103721
2085
第一行裡,我們將 N 初使化為 0。
01:45
For each of these two people,
49
105806
1302
每指到這兩個人的其中一個
01:47
we then increment n by 1.
50
107108
2168
我們就將 N 加 1。
01:49
So, in the first trip through the loop,
51
109276
1582
所以,在迴圈的第一輪裡,
01:50
we update n from zero to 1,
52
110858
2005
我們將 N 變成 1,
01:52
on the second trip through that same loop,
53
112863
1655
而在同個迴圈的第二輪裡,
01:54
we update n from 1 to 2.
54
114518
2249
我們再將 N 從 1 變為 2。
01:56
And so, by this algorithm's end, n is 2,
55
116767
3341
然後演算法就結束了, N 就是 2,
02:00
which indeed matches the number of people in the room.
56
120108
2113
這數字正好符合這房間的人數。
02:02
So far, so good.
57
122221
1223
到目前為止還不錯。
02:03
How about a corner case, though?
58
123444
1752
不過如果看看極端的例子呢?
02:05
Suppose that there are zero people in the room,
59
125196
1964
假設房間裡只有 0 個人,
02:07
besides me, who's doing the counting.
60
127160
2347
當然在算人數的我不算在內。
02:09
In line 1, we again initialize n to zero.
61
129507
3010
第一行裡我們還是將 N 初始化為 0。
02:12
This time, though, line 3 doesn't execute at all
62
132517
2522
但這一次,第三行完全沒有執行,
02:15
since there isn't a person in the room,
63
135039
1880
因為房間裡跟本沒人。
02:16
and so, n remains zero,
64
136919
1624
因此,N 維持是 0,
02:18
which indeed matches the number of people in the room.
65
138543
2359
這確實也符合房間裡的人數。
02:20
Pretty simple, right?
66
140902
906
蠻簡單的,對吧?
02:21
But counting people one a time is pretty inefficient, too, no?
67
141808
3216
但是一次數一個人很沒效率,對吧?
02:25
Surely, we can do better!
68
145024
1568
當然,我們有更好的辦法!
02:26
Why not count two people at a time?
69
146592
1866
何不一次數兩個人呢?
02:28
Instead of counting 1, 2, 3, 4, 5, 6, 7, 8, and so forth,
70
148458
5099
與其算 1、2、3、4、5、6、7、8 等等,
02:33
why not count
71
153557
918
何不試試
02:34
2, 4, 6, 8, and so on?
72
154475
2127
2、4、6、8,然後一直下去?
02:36
It even sounds faster, and it surely is.
73
156602
2418
這聽起來快多了, 而實際上也是。
02:39
Let's express this optimization in pseudocode.
74
159020
2835
讓我們將這項改進 用虛擬碼來表示看看。
02:41
Let n equal zero.
75
161855
1462
令 N = 0。
02:43
For each pair of people in room,
76
163317
1997
每指到房間裡的「一對人」,
02:45
set n = n + 2.
77
165314
2588
就將 N 設為 N + 2。
02:47
Pretty simple change, right?
78
167902
1673
這轉換很簡單,對吧?
02:49
Rather than count people one at a time,
79
169575
1791
與其一次算一個,
02:51
we instead count them two at a time.
80
171366
2343
我們改成一次算兩個。
02:53
This algorithm's thus twice as fast as the last.
81
173709
2815
這個演算法會比上一個快兩倍。
02:56
But is it correct?
82
176524
1346
但是它是對的嗎?
02:57
Let's see.
83
177870
794
來看看。
02:58
Does it work if there are 2 people in the room?
84
178664
2125
如果房間裡有兩個人 它是對的嗎?
03:00
In line 1, we initialize n to zero.
85
180789
2342
第一行裡,我們將 N 初使化為 0。
03:03
For that one pair of people, we then increment n by 2.
86
183131
3268
每指到房內的這對人, 我們就將 N 加 2。
03:06
And so, by this algorithm's end, n is 2,
87
186399
2522
然後演算法就結束了, N 就是 2,
03:08
which indeed matches the number of people in the room.
88
188921
2382
這數字正好符合這房間的人數。
03:11
Suppose next that there are zero people in the room.
89
191303
2412
接著假設房間裡有 0 個人。
03:13
In line 1, we initialize n to zero.
90
193715
2587
第一行裡,我們將 N 初使化為 0。
03:16
As before, line 3 doesn't execute at all
91
196302
2080
而像之前一樣, 第三行完全沒執行,
03:18
since there aren't any pairs of people in the room,
92
198382
2342
因為房間裡根本沒有「一對人」。
03:20
and so, n remains zero,
93
200724
1686
因此 N 維持是 0,
03:22
which indeed matches the number of people in the room.
94
202410
2773
這也符合房間裡的人數。
03:25
But what if there are 3 people in the room?
95
205183
2372
但如果房間裡有 3 個人呢?
03:27
How does this algorithm fair?
96
207555
1937
這演算法會如何?
03:29
Let's see.
97
209492
829
來看看。
03:30
In line 1, we initialize n to zero.
98
210321
2670
第一行裡,我們將 N 初使化為 0。
03:32
For a pair of those people,
99
212991
1290
每指到這裡面的一對人,
03:34
we then increment n by 2,
100
214281
1916
我們就將 N 加 2,
03:36
but then what?
101
216197
992
接著?
03:37
There isn't another full pair of people in the room,
102
217189
2262
房間裡就沒有完整的一對,
03:39
so line 2 no longer applies.
103
219451
2210
所以第二行就不適用了。
03:41
And so, by this algorithm's end,
104
221661
1669
接著演算法結束,
03:43
n is still 2, which isn't correct.
105
223330
2596
N 還是 2,這樣就錯掉了。
03:45
Indeed this algorithm is said to be buggy
106
225926
2332
事實上我們會說 這個演算法有錯誤(bug),
03:48
because it has a mistake.
107
228258
1148
因為裡頭有些問題。
03:49
Let's redress with some new pseudocode.
108
229406
1896
我們再提出新的虛擬碼吧!
03:51
Let n equal zero.
109
231302
1793
令 N = 0。
03:53
For each pair of people in room,
110
233095
2123
每指到房間裡的一對人,
03:55
set n = n + 2.
111
235218
2422
就將 N 設為 N + 2。
03:57
If 1 person remains unpaired,
112
237640
2459
如果還有 1 個人沒辦法成對,
04:00
set n = n + 1.
113
240099
2376
就將 N 設為 N + 1。
04:02
To solve this particular problem,
114
242475
1507
為了解決這個問題,
04:03
we've introduced in line 4 a condition,
115
243982
2249
我們在第四行提出了新的條件,
04:06
otherwise known as a branch,
116
246231
1835
也叫作「分支」,
04:08
that only executes if there is one person
117
248066
2253
它只有在有 1 個人沒有被配對的情況下 才會執行。
04:10
we could not pair with another.
118
250319
1876
它只有在有 1 個人沒有被配對的情況下 才會執行。
04:12
So now, whether there's 1 or 3
119
252195
2065
所以現在,不管房間裡
04:14
or any odd number of people in the room,
120
254260
2273
有 1 個或 3 個人、或是任何奇數個人,
04:16
this algorithm will now count them.
121
256533
2288
這個演算法現在會算到他們了。
04:18
Can we do even better?
122
258821
1345
我們還能做得更好嗎?
04:20
Well, we could count in 3's or 4's or even 5's and 10's,
123
260166
3294
嗯,我們可以一次數 3 個、數 4 個、 甚至是 5 個或 10 個,
04:23
but beyond that it's going to get
124
263460
1300
不過再下去要指出一組人 就會變得有點困難。
04:24
a little bit difficult to point.
125
264760
1870
不過再下去要指出一組人 就會變得有點困難。
04:26
At the end of the day,
126
266630
937
而在最後,
04:27
whether executed by computers or humans,
127
267567
2264
無論是電腦或是人在執行指令,
04:29
algorithms are just a set of instructions
128
269831
1960
演算法就只是一組 用來解決問題的指令。
04:31
with which to solve problems.
129
271791
1838
演算法就只是一組 用來解決問題的指令。
04:33
These were just three.
130
273629
1743
這裡只是三個例子。
04:35
What problem would you solve with an algorithm?
131
275372
2982
而你想要用演算法解決什麼問題呢?
關於本網站

本網站將向您介紹對學習英語有用的 YouTube 視頻。 您將看到來自世界各地的一流教師教授的英語課程。 雙擊每個視頻頁面上顯示的英文字幕,從那裡播放視頻。 字幕與視頻播放同步滾動。 如果您有任何意見或要求,請使用此聯繫表與我們聯繫。

https://forms.gle/WvT1wiN1qDtmnspy7