プリンキピア

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

Hashingハッシュ法
Hashing is the problem of designing efficient algorithms which map each object to an integer so that most (if not all) objects will be assigned distinct integers. Although hashing algorithms are often specific to each data type, there exist some general approaches for designing hashing algorithms. The hash value of a simple data type such as an integer can just be the value of the integer itself. The hash value of a string of text can be some mathematical combination of the numeric value of each character in the string. Likewise, the hash value of a collection such as a list can be some combination of the underlying numeric data within each element in the collection.
英語のヒント
ハッシュ法は、すべてとはいかなくても大半のオブジェクトに異なる整数が割り当てられるよう、各オブジェクトを整数に対応させる、効率的なアルゴリズムを設計する問題である。ハッシュのアルゴリズムはデータ型ごとに固有であることが多いが、設計のための一般的な方法もある。整数のような単純なデータ型のハッシュ値は、その整数の値そのものでよい。テキストの文字列のハッシュ値は、各文字の数値を何らかの数学的な方法で組み合わせたものにできる。同様に、リストなどの集まりのハッシュ値も、各要素の基礎となる数値データを、何らかの方法で組み合わせて作れる。
Hashing has a variety of applications spanning computer systems, database systems, computer security, and searching algorithms. For example, hashing algorithms are often used designing secure systems for protecting stored passwords even after a security breach occurs. In the context of data structure problems, hashing offers a different approach than binary search. Instead of relying on pairwise comparisons to narrow down the expected location of an element in a sorted list in logarithmic time, hashing search algorithms can instead directly index an element by hash value in constant time. If binary search trees implement sets and maps by applying the concept of binary search, a hash table implements sets and maps by applying the concept of hashing (Figure 3.25). Rather than organize elements in sorted order from left to right as in a binary search tree, hash tables store and retrieve elements in an array indexed by hash value.
英語のヒント
ハッシュ法は、コンピューターシステム、データベース、コンピューターのセキュリティー、探索アルゴリズムなど、多岐にわたる用途を持つ。たとえば、セキュリティー侵害の後でも保存済みパスワードを守る、安全なシステムの設計によく使われる。データ構造の問題では、二分探索と異なる方法を提供する。整列したリストで、対ごとの比較を繰り返して要素のあるはずの位置を対数時間で絞り込む代わりに、ハッシュ探索では、ハッシュ値によって定数時間で直接要素を参照できる。二分探索木が二分探索の考え方を使って集合やマップを実装するのに対し、ハッシュテーブルはハッシュの考え方を使って実装する(図3.25)。二分探索木のように左から右へ整列順に並べるのではなく、ハッシュ値をインデックスとする配列で、要素を保存・取得する。
Figure 3.25

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

Figure 3.25 Hash tables data structures apply hashing to implement abstract data types such as sets and maps, but must handle collisions between elements that share the same hash value. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.25 ハッシュテーブルのデータ構造は、ハッシュ法で集合やマップなどの抽象データ型を実装するが、同じハッシュ値を持つ要素同士の衝突を処理しなければならない。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Hashing search algorithms are often preferred over binary search algorithms for their runtime benefits, but they come with unique drawbacks. Ideally, different objects would hash to different hash values. But the combinatorial explosion of possibilities for unique strings or collections of complex data means that a collision, or a situation in which multiple objects hash to the same integer index value, is inevitable since integers in a computer can typically only represent a certain, fixed range of integer numbers. Combinatorial explosion is not only a problem for the design of efficient algorithms, but also the design of efficient data structures too.
英語のヒント
ハッシュ探索は、実行時間の利点から、二分探索より好まれることが多いが、特有の欠点も伴う。理想的には、異なるオブジェクトは異なるハッシュ値に変換されるはずである。しかし、文字列や複雑なデータの集まりには、互いに異なるものが組合せ爆発によって膨大な数存在する。一方、コンピューターの整数が表せる範囲は通常、一定の固定範囲に限られる。このため、複数のオブジェクトが同じ整数のインデックス値へ変換される、衝突という状況は避けられない。組合せ爆発は、効率的なアルゴリズムの設計だけでなく、効率的なデータ構造の設計にとっても問題となる。

Graph Problemsグラフの問題

While data structure problems focus primarily on storage and retrieval of elements in a collection, graph problems include a wide variety of computing problems involving the graph data structure. Unlike other data structures, graphs include not only elements (vertices) but also relationships between elements (edges). Graph problems often ask algorithm designers to explore the graph in order to answer questions about elements and the relationships between elements.
英語のヒント
データ構造の問題が、主に集まりに含まれる要素の保存と取得を扱うのに対し、グラフの問題は、グラフのデータ構造に関わる多種多様な計算上の問題を含む。ほかのデータ構造と異なり、グラフは要素(頂点)だけでなく、要素間の関係(辺)も含む。グラフの問題では、要素やその関係についての問いに答えるために、グラフを探索することが設計者に求められることが多い。
Traversal走査
Traversal is the problem of exploring all the vertices in a graph. Graph data structures differ from tree data structures in that there is no explicit root node to begin the traversal and edges can connect back to other parts of the graph. In general, there is no guarantee of hierarchy in a graph. Graph traversal algorithms such as depth-first search and breadth-first search begin at an arbitrary start vertex and explore outwards from the start vertex while keeping track of a set of explored vertices.
英語のヒント
走査は、グラフ内のすべての頂点を調べる問題である。グラフのデータ構造は、走査を始める明確な根ノードがなく、辺がグラフのほかの部分へ戻ってつながることがある点で、木と異なる。一般に、グラフに階層があるという保証はない。深さ優先探索や幅優先探索などのグラフ走査アルゴリズムは、任意の開始頂点から始め、調べた頂点の集合を記録しながら、外側へ探索を広げる。
Depth-First Search
深さ優先探索
A depth-first search is a graph traversal algorithm that recursively explores each neighbor, continuing as far possible along each subproblem depth-first (Figure 3.26). Explored vertices are added to a global set to ensure that the algorithm only explores each vertex once. The runtime of depth-first search is in O(|V| + |E|) with respect to |V|, the number of vertices, and |E|, the number of edges.
英語のヒント
深さ優先探索は、隣接する各頂点を再帰的に調べ、各部分問題で可能な限り奥まで進むグラフ走査アルゴリズムである(図3.26)。調べた頂点は全体で共用する集合へ追加し、各頂点を1回しか探索しないようにする。実行時間は、頂点数|V|と辺数|E|について、O(|V| + |E|)に属する。
Figure 3.26

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

Figure 3.26 Depth-first search is a graph traversal algorithm that continues as far down a path as possible from a start vertex before backtracking. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.26 深さ優先探索は、開始頂点から可能な限り経路を奥へ進み、その後で引き返すグラフ走査アルゴリズムである。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Breadth-First Search
幅優先探索
A breadth-first search iteratively explores each neighbor, expanding the search level-by-level breadth-first (Figure 3.27). Explored vertices are also added to a global set to ensure that the algorithm explores each vertex once and in the correct level-order. The runtime of breadth-first search is also in O(|V| + |E|) with respect to |V|, the number of vertices, and |E|, the number of edges.
英語のヒント
幅優先探索は、隣接する各頂点を反復的に調べ、同じ深さのレベルごとに、横へ広がるように探索する(図3.27)。この場合も、調べた頂点を全体で共用する集合へ追加し、各頂点を1回ずつ、正しいレベル順で探索するようにする。実行時間も、頂点数|V|と辺数|E|について、O(|V| + |E|)に属する。
Graph traversal algorithms are the foundational algorithm design patterns for most graph processing algorithms. Many algorithms require some amount of exploration, and that exploration typically starts at some vertex and continues processing each reachable vertex at most once. A reachable vertex can be reached if a path or sequence of edges from the start vertex exists. As opposed to depth-first search, breadth-first search has the benefit of exploring vertices closer to the start before exploring vertices further from the start, which can be useful for solving problems such as unweighted shortest paths.
英語のヒント
グラフ走査アルゴリズムは、ほとんどのグラフ処理アルゴリズムの基本となる設計パターンである。多くのアルゴリズムには、ある程度の探索が必要で、通常はある頂点から始め、到達可能な各頂点を高々1回ずつ処理する。到達可能な頂点とは、開始頂点からその頂点まで、辺の列である経路が存在する頂点である。幅優先探索には、深さ優先探索と違い、開始点から遠い頂点より先に近い頂点を調べる利点がある。これは、重みのない最短経路問題などを解くのに役立つ。
Figure 3.27

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

Figure 3.27 Depth-first search is a graph traversal algorithm that continues as far down a path as possible from a start vertex before backtracking. Breadth-first search is a graph traversal algorithm that explores level by level expanding outward from the start vertex. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.27 深さ優先探索は、開始頂点から可能な限り経路を奥へ進んでから引き返すグラフ走査アルゴリズムである。幅優先探索は、開始頂点から外側へ広がりながら、レベルごとに探索するグラフ走査アルゴリズムである。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)