第3章データ構造とアルゴリズム
Algorithm Design Patternsアルゴリズムの設計パターン
This case study of canonical search algorithms demonstrates some ideas about algorithmic problem-solving, such as how algorithm design involves iterative improvement (from sequential search to binary search). But this case study does not demonstrate how algorithms are designed in practice. Algorithm designers are occasionally inspired by real-world analogies and metaphors, such as relying on sorted order to divide a dictionary into two equal halves. More often, they depend on knowledge of an existing algorithm design pattern, or a solution to a well-known computing problem, such as sorting and searching. Rather than develop wholly new ideas each time they face a new problem, algorithm designers instead apply one or more algorithm design patterns to solve new problems. By focusing on algorithm design patterns, programmers can solve a wide variety of problems without having to invent a new algorithm every time.
英語のヒント
標準的な探索アルゴリズムのこの事例は、逐次探索から二分探索へのように、アルゴリズムの設計には反復的な改善が伴うことなど、アルゴリズムによる問題解決の考え方を示している。しかし、実際にどうアルゴリズムを設計するかを示しているわけではない。アルゴリズムの設計者は、辞書が整列されていることを利用して等しい2つの部分に分ける場合のように、現実世界との類推や比喩から着想を得ることもある。だが、より多くの場合、既存のアルゴリズムの設計パターン、すなわち整列や探索などのよく知られた計算上の問題の解法についての知識に頼る。設計者は、新しい問題に直面するたびに、まったく新しい考えを生み出すのではなく、1つ以上のアルゴリズム設計パターンを適用して解決する。設計パターンに注目すれば、プログラマーは毎回新しいアルゴリズムを発明しなくても、多種多様な問題を解ける。
For example, suppose we want to design an autocomplete feature, which helps users as they type text into a program by offering word completions for a given prefix query. Algorithm designers begin by modeling the problem in terms of more familiar data types.
英語のヒント
たとえば、自動補完機能を設計したいとしよう。これは、プログラムへ文字を入力する利用者を助けるため、与えられた接頭部の検索文字列に対して、単語を完成させる候補を提示する機能である。アルゴリズムの設計者は、まず、よりなじみのあるデータ型を使って問題をモデル化する。
• The input is a prefix query, such as a string of letters that might represent the start of a word (e.g., “Sea”).
・入力は、単語の先頭を表すような文字の並び(たとえば「Sea」)を指定する、接頭部の検索文字列である。
• The output is a list of matching terms (completion suggestions) for the prefix query.
・出力は、その接頭部の検索文字列に一致する語(補完の候補)のリストである。
In addition to the input and output data, we assume that there is a list of potential terms that the algorithm will use to select the matching terms.
英語のヒント
入力と出力のデータに加え、アルゴリズムが一致する語を選び出すために使う、候補となる語のリストがあると仮定する。
There are a few different ways we could go about solving this problem. One approach is to apply the sequential search design pattern to the list of possible words (Figure 3.11). For each term in the list, we add it to the result if it matches the prefix query. Another approach is to first sort the list of potential terms and then apply two binary searches: the first binary search to find the first matching term and the second binary search to find the last matching term. The output list is all the terms between the first match and the last match.
英語のヒント
この問題の解き方はいくつか考えられる。1つは、候補となる単語のリストに逐次探索の設計パターンを適用する方法である(図3.11)。リストの各語を調べ、接頭部の検索文字列に一致すれば、結果に加える。もう1つは、まず候補の語のリストを整列させ、その後2回の二分探索を行う方法である。1回目で最初に一致する語を見つけ、2回目で最後に一致する語を見つける。出力のリストは、最初の一致から最後の一致までの、すべての語である。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 3.11 Sequential search needs to check every term to see if it matches the prefix “Sea,” whereas two binary searches can be used to find the start and end points of the matching terms in the list. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.11 逐次探索では、接頭部「Sea」に一致するか、すべての語を確認しなければならない。一方、二分探索を2回使えば、リスト内で一致する語の範囲の先頭と末尾を見つけられる。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
TECHNOLOGY IN EVERYDAY LIFE日常生活の中の技術
Online Autocomplete Algorithmsオンラインの自動補完アルゴリズム
In online mapping, autocomplete might take the prefix Sea and automatically suggest the city Seattle. We know that search algorithms solve this problem by maintaining a sorted collection of place suggestions. But many online mapping applications allow users to change maps as the places change in the real world. How can we design the autocomplete feature to support real-world user changes? To apply the binary search algorithm, all place names must be stored in a sorted array-based list. So, every change will also need to maintain the sorted order.
英語のヒント
オンライン地図の自動補完では、接頭部Seaを受け取り、都市Seattle(シアトル)を自動で候補に挙げるかもしれない。探索アルゴリズムは、場所の候補を整列した集まりとして保持して、この問題を解くことがわかっている。しかし多くのオンライン地図アプリケーションでは、現実世界の場所の変化に合わせ、利用者が地図を変更できる。実際の利用者による変更に対応する自動補完機能を、どう設計すればよいだろうか。二分探索を適用するには、すべての地名を、整列した配列に基づくリストに保存しなければならない。したがって、変更のたびに整列順を維持する必要もある。
If we instead add all the place names to a binary search tree, what are the steps for the autocomplete algorithm? How does this choice affect additions, changes, and removals?
代わりに、すべての地名を二分探索木に追加した場合、自動補完アルゴリズムはどのような手順になるだろうか。この選択は、追加、変更、削除にどんな影響を与えるだろうか。
Algorithm Analysisアルゴリズムの分析
Rather than rely on a direct analogy to our human experiences, these two algorithms for the autocomplete feature compose one or more algorithm design patterns to solve the problem. How do we know which approach might be more appropriate to use? One type of analysis is known as algorithm analysis, which studies the outputs produced by an algorithm as well as how the algorithm produces those outputs. Those outputs are then evaluated for correctness, which considers whether the outputs produced by an algorithm match the expected or desired results across the range of possible inputs. An algorithm is considered correct only if its computed outputs are consistent with all the expected outputs; otherwise, the algorithm is considered incorrect.
英語のヒント
自動補完機能のこれら2つのアルゴリズムは、人間の経験から直接類推するのではなく、1つ以上のアルゴリズム設計パターンを組み合わせて問題を解く。どちらの方法がより適切かを、どう判断すればよいだろうか。分析の一種に、アルゴリズム解析と呼ばれるものがある。これは、アルゴリズムが生み出す出力と、それをどのように生み出すかを研究するものである。そして出力について正しさを評価する。ここでは、取り得る入力の範囲全体にわたり、アルゴリズムの出力が期待される結果や望ましい結果に一致するかを検討する。計算した出力が、期待されるすべての出力と整合する場合にだけ、そのアルゴリズムは正しいとみなされ、それ以外は正しくないとみなされる。
Although this definition might sound simple, verifying an algorithm for correctness is often quite difficult in practice, because algorithms are designed to generalize and automate complex processes. The most direct way to verify correctness is to check that the algorithm computes the correct output for every possible input. This is not only computationally difficult, but even potentially impossible to achieve, since some algorithms can accept an infinite range of inputs.
英語のヒント
この定義は単純に聞こえるかもしれないが、実際にアルゴリズムの正しさを検証するのは、かなり難しいことが多い。アルゴリズムは複雑な過程を一般化し、自動化するように設計されているからである。最も直接的な検証方法は、あり得るすべての入力に対して、正しい出力を計算することを確認することである。これは計算上難しいだけでなく、実現不可能なことさえあり得る。入力の範囲が無限であるアルゴリズムもあるからである。
Verifying the correctness of an algorithm is difficult not only due to generality, but also due to ambiguity. Earlier, we saw how canonical searching algorithms may have different outputs according to the input collection type. What happens if the target term contains special characters or misspellings? Should the algorithm attempt to find the closest match? Some ambiguities can be resolved by being explicit about the expected output, but there are also cases where ambiguity simply cannot be resolved in a satisfactory way. If we decide to find the closest match to the target term, how does the algorithm handle cultural differences in interpretation? If humans do not agree on the expected output, but the algorithm must compute some output, what output does it then compute? Or, if we do not want our algorithms to compute a certain output, how does it recognize those situations?
英語のヒント
アルゴリズムの正しさの検証が難しいのは、一般性だけでなく、曖昧さのためでもある。先ほど、標準的な探索アルゴリズムでも、入力される集まりの型によって出力が異なることを見た。目的の語に特殊文字や綴りの誤りが含まれると、どうなるだろうか。最も近いものを見つけようとするべきだろうか。期待する出力を明確にすれば解決できる曖昧さもあるが、満足できる形では解消できないものもある。目的の語に最も近いものを見つけると決めた場合、解釈の文化的な違いをどう扱うのだろうか。人間が期待する出力について合意できないのに、アルゴリズムは何らかの出力を計算しなければならないなら、何を計算するのだろうか。あるいは、特定の出力を計算してほしくない場合、そうした状況をどう認識するのだろうか。
Correctness primarily considers consistency between the algorithm and the model, rather than the algorithm and the real world. Our autocomplete model from earlier returned all word completions that matched the incomplete string of letters. But in practice, this output would likely be unusable: a user typing "a" would see a list of all words starting with the letter "a." Since our model did not specify how to order the results, the user might get frustrated by the irrelevancy of many of the word completions. Suppose we remedy this issue by defining a relevancy metric: every time a user completes typing a word, increase that word’s relevancy for future autocompletion requests. But, as Safiya Noble showed in Algorithms of Oppression, determining relevance in this universalizing way can have undesirable social impacts. Perhaps due to relevancy metrics determined by popular vote, at one point, Google search autosuggestions included ideas like:
英語のヒント
正しさで主に検討するのは、アルゴリズムと現実世界との整合性よりも、アルゴリズムとモデルとの整合性である。先ほどの自動補完モデルは、入力途中の文字列に一致する単語の補完候補をすべて返した。しかし実際には、この出力は使い物にならない可能性が高い。「a」と入力すると、aで始まるすべての単語のリストが表示されるからである。モデルでは結果をどう並べるかを定めなかったため、多くの補完候補が無関係であることに、利用者はいらだつかもしれない。関連度の指標を定義して、この問題を解決するとしよう。利用者が単語を最後まで入力するたびに、その単語の関連度を上げ、以後の自動補完の要求に利用するのである。だが、サフィヤ・ノーブルが『Algorithms of Oppression(抑圧のアルゴリズム)』で示したように、こうして普遍化するやり方で関連性を決めると、望ましくない社会的影響が生じ得る。おそらく多数決で定まる関連度の指標のために、かつてGoogle検索の自動候補には、次のような考えが含まれていた。
• Women cannot: drive, be bishops, be trusted, speak in church
英語のヒント
・女性は〜できない:運転する、主教になる、信用される、教会で発言する。
• Women should not: have rights, vote, work, box
・女性は〜すべきでない:権利を持つ、投票する、働く、ボクシングをする。
• Women should: stay at home, be in the kitchen
・女性は〜すべきだ:家にいる、台所にいる。
• Women need to: be put in their places, know their place, be controlled, be disciplined2
英語のヒント
・女性には〜が必要だ:身の程をわきまえさせられる、身の程を知る、管理される、しつけられる。2
Noble’s critique extends further to consider the intersection of social identities such as race and gender as they relate to the outputs of algorithms that support our daily life.
英語のヒント
ノーブルの批判はさらに進み、人種やジェンダーなどの社会的アイデンティティーが、私たちの日常生活を支えるアルゴリズムの出力に関わる際、互いにどう交差するかを検討する。