字幕表 動画を再生する
What's an algorithm?
アルゴリズムって何?
In computer science,
コンピュータサイエンスでは
an algorithm is a set of instructions
アルゴリズムは命令の集合である
for solving some problem, step-by-step.
問題を解決するために、一歩一歩進んでいきます。
Typically, algorithms are executed by computers,
一般的に、アルゴリズムはコンピュータによって実行されます。
but we humans have algorithms as well.
しかし、私たち人間にもアルゴリズムがあります。
For instance, how would you go about counting
例えば、どのようにして
the number of people in a room?
の人数ですか?
Well, if you're like me,
あなたが私のような人ならね
you probably point at each person,
一人一人を指差しているのではないでしょうか。
one at a time,
一度に一つずつ。
and count up from 0:
と0からカウントアップします。
1, 2, 3, 4 and so forth.
1、2、3、4など。
Well, that's an algorithm.
まあ、それはアルゴリズムですね。
In fact, let's try to express it
実際に表現してみましょう。
a bit more formally in pseudocode,
を疑似コードでもう少し形式的に。
English-like syntax
えいごライクこうぶん
that resembles a programming language.
プログラミング言語に似ている
Let n equal 0.
nを0とします。
For each person in room, set n = n + 1.
部屋にいる各人について、n=n+1とする。
How to interpret this pseudocode?
この疑似コードをどのように解釈すればいいのでしょうか?
Well, line 1 declares, so to speak,
まあ、1行目は、いわば宣言している。
a variable called n
という変数
and initializes its value to zero.
で、その値をゼロに初期化します。
This just means that at the beginning of our algorithm,
これは、私たちのアルゴリズムの最初にあることを意味しています。
the thing with which we're counting
数え物
has a value of zero.
の値はゼロです。
After all, before we start counting,
結局、数え始める前に
we haven't counted anything yet.
まだ何もカウントしていません。
Calling this variable n is just a convention.
この変数 n を呼び出すことは単なる慣例です。
I could have called it almost anything.
ほとんど何とでも呼べました。
Now, line 2 demarks the start of loop,
さて、2行目はループの開始を示しています。
a sequence of steps that will repeat some number of times.
を何度か繰り返すことになります。
So, in our example, the step we're taking
ですから、この例では、私たちが取っているステップは
is counting people in the room.
は部屋の中の人を数えています。
Beneath line 2 is line 3,
2行目の下が3行目です。
which describes exactly how we'll go about counting.
これは、私たちがどのようにカウントするかを正確に説明しています。
The indentation implies that it's line 3
インデントは、それが3行目であることを意味しています。
that will repeat.
を繰り返すことになります。
So, what the pseudocode is saying
つまり、疑似コードが言っていることは
is that after starting at zero,
は、ゼロからスタートした後のことです。
for each person in the room,
一人一人のために
we'll increase n by 1.
nを1ずつ増やしていきます。
Now, is this algorithm correct?
さて、このアルゴリズムは正しいのでしょうか?
Well, let's bang on it a bit.
まあ、ちょっと叩いてみましょう。
Does it work if there are 2 people in the room?
2人いても大丈夫ですか?
Let's see.
見てみましょう。
In line 1, we initialize n to zero.
1行目では、nを0に初期化します。
For each of these two people,
この二人のそれぞれのために
we then increment n by 1.
次に、nを1ずつ増やしていきます。
So, in the first trip through the loop,
だから、最初のループを通る旅では
we update n from zero to 1,
nを0から1に更新します。
on the second trip through that same loop,
同じループを通る2回目の旅で
we update n from 1 to 2.
nを1から2に更新します。
And so, by this algorithm's end, n is 2,
そして、このアルゴリズムの終了時には、nは2になります。
which indeed matches the number of people in the room.
これは確かに部屋の人数と一致している。
So far, so good.
ここまでは順調だ
How about a corner case, though?
でも、コーナーケースはどうなんだろう?
Suppose that there are zero people in the room,
部屋の中に人が0人いるとします。
besides me, who's doing the counting.
私の他に誰が数えているのかな?
In line 1, we again initialize n to zero.
1行目では、再びnを0に初期化します。
This time, though, line 3 doesn't execute at all
しかし、今回は3行目が全く実行されません。
since there isn't a person in the room,
部屋に人がいないので
and so, n remains zero,
となりますので、nは0のままです。
which indeed matches the number of people in the room.
これは確かに部屋の人数と一致している。
Pretty simple, right?
簡単だろ?
But counting people one a time is pretty inefficient, too, no?
でも一人ずつ数えるのもかなり非効率だよね?
Surely, we can do better!
確かに、私たちはもっと良いことができます!
Why not count two people at a time?
2人ずつ数えてみてはいかがでしょうか?
Instead of counting 1, 2, 3, 4, 5, 6, 7, 8, and so forth,
1,2,3,4,5,6,7,8などと数えるのではなく。
why not count
数えてはいけない
2, 4, 6, 8, and so on?
2,4,6,8,とか?
It even sounds faster, and it surely is.
速く聞こえることさえあるし、確かにそうですね。
Let's express this optimization in pseudocode.
この最適化を疑似コードで表現してみましょう。
Let n equal zero.
nは0に等しいとしましょう。
For each pair of people in room,
部屋にいる各組の人のために
set n = n + 2.
n=n+2とします。
Pretty simple change, right?
かなり単純な変化でしょ?
Rather than count people one at a time,
人を一人ずつ数えるのではなく
we instead count them two at a time.
一度に2つ数えるのではなく、2つずつ数える。
This algorithm's thus twice as fast as the last.
このアルゴリズムは前のアルゴリズムに比べて2倍の速度を実現しています。
But is it correct?
しかし、それは正しいのでしょうか?
Let's see.
見てみましょう。
Does it work if there are 2 people in the room?
2人いても大丈夫ですか?
In line 1, we initialize n to zero.
1行目では、nを0に初期化します。
For that one pair of people, we then increment n by 2.
その1組の人に対して、nを2ずつ増やしていきます。
And so, by this algorithm's end, n is 2,
そして、このアルゴリズムの終了時には、nは2になります。
which indeed matches the number of people in the room.
これは確かに部屋の人数と一致している。
Suppose next that there are zero people in the room.
次に、部屋の中に人が0人いたとします。
In line 1, we initialize n to zero.
1行目では、nを0に初期化します。
As before, line 3 doesn't execute at all
前と同じように、3行目は全く実行されません。
since there aren't any pairs of people in the room,
部屋にはペアの人がいないので
and so, n remains zero,
となりますので、nは0のままです。
which indeed matches the number of people in the room.
これは確かに部屋の人数と一致している。
But what if there are 3 people in the room?
でも、3人いたらどうするの?
How does this algorithm fair?
このアルゴリズムはどのように公平なのでしょうか?
Let's see.
見てみましょう。
In line 1, we initialize n to zero.
1行目では、nを0に初期化します。
For a pair of those people,
その人たちのペアのために
we then increment n by 2,
次に、nを2ずつ増やしていきます。
but then what?
然し何だ?
There isn't another full pair of people in the room,
部屋にはもう一組の人がいません。
so line 2 no longer applies.
ということで、2行目は適用されなくなりました。
And so, by this algorithm's end,
それで、このアルゴリズムの終了によって
n is still 2, which isn't correct.
nは2のままですが、これは正しくありません。
Indeed this algorithm is said to be buggy
実際、このアルゴリズムはバグが多いと言われています。
because it has a mistake.
というのは、間違いがあるからです。
Let's redress with some new pseudocode.
新しい疑似コードで解決しましょう。
Let n equal zero.
nは0に等しいとしましょう。
For each pair of people in room,
部屋にいる各組の人のために
set n = n + 2.
n=n+2とします。
If 1 person remains unpaired,
1人がペアリングされていないままの場合
set n = n + 1.
n=n+1とします。
To solve this particular problem,
この特殊な問題を解決するために
we've introduced in line 4 a condition,
4行目に条件を導入しました。
otherwise known as a branch,
そうでなければブランチとして知られています。
that only executes if there is one person
一人だけ
we could not pair with another.
他の人とは組めませんでした。
So now, whether there's 1 or 3
だから今は、1があろうと3があろうと
or any odd number of people in the room,
または奇数人でも良い。
this algorithm will now count them.
このアルゴリズムはそれらをカウントするようになりました。
Can we do even better?
もっといいものができるのかな?
Well, we could count in 3's or 4's or even 5's and 10's,
まあ、3ལsとか4ལsとか5ལsとか10ལsとか数えてもいいんですけどね。
but beyond that it's going to get
その先にあるのは
a little bit difficult to point.
ちょっと指しにくい
At the end of the day,
一日の終わりに
whether executed by computers or humans,
コンピュータが実行しても人間が実行しても
algorithms are just a set of instructions
アルゴリズムは命令の集合
with which to solve problems.
問題を解決するための
These were just three.
この3つだけでした。
What problem would you solve with an algorithm?
アルゴリズムを使ってどんな問題を解決するのか?