Algorithms, traces and correctness evidence
導入済み
4:48
計算的思考
あなたの学校はcanteenアプリを求めている。生徒はスマホで昼食を注文し、カード残高から支払いを行い、キッチンが注文を確認する。これらを一度に記述することはできない……
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| Binary search/ˈbaɪnəri sɜːtʃ/ | |
| algorithm/ˈælɡərɪθəm/ | アルゴリズム |
| trace/treɪs/ | to follow an algorithm step by step, recording the values of its variables |
| boundary/ˈbaʊndəri/ | A line, surface or limit that separates regions or marks the extent of an area or set. |
| linear search/ˈlɪnɪə sɜːtʃ/ | Checking items in sequence until the required item is found or the list ends. |
| trace table/treɪs ˈteɪbl/ | A table recording variable values, conditions or outputs while an algorithm is followed step by step. |
| boundary test/ˈbaʊndəri test/ | 境界テスト |
| empty array |
長年隠れていた二分探索のバグ、2006年
- 2006年、Googleのジョシュア・ブロックは、Javaの標準ライブラリや多くの教科書にある二分探索にバグがあると報告した。
low + highが最大の整数を超えると、mid = (low + high) / 2という行でオーバーフローが起こる。これは、要素数が10億を超える配列でのみ起こる。そのコードは約9年間使われていた。 - このコードは小さな配列でのテストに合格していた。アルゴリズムの正しさには、典型的なケースだけでなく、境界ケースでのトレースやテストによる証拠が必要である。
Why did the binary search bug go unnoticed for years?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
low + high overflowed only for huge arrays.
コンピュテーショナル・シンキング
- 分解:問題を分割する。抽象化:重要なものだけを残す。続いて、パターン認識とアルゴリズムの設計を行う。
- まず入力の条件を明記する。二分探索にはソート済みの配列が必要である。
Binary search requires a sorted array.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
State the input conditions first.
このカードは新しいため、翻訳ができるまで英語で表示されます。
Linear and binary search
- Linear search checks each item: up to n comparisons, O(n).
- Binary search 二分查找 halves the sorted range each step: at most about log₂ n comparisons, O(log n). For 1,000,000 items, about 20.
A linear search checks every item of a 19-item list without finding the target. How many comparisons occur?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
n comparisons in the worst case.
About how many comparisons does binary search need at most for 1,000,000 sorted items?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
log₂ 1,000,000 ≈ 20.
トレース表
- トレース表は、各ステップの後の各変数の値を記録する。アルゴリズムがどこで誤るかを正確に示す。
Search probes and boundary cases
Compare linear search with binary search on sorted data. Trace a match or an absent target; indices start at zero.
解説例:二分探索のトレース
A = [3, 8, 12, 17, 23, 31, 40](インデックスは0~6)から23を探索する。
low high mid A[mid] action
0 6 3 17 23 > 17 → low = 4
4 6 5 31 23 < 31 → high = 4
4 4 4 23 found at index 4
- 比較回数: 3回。線形探索では最大7回となる。
- より安全な中点の求め方:
mid = low + (high - low) // 2は、オーバーフローを起こさずに同じ値を求める。
In the trace, what is mid on the first step for low = 0, high = 6?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
(0 + 6) // 2 = 3.
境界テスト
- 空の配列、要素が1個の配列、最初と最後の項目、存在しない項目、非常に大きな入力をテストする。
- それぞれの結果を、手計算で求めた期待される出力と比較する。
Which are boundary tests for a search?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Test the edges.
典型的なテストに合格しても、正しさの証明にはならない。 ブロックが報告したバグは、長年にわたり小さなテストをすべて通過していた。手作業でトレースし、境界や極端なケースをテストしよう。
Passing typical tests proves an algorithm correct.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Test boundaries and extremes.
アルゴリズムの入力条件を明記し、表でトレースして、境界ケースをテストする。線形探索は O(n)、ソート済みデータに対する二分探索は O(log n) である。正しさには、典型的なケースだけでなく、境界ケースでの証拠も必要である。
Write a trace table for binary search of 40 in [3, 8, 12, 17, 23, 31, 40] and list three boundary tests.
Trace and test.
何が問題でしたか?この内容はサイトの運営チームだけが確認します。