プリンキピア

第3章データ構造とアルゴリズム

3.4 Algorithmic Paradigms3.4 アルゴリズムの基本的な考え方

Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Apply the divide and conquer technique
・分割統治法を適用する。
• Explain the brute-force method
・総当たり法を説明する。
• Interpret and apply the greedy method
・貪欲法を理解し、適用する。
• Understand how to apply reductions to solve problems
・帰着を利用して問題を解く方法を理解する。
Algorithm design patterns are solutions to well-known computing problems. In 3.5 Sample Algorithms by Problem, we will survey algorithm design patterns by problem. As it turns out, many of these algorithm design patterns share similarities in their approaches to solving problems. Here, we will introduce the algorithmic paradigm, which is the common concepts and ideas behind algorithm design patterns.
英語のヒント
アルゴリズムの設計パターンは、よく知られた計算上の問題に対する解法である。3.5節「問題別のアルゴリズムの例」では、問題ごとに設計パターンを概観する。実は、これらの多くは、問題の解き方に共通点がある。ここでは、設計パターンの背後にある共通の概念や考え方、すなわちアルゴリズムのパラダイムを紹介する。

Divide and Conquer Algorithms分割統治アルゴリズム

A divide and conquer algorithm is an algorithmic paradigm that breaks down a problem into smaller subproblems (divide), recursively solves each subproblem (conquer), and then combines the result of each subproblem to form the overall solution. The algorithm idea of recursion is fundamental to divide and conquer algorithms because it solves complex problems by dividing input data into smaller instances of the same problem known as subproblems. Such recursion calls terminate when the inputs become so small or so simple that other non-recursive procedures can provide the answers.
英語のヒント
分割統治アルゴリズムは、問題をより小さい部分問題へ分け(分割)、各部分問題を再帰的に解き(統治)、その結果を組み合わせて全体の解を作る、アルゴリズムのパラダイムである。再帰という考え方は、分割統治アルゴリズムの基礎となる。再帰は、入力データを部分問題と呼ばれる、同じ問題のより小さい事例へ分けることで、複雑な問題を解くからである。この再帰呼出しは、入力が十分小さく、あるいは単純になり、再帰しない別の手順で答えを出せるようになった時点で終了する。
A subproblem is a smaller instance of a problem that can be solved independently, and each subproblem can be solved independently of other subproblems by reapplying the same recursive algorithm. To design recursive subproblems, algorithm designers often focus on identifying structural self-similarity in the input data. This process repeats until the input data is small enough to solve directly. Once all the subproblems have been solved, the recursive algorithm reassembles each of these independent solutions to compute the result for the original problem.
英語のヒント
部分問題とは、独立して解ける、ある問題の小さい事例である。同じ再帰アルゴリズムを再び適用すれば、ほかの部分問題から独立して、それぞれを解ける。再帰的な部分問題を設計するとき、設計者は入力データの構造的な自己相似性を見つけることに注目する場合が多い。この過程は、入力が直接解けるほど小さくなるまで繰り返される。すべての部分問題が解けると、再帰アルゴリズムは個別の解を組み立て直し、元の問題の結果を計算する。
3 B. Dawson, “Arranging invisible icons in quadratic time,” 2021. https://randomascii.wordpress.com/2021/02/16/arranging-invisible-icons-in-quadratic-time/
3 B. Dawson「Arranging invisible icons in quadratic time(見えないアイコンを2次の時間で並べる)」2021年。https://randomascii.wordpress.com/2021/02/16/arranging-invisible-icons-in-quadratic-time/
Earlier, we introduced binary search to find a target within a sorted list as an analogy for finding a term in a dictionary sorted alphabetically. Instead of starting from the beginning of the dictionary and checking each term, as in a sequential search, we could instead start from the middle and look left or right based on where we would expect to find the term in the dictionary. But binary search can also be understood as an example of a divide and conquer algorithm (Figure 3.16).
英語のヒント
先に、アルファベット順の辞書から語を探すこととの類推として、整列したリストから目的の要素を見つける二分探索を紹介した。逐次探索のように最初から1語ずつ確認する代わりに、中央から始め、目的の語がどこにあると予想されるかに従って左か右を調べることができた。二分探索は、分割統治アルゴリズムの一例として理解することもできる(図3.16)。
1. The problem of finding a target within the entire sorted list is broken down (divided) into the subproblem of finding a target within half of the list after comparing the middle element to the target. Half of the list can be ruled out based on this comparison, leaving binary search to find the target within the remaining half.
英語のヒント
1. 整列したリスト全体から目的の要素を見つける問題は、中央の要素と目的の要素を比較した後、リストの半分から探す部分問題へ分けられる(分割)。比較によって一方の半分を候補から除けるため、二分探索は残り半分で目的の要素を探せばよくなる。
2. Binary search is repeated on the remaining half of the sorted list (conquer). This process continues recursively until the target is found in the sorted list (or reported as not in the list at all).
2. 整列したリストの残り半分で二分探索を繰り返す(統治)。この処理は、目的の要素が見つかるまで、またはリスト内にはないと報告されるまで、再帰的に続く。
3. To solve the original problem of finding a target within the entire sorted list, the result of the subproblem must inform the overall solution. The original call to binary search reports the same result as its subproblem.
英語のヒント
3. 整列したリスト全体から目的の要素を探す元の問題を解くには、部分問題の結果を全体の解に反映する必要がある。最初の二分探索の呼出しは、その部分問題と同じ結果を報告する。
Figure 3.16

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 3.16 Binary search is a divide and conquer algorithm that repeatedly makes one recursive call on half of remaining elements. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.16 二分探索は、残った要素の半分に対して、再帰呼出しを1回行うことを繰り返す、分割統治アルゴリズムである。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Binary search makes a single recursive call on the remaining half of the list as part of the conquer step, but other divide and conquer algorithms make multiple recursive calls to solve their problems. We will also see algorithms that do a lot of work in the final step of combining results more than just reporting the same result as a subproblem.
英語のヒント
二分探索では、統治の段階で、リストの残り半分に対する再帰呼出しを1回行う。しかし、ほかの分割統治アルゴリズムでは、問題を解くために複数回の再帰呼出しを行う。最後に結果を組み合わせる段階でも、部分問題と同じ結果を報告するだけでなく、多くの処理を行うアルゴリズムを見ることになる。
Given a list of elements in an unknown order, a sorting algorithm should return a new list containing the same elements rearranged into a logical order, such as least to greatest. One canonical divide and conquer algorithm for comparison sorting is called merge sort. The problem of comparison sorting is grounded in the comparison operation. The comparison operation is like a traditional weighing scale that tells whether one element is heavier, lighter, or the same weight as another element, but provides no information about the exact weight or value of the element. Though this might seem like a serious restriction, comparison sorting is actually a very rich problem in computer science—it is perhaps the most deeply studied problem in computer science. Choosing comparison as the fundamental operation is also practical for complex data, where it might be hard (or even impossible) to assign an exact numeric ranking (Figure 3.17).
英語のヒント
順序が不明な要素のリストを受け取ったとき、整列アルゴリズムは、同じ要素を、小さいものから大きいものの順など、論理的な順序に並べ替えた新しいリストを返すべきである。比較による整列の標準的な分割統治アルゴリズムの1つが、マージソートである。比較による整列の問題は、比較操作を基礎とする。比較操作は昔ながらの天秤に似ており、一方の要素が他方より重い、軽い、同じ重さであることはわかるが、正確な重さや値の情報は得られない。大きな制約に思えるかもしれないが、比較による整列は、実際にはコンピューターサイエンスの非常に豊かな問題であり、おそらくこの分野で最も深く研究されてきた問題である。正確な数値の順位を割り当てることが難しい、あるいは不可能でさえある複雑なデータに対しても、比較を基本操作に選ぶのは実用的である(図3.17)。
1. The problem of sorting the list is broken down (divided) into two subproblems: the subproblem of sorting the left half and the subproblem of sorting the right half.
1. リストを整列する問題を、左半分を整列する問題と、右半分を整列する問題の2つに分ける(分割)。
2. Merge sort is repeated to sort each half (conquer). This process continues recursively until the sublists are one element long. To sort a one-element list, the algorithm does not need to do anything, since the elements in the list are already arranged in a logical order.
2. 各半分を整列するため、マージソートを繰り返す(統治)。この処理は、部分リストが要素1個になるまで再帰的に続く。要素1個のリストは、すでに論理的な順序で並んでいるので、整列するために何もする必要がない。
3. To solve the original problem of sorting the entire list, combine adjacent sorted sublists by merging them while maintaining sorted order. Merging each pair of adjacent sorted sublists repeats to form larger and larger sorted sublists until the entire list is fully sorted.
英語のヒント
3. リスト全体を整列する元の問題を解くには、隣り合う整列済みの部分リストを、整列順を保ちながらマージして組み合わせる。隣接する部分リストの各組をマージすることを繰り返し、整列済みの部分リストを大きくしていく。リスト全体が完全に整列するまで続ける。
Figure 3.17

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 3.17 Merge sort is a divide and conquer algorithm that repeatedly makes two recursive calls on both halves of the sublist. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.17 マージソートは、部分リストの左右両方の半分に対し、2回の再帰呼出しを繰り返す分割統治アルゴリズムである。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)