第3章データ構造とアルゴリズム
3.1 Introduction to Data Structures and Algorithms3.1 データ構造とアルゴリズム入門
Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Understand the difference between algorithms and programs
・アルゴリズムとプログラムの違いを理解する。
• Relate data structures and abstract data types
・データ構造と抽象データ型を関連付ける。
• Select the data structure that is appropriate to solve a given problem practically Computer science is the study of computers and computational systems that involve data representation and process automation. Owing to their historical roots as calculators, computers can easily represent numerical data. Calculators rely on algorithms to add, subtract, multiply, and divide numbers. But what about more complex data? How do computers represent complex objects like graphs, images, videos, or sentences? What complications arise when we represent or process data in certain ways? These are some of the foundational questions that computer scientists and programmers focus on when designing software and applications that we use to solve problems.
英語のヒント
・与えられた問題を実際に解くのに適したデータ構造を選ぶ。コンピューターサイエンスは、データの表現と処理の自動化に関わるコンピューターおよび計算システムを研究する学問である。コンピューターは歴史的に計算機から生まれたため、数値データを容易に表現できる。計算機はアルゴリズムを使って数の加算、減算、乗算、除算を行う。では、もっと複雑なデータはどうだろうか。コンピューターは、グラフ、画像、動画、文のような複雑な対象をどう表現するのだろうか。ある方法でデータを表現したり処理したりすると、どのような複雑さが生じるのだろうか。これらは、問題を解くために私たちが使うソフトウェアやアプリケーションを設計するとき、コンピューター科学者やプログラマーが注目する基本的な問いの一部である。
A data type determines how computers process data by defining the possible values for data and the possible functionality or operations on that data. For example, the integer data type is defined as values from a certain range of positive or negative whole numbers with functionality including addition, subtraction, multiplication, and division. The string data type is defined as a sequence of characters where each character can be a letter, digit, punctuation, or space, with functionalities that include adding or deleting a character from a string, concatenating strings, and comparing two strings based, for example, on their alphabetical order.
英語のヒント
データ型は、データが取り得る値と、そのデータに対して可能な機能や操作を定めることで、コンピューターがデータをどう処理するかを決定する。たとえば整数型は、ある範囲に属する正または負の整数を値とし、加算、減算、乗算、除算を含む機能を持つものとして定義される。文字列型は文字の列として定義される。それぞれの文字は、英字、数字、句読点や記号、空白のいずれかであり、機能には、文字列への文字の追加や削除、文字列の連結、たとえばアルファベット順に基づく2つの文字列の比較などがある。
Data types like strings are an example of abstraction, the process of simplifying a concept in order to represent it in a computer. The string data type takes a complex concept like a sentence and represents it in terms of more basic data that a computer can work with. When a computer compares two strings, it is really comparing the individual numerical character codes (see Chapter 5 Hardware Realizations of Algorithms: Computer Systems Design) corresponding to each pair of characters within the two strings.
英語のヒント
文字列のようなデータ型は、抽象化の一例である。抽象化とは、ある概念をコンピューター内で表現するために単純化する過程である。文字列型は、文のような複雑な概念を、コンピューターが扱える、より基本的なデータで表す。コンピューターが2つの文字列を比較するとき、実際に比較しているのは、両文字列の各文字の組に対応する個々の数値の文字コードである(第5章「アルゴリズムのハードウェアによる実現:コンピューターシステム設計」参照)。
In this section, we will learn how to solve problems by choosing abstractions for complex data. We will see that just as our data grows more complex, so do our algorithms.
英語のヒント
この節では、複雑なデータに対する抽象化を選ぶことで問題を解く方法を学ぶ。データが複雑になるにつれて、アルゴリズムも同様に複雑になることがわかるだろう。
Introduction to Algorithmsアルゴリズム入門
An algorithm is a sequence of precise instructions that operate on data. We can think of recipes, plans, or instructions from our daily lives as examples of algorithms. Computers can only execute a finite pre-defined set of instructions exactly as instructed, which is why programming can feel like such a different way of communicating as compared to our natural human languages. A program is an implementation (realization) of an algorithm written in a formal programming language.
英語のヒント
アルゴリズムとは、データに作用する正確な指示の列である。日常生活のレシピ、計画、手順なども、アルゴリズムの例と考えられる。コンピューターが実行できるのは、あらかじめ定められた有限個の指示だけであり、それを指示どおりに実行する。このため、プログラミングは人間の自然言語とは大きく異なる伝達方法のように感じられることがある。プログラムとは、形式的なプログラミング言語で書かれたアルゴリズムの実装、すなわち実現である。
Although each programming language is different from all the others, there are still common ideas across all of them. Knowing just a few of these common ideas enables computer scientists to address a wide variety of problems without having to start from scratch every single time. For example, the abstraction of string data enables programmers to write programs that operate on human-readable letters, digits, punctuation, or spaces without having to determine how to delve into each of these concepts. Programming languages allow us to define abstractions for representing ideas in a computer (see Chapter 4 Linguistic Realization of Algorithms: Low-Level Programming Languages for more).
英語のヒント
プログラミング言語はどれも互いに異なるが、すべてに共通する考え方もある。そうした共通の考え方をいくつか知っているだけで、コンピューター科学者は毎回ゼロから始めることなく、多種多様な問題に取り組める。たとえば文字列データの抽象化があれば、プログラマーは、人間が読める英字、数字、句読点や記号、空白を扱うプログラムを書く際に、それぞれの概念を細部までどう扱うかを決めずに済む。プログラミング言語を使うと、コンピューター内で考えを表すための抽象化を定義できる(詳しくは第4章「アルゴリズムの言語による実現:低水準プログラミング言語」参照)。
The study of data structures and algorithms focuses on identifying what is known as a canonical algorithm: a well-known algorithm that showcases design principles helpful across a wide variety of problems. In this chapter, rather than focusing on the programming details, we will instead focus on algorithms and the ideas behind them.
英語のヒント
データ構造とアルゴリズムの研究では、標準的なアルゴリズムと呼ばれるものを見いだすことに重点を置く。これは、さまざまな問題で役立つ設計原理を示す、よく知られたアルゴリズムのことである。本章では、プログラミングの細かな点よりも、アルゴリズムとその背後にある考え方に注目する。
Understanding Data Structuresデータ構造を理解する
For many real-world problems, the ability to design an algorithm depends on how the data is represented. A data structure is a complex data type with two equally important parts:
英語のヒント
現実世界の多くの問題では、アルゴリズムを設計できるかどうかは、データの表し方に左右される。データ構造とは、次の2つの同じくらい重要な部分を持つ複雑なデータ型である。
1. a specific representation or way of organizing a collection of more than one element, which is an individual value or data point, and
1. 複数の要素からなる集まりを表現したり整理したりする、具体的な方法。要素とは、個々の値やデータ点である。
2. a specific functionality or operations such as adding, retrieving, and removing elements.
英語のヒント
2. 要素の追加、取得、削除などの具体的な機能や操作。
In our previous example, a string is a data structure for representing sentences as a sequence of characters. It has specific functionality such as character insertion or deletion, string concatenation, and string comparison. Although the values for complex data are often diverse, computer scientists have designed data structures so that they can be reused for other problems. For example, rather than designing a specialized data structure for sentences in every human language, we often use a single, universal string data structure to represent sentences, including characters from different languages in the same sentence. (We will later see some drawbacks of generalizing assumptions in the design of data structures and algorithms.) In addition, computers take time to execute algorithms, so computer scientists are concerned about efficiency in terms of how long an algorithm takes to compute a result.
英語のヒント
先ほどの例では、文字列は文を文字の列として表すためのデータ構造である。文字の挿入や削除、文字列の連結、文字列の比較といった具体的な機能を持つ。複雑なデータの値は多様であることが多いが、コンピューター科学者は、ほかの問題でも再利用できるようにデータ構造を設計してきた。たとえば、人間の各言語の文に特化したデータ構造を個別に設計する代わりに、1つの汎用的な文字列データ構造で文を表すことが多い。同じ文の中に異なる言語の文字が含まれる場合も表現できる。(データ構造とアルゴリズムを設計する際、前提を一般化することの欠点については後で見る。)また、コンピューターがアルゴリズムを実行するには時間がかかる。そのためコンピューター科学者は、結果の計算にどのくらい時間がかかるかという観点から効率を重視する。
Among the different types of universal data structures, computer scientists have found it helpful to categorize data structures according to their functionality without considering their specific representation. An abstract data type (ADT) consists of all data structures that share common functionality but differ in specific representation.
英語のヒント
さまざまな種類の汎用的なデータ構造について、コンピューター科学者は、具体的な表現方法を考えずに機能によって分類することが役立つと考えてきた。抽象データ型(ADT)は、機能は共通しているが具体的な表現方法が異なる、すべてのデータ構造からなる。
Common abstract data types for complex data follow, and list and set types are shown in Figure 3.2. We will discuss each abstract data type in more detail together with their data structure implementations.
複雑なデータを扱う代表的な抽象データ型を以下に挙げる。リスト型と集合型は図3.2に示す。各抽象データ型については、それを実装するデータ構造とともに、さらに詳しく説明する。
• A list represents an ordered sequence of elements and allows adding, retrieving, and removing elements from any position in the list. Lists are indexed because they allow access to elements by referring to the element's index, which is the position or address for an element in the list. For example, a list can be used to represent a to-do list, where each item in the list is the next task to be completed in chronological order.
英語のヒント
・リストは、要素の順序付きの列を表し、リスト内の任意の位置で要素を追加、取得、削除できる。リストにはインデックスがあり、要素のインデックスを参照してその要素にアクセスできる。インデックスとは、リスト内の要素の位置やアドレスである。たとえば、リストを使って、時系列順に次に行う作業を各項目とする、やることリストを表せる。
• A set represents an unordered collection of unique elements and allows adding, retrieving, and removing elements from the set. Sets typically offer less functionality than lists, but this reduction in functionality allows for more efficient data structure representations. For example, a set can be used to represent the names of all the places that you want to visit in the future.
英語のヒント
・集合は、重複しない要素を順序なしで集めたものを表し、その中で要素を追加、取得、削除できる。集合は通常、リストよりも機能が少ないが、そのぶん、より効率的なデータ構造で表現できる。たとえば、集合を使って、将来訪れたい場所の名前をすべて表せる。
• A map represents unordered associations between key-value pairs of elements, where each key can only appear once in the map. A map is also known as a dictionary since each term (key) has an associated definition (value). Maps are often used in combination with other data structures. For example, a map can be used to represent a travel wish list: each place that you want to visit in the future can be associated with the list of things that you want to do when you arrive at a given place.
英語のヒント
・マップは、要素のキーと値の組の間の、順序のない対応関係を表す。各キーは、マップ内に一度しか現れることができない。それぞれの語(キー)に定義(値)が対応することから、マップは辞書とも呼ばれる。マップは、ほかのデータ構造と組み合わせて使われることが多い。たとえば、マップを使って旅行の希望リストを表せる。将来訪れたい各場所に、その場所へ着いたときにしたいことのリストを対応させるのである。
• A priority queue represents a collection of elements where each element has an associated priority value. In addition to adding elements, priority queues focus on retrieving and removing the element with the highest priority. For example, a priority queue can be used to represent inpatient processing at a hospital emergency room: the patients with more urgent need for care may be prioritized and dealt with first.
英語のヒント
・優先度付きキューは、それぞれの要素に優先度の値を対応させた要素の集まりを表す。要素の追加に加え、優先度付きキューでは、優先度が最も高い要素を取得し、削除することを重視する。たとえば、病院の救急室で入院患者を処理する順序を表すのに使える。より緊急に治療を必要とする患者が、優先的に対応を受けることがある。
• A graph represents binary relations among a collection of entities. More specifically, the entities are represented as vertices in the graph, and a directed or undirected edge is added between two vertices to represent the presence or absence of a certain relation. For example, a friendship graph can be used to represent the friendship relations between people, in which case an undirected edge is added between two persons if they are friends. Graphs allow operations such as adding vertices and edges, removing vertices and edges, and retrieving all edges adjacent to a given vertex.
英語のヒント
・グラフは、実体の集まりの間にある二項関係を表す。より具体的には、実体をグラフの頂点で表し、2つの頂点の間に有向辺または無向辺を加えて、ある関係の有無を表す。たとえば、友人関係グラフで人々の友人関係を表せる。この場合、2人が友人なら、その2人の間に無向辺を加える。グラフでは、頂点や辺の追加、頂点や辺の削除、ある頂点に接続するすべての辺の取得などの操作ができる。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 3.2 Lists and sets are common abstract data types used to represent complex data and can be in the form of integers or string data. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.2 リストと集合は、複雑なデータを表すのによく使われる抽象データ型で、整数や文字列データの形を取ることができる。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)