プリンキピア

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

Big O Notation for Orders of Growth増加のオーダーのビッグO記法

In the 1970s, computer scientists applied asymptotic notation, a mathematical notation that formally defines the order of growth. We can use Big O notation to describe the time complexity of the sequential search algorithm. In general, we say a function f(N) is in the class of O(g(N)), denoted by f(N) = O(g(N)) or f(N) in O(g(N)), if when N tends to infinity, the ratio f(N)/g(N) is upper bounded by some constant. The function g(N) is usually some simple function that defines the order of growth such as g(N) = 1 (constant function), g(N) = N (linear function), g(N) = log N (logarithmic function), or other functions as follows:
英語のヒント
1970年代、コンピューター科学者は、増加のオーダーを形式的に定義する数学記法である漸近記法を取り入れた。逐次探索の時間計算量も、ビッグO記法で記述できる。一般に、Nを無限大に近付けたとき、比f(N)/g(N)がある定数で上から抑えられるなら、関数f(N)はO(g(N))のクラスに属するといい、f(N) = O(g(N))またはf(N) in O(g(N))と表す。関数g(N)は通常、g(N) = 1(定数関数)、g(N) = N(線形関数)、g(N) = log N(対数関数)など、増加のオーダーを定める単純な関数である。次のように表せる。
• In the best case, the order of growth of sequential search is in O(1).
・最良の場合、逐次探索の増加のオーダーはO(1)に属する。
• In the worst case, the order of growth of sequential search is in O(N) with respect to N, the number of words in the list.
・最悪の場合、逐次探索の増加のオーダーは、リスト内の単語数Nについて、O(N)に属する。
The constant order of growth is described in Big O notation as “O(1)” while the linear order of growth is described in Big O notation as “O(N) with respect to N, the size of the problem.” Big O notation formalizes the concept of a prediction. Given the size of the problem, N, calculate how long it takes to run the algorithm on a problem of that size. For large lists, in order to double the worst-case runtime of sequential search, we would need to double the size of the list.
英語のヒント
増加のオーダーが定数であることは、ビッグO記法で「O(1)」と書き、線形であることは「問題のサイズNについてO(N)」と書く。ビッグO記法は、予測という概念を形式化する。問題のサイズNが与えられたとき、そのサイズの問題でアルゴリズムを実行するのにかかる時間を計算するのである。大きなリストでは、逐次探索の最悪時の実行時間を2倍にするには、リストのサイズを2倍にする必要がある。
O(1) and O(N) are not the only orders of growth.
増加のオーダーは、O(1)とO(N)だけではない。
• O(1), or constant.
・O(1):定数。
• O(log N), or logarithmic.
・O(log N):対数。
• O(N), or linear.
・O(N):線形。
• O(N log N), or linearithmic.
英語のヒント
・O(N log N):線形対数。
• O(N2), or quadratic.
英語のヒント
・O(N2):2次。
• O(N3), or cubic.
英語のヒント
・O(N3):3次。
• O(2N), O(3N), . . . , or exponential.
英語のヒント
・O(2N)、O(3N)、…:指数。
• O(N!), or factorial.
英語のヒント
・O(N!):階乗。
The O(log N), or logarithmic, order of growth appears quite often in algorithm analysis. The logarithm of a large number tells how many times it needs to be divided by a small number until it reaches 1. The binary logarithm, or log2, tells how many times a large number needs to be divided by 2 until it reaches 1. In the worst case, the time complexity of sequential search is in O(N) with respect to N, the number of words in the list, since each repetition of sequential search rules out one remaining element. How about binary search? In the worst case, the time complexity of binary search is in O(log N) with respect to N, the number of words in the list, since each repetition of binary search rules out half the remaining elements.
英語のヒント
O(log N)、すなわち対数の増加のオーダーは、アルゴリズム解析によく現れる。大きな数の対数は、その数を小さな数で何回割ると1になるかを示す。2を底とする対数、すなわちlog2は、大きな数を2で何回割ると1になるかを示す。最悪の場合、逐次探索の時間計算量は、リストの単語数NについてO(N)に属する。1回の繰り返しで、残る要素を1個ずつ除外するからである。二分探索はどうだろうか。最悪の場合、二分探索の時間計算量は、リストの単語数NについてO(log N)に属する。1回の繰り返しで、残る要素の半分を除外するからである。
Another way to understand orders of growth is to consider how a change in the size of the problem results in a change to the resource usage. When we double the size of the input problem, algorithms in each order of growth respond differently (Figure 3.15).
英語のヒント
増加のオーダーを理解するもう1つの方法は、問題のサイズの変化が、資源使用量をどう変えるかを考えることである。入力する問題のサイズを2倍にすると、増加のオーダーごとに、アルゴリズムの反応は異なる(図3.15)。
• O(1) algorithms will not require any more resources.
・O(1)のアルゴリズムでは、必要な資源は増えない。
• O(log N) algorithms will require 1 additional resource unit.
・O(log N)のアルゴリズムでは、追加で1単位の資源が必要になる。
• O(N) algorithms will require 2 times the number of resources.
・O(N)のアルゴリズムでは、2倍の資源が必要になる。
• O(N log N) algorithms will require a little more than 2 times the number of resources.
・O(N log N)のアルゴリズムでは、2倍を少し超える資源が必要になる。
• O(N2) algorithms will require 4 times the number of resources.
・O(N2)のアルゴリズムでは、4倍の資源が必要になる。
• O(N3) algorithms will require 8 times the number of resources.
・O(N3)のアルゴリズムでは、8倍の資源が必要になる。
• O(2N), O(3N), . . . algorithms will require the squared or cubed number of resources.
・O(2N)、O(3N)、…のアルゴリズムでは、資源の数を2乗、3乗した量が必要になる。
• O(N!) algorithms will require even more.
・O(N!)のアルゴリズムでは、さらに多くの資源が必要になる。
This growth compounds, so quadrupling the size of the problem for an O(N2) algorithm will require 16 times the number of resources. Algorithm design and discovery is often motivated by these massive differences between orders of growth. Note that this explanation of how each order of growth responds differently oversimplifies the problem. Rigorously speaking, a function f(N) expressed in the big-O notation as being is in the class of O(g(N)) can be much more complex than the simple function g(N). For example, f(N) = 4log N + 100 log(log N) is in O(log N), but when N doubles, f(2N) is definitely not just one unit more than the original function f(N). A similar argument applies for all other functions other than the constant O(1) function.
英語のヒント
この増加は累積するため、O(N2)のアルゴリズムで問題のサイズを4倍にすると、16倍の資源が必要になる。アルゴリズムの設計や発見は、こうした増加のオーダー間の大きな違いに動機付けられることが多い。各オーダーの反応の違いについて、この説明は問題を過度に単純化していることに注意しよう。厳密にいえば、ビッグO記法でO(g(N))のクラスに属すると表される関数f(N)は、単純な関数g(N)よりも、はるかに複雑な場合がある。たとえばf(N) = 4log N + 100 log(log N)はO(log N)に属するが、Nを2倍にしたとき、f(2N)は元のf(N)より1単位だけ大きくなるわけでは決してない。定数のO(1)の関数を除く、ほかのすべての関数にも、同様の議論が当てはまる。
Figure 3.15

data source: Geeks for Geeks, “Big O Notation Tutorial—A Guide to Big O Analysis,” last updated Mar ch 29, 2024; attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 3.15 This chart shows the time it would take for an algorithm with each of the given orders of growth to finish running on a problem of the given size, N. When an algorithm takes longer than 1025 years to compute, that means it takes longer than the current age of the universe. (data source: Geeks for Geeks, “Big O Notation Tutorial—A Guide to Big O Analysis,” last updated March 29, 2024; attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図3.15 この表は、示したそれぞれの増加のオーダーを持つアルゴリズムが、指定されたサイズNの問題で実行を終えるまでの時間を示す。計算に1025年より長くかかるなら、現在の宇宙の年齢より長くかかることを意味する。(データ出典:Geeks for Geeks「Big O Notation Tutorial—A Guide to Big O Analysis」、最終更新2024年3月29日。出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
As the size of the problem increases, algorithmic complexity becomes a larger and larger factor. For problems dealing with just 1,000 elements, the time it would take to run an exponential-time algorithm on that problem exceeds the current age of the universe. In practice, across applications working with large amounts of data, O(N log N) is often considered the limit for real-world algorithms. Even then, O(N log N) algorithms cannot be run too frequently. For algorithms that need to run frequently on large amounts of data, algorithm designers target O(N), O(log N), or O(1).
英語のヒント
問題のサイズが増えると、アルゴリズムの計算量はますます大きな要因になる。わずか1,000個の要素を扱う問題でも、指数時間のアルゴリズムの実行にかかる時間は、現在の宇宙の年齢を超える。大量のデータを扱う現実のアプリケーションでは、O(N log N)が実用的なアルゴリズムの限界とみなされることが多い。それでも、O(N log N)のアルゴリズムをあまり頻繁に実行することはできない。大量のデータに対して頻繁に実行する必要がある場合、設計者はO(N)、O(log N)、O(1)を目指す。