markdown
計算量の基本md 76c3ed3
lecture/information/algorithm/foundation/computational-complexity-basics.lecture.n.md
Download PDF

計算量けいさんりょう基本きほん

date2026-07-14document_iddoc_4f15851d03885718cae9cc76d1608a96description入力サイズに対する時間・空間計算量と、O・Theta・Omega記法の相違を日英両言語で説明する。prerequisitesfor 文の反復 / 対数の基本 / 一次式と二次式type講義statusactiverelateddata/lecture/information/algorithm/algorithms-portal.lecture.n.md / data/lecture/information/algorithm/search/linear-search.lecture.n.md / data/lecture/information/algorithm/search/binary-search.lecture.n.md
algorithmfoundationundergraduatelecture

1導入どうにゅう

この講義こうぎでは、入力にゅうりょく規模きぼおうじた実行時間じっこうじかん記憶領域きおくりょういき増加ぞうか評価ひょうかする方法ほうほう説明せつめいする。特定とくてい計算機けいさんき計測けいそくした秒数びょうすうだけでなく、入力にゅうりょく大規模だいきぼになったときの増加率ぞうかりつ比較ひかくすることで、アルゴリズムの性質せいしつ分析ぶんせきできる。

2入力にゅうりょくサイズと計算資源けいさんしげん

入力にゅうりょくサイズInput size n とは、問題もんだい入力規模にゅうりょくきぼあらわ非負整数ひふせいすうである。一次元配列いちじげんはいれつでは要素数ようそすう二次元配列にじげんはいれつでは行数ぎょうすう列数れつすうわせなど、問題もんだいてきした尺度しゃくど選択せんたくする。したがって、すべての入力にゅうりょく単一たんいつn表現ひょうげんできるとはかぎらない。

時間計算量じかんけいさんりょうTime complexityとは、入力にゅうりょくサイズにたいして基本操作きほんそうさ実行回数じっこうかいすうがどのように増加ぞうかするかをあらわ関数かんすうである。空間計算量くうかんけいさんりょうSpace complexityとは、実行中じっこうちゅう必要ひつよう記憶領域きおくりょういき増加ぞうかあらわ関数かんすうである。入力自体にゅうりょくじたい除外じょがいした追加領域ついかりょういきだけを評価ひょうかする場合ばあいは、補助空間計算量ほじょくうかんけいさんりょうAuxiliary-space complexityぶ。

3入力にゅうりょくごとの差異さい

同一どういつのサイズ n でも、入力内容にゅうりょくないようによって実行回数じっこうかいすうことなることがある。

  • 最良計算量さいりょうけいさんりょうBest-case complexityは、サイズ n入力にゅうりょくにおける最小さいしょう資源使用量しげんしようりょうである。
  • 最悪計算量さいあくけいさんりょうWorst-case complexityは、サイズ n入力にゅうりょくにおける最大さいだい資源使用量しげんしようりょうである。
  • 平均計算量へいきんけいさんりょうAverage-case complexityは、サイズ n入力集合にゅうりょくしゅうごう確率分布かくりつぶんぷ指定していしたときの資源使用量しげんしようりょう期待値きたいちである。

平均計算量へいきんけいさんりょう確率分布かくりつぶんぷ明示めいじしなければ定義ていぎできない。また、最良さいりょう平均へいきん最悪さいあくという入力にゅうりょく区分くぶんと、次節じせつOΘΩ という関数かんすう漸近的関係ぜんきんてきかんけいべつ概念がいねんである。

4漸近記法ぜんきんきほう

f(n)g(n)十分じゅうぶんおおきな n非負ひふとなる関数かんすうとする。

  • f(n)=O(g(n)) とは、あるせい定数ていすう c,n0存在そんざいし、すべての n[PARSE ERROR: Undefined("Command(\"ge\")")]n0f(n)[PARSE ERROR: Undefined("Command(\"le\")")]cg(n)成立せいりつすることである。これは漸近的上界ぜんきんてきじょうかいあらわす。
  • f(n)=Ω(g(n)) とは、あるせい定数ていすう c,n0存在そんざいし、すべての n[PARSE ERROR: Undefined("Command(\"ge\")")]n0cg(n)[PARSE ERROR: Undefined("Command(\"le\")")]f(n)成立せいりつすることである。これは漸近的下界ぜんきんてきげかいあらわす。
  • f(n)=Θ(g(n)) とは、f(n)=O(g(n))f(n)=Ω(g(n))双方そうほう成立せいりつすることである。これは定数倍ていすうばいのぞいて漸近的増加率ぜんきんてきぞうかりつ一致いっちすることをあらわす。

したがって、O上界じょうかいであり、つね増加率ぞうかりつ一致いっち意味いみするわけではない。たとえば 3n+5=O(n2)ただしいが、より精密せいみつ評価ひょうか3n+5=Θ(n) である。

5具体例ぐたいれい

T(n)=3n+5 とする。n[PARSE ERROR: Undefined("Command(\"ge\")")]1 では

3n[PARSE ERROR: Undefined("Command(\"le\")")]T(n)[PARSE ERROR: Undefined("Command(\"le\")")]8n

成立せいりつするため、T(n)=Θ(n) である。定数項ていすうこう定数倍ていすうばい漸近的増加率ぜんきんてきぞうかりつ変更へんこうしない。

ながn配列はいれつ先頭せんとうから検査けんさする線形探索せんけいたんさくでは、先頭せんとう一致いっちする最良時間さいりょうじかんΘ(1)末尾まつび一致いっちするか不一致ふいっちとなる最悪時間さいあくじかんΘ(n) である。一方いっぽう整列済せいれつず配列はいれつ二分探索にぶんたんさくは、各比較かくひかく候補区間こうほくかん半分はんぶん縮小しゅくしょうする。k 回後かいご候補数こうほすう高々たかだか n/2k であるため、空配列くうはいれつふくめた最悪時間さいあくじかんΘ(log(n+1)) となる。

入力にゅうりょく参照さんしょうするだけで一定個数いっていこすう変数へんすう使用しようするアルゴリズムの補助空間ほじょくうかんΘ(1) である。ながn別配列べつはいれつ確保かくほするなら、補助空間ほじょくうかんΘ(n) となる。時間じかん空間くうかん独立どくりつ評価ひょうかする。

6後続講義こうぞくこうぎへの接続せつぞく

つぎ再帰講義さいきこうぎでは、再帰呼出さいきよびだしの回数かいすう最大深度さいだいしんどから時間じかん補助空間計算量ほじょくうかんけいさんりょう導出どうしゅつする。そのあと整列講義せいれつこうぎ探索講義たんさくこうぎでは、適用条件てきようじょうけん正当性せいとうせい計算量けいさんりょうあわせて比較ひかくする。

data/lecture/information/algorithm/foundation/recursion-basics.lecture.n.md

7要約ようやく

  • 入力にゅうりょくサイズは、問題もんだいてきした尺度しゃくどによって定義ていぎする。
  • 時間計算量じかんけいさんりょう空間計算量くうかんけいさんりょうは、ことなる計算資源けいさんしげん評価ひょうかする。
  • 最良さいりょう平均へいきん最悪さいあく入力にゅうりょく区分くぶんであり、OΘΩ関数間かんすうかん漸近的関係ぜんきんてきかんけいである。
  • O上界じょうかいΩ下界げかいΘ同一どういつ関数かんすうによる上界じょうかい下界げかいあらわす。

Fundamentals of Computational Complexity

1Introduction

This lecture explains how to evaluate growth in running time and memory use as the input size increases. Rather than relying only on elapsed seconds measured on a particular computer, asymptotic growth permits analysis of an algorithm as inputs become large.

2Input Size and Computational Resources

The {input size} n is a nonnegative integer that measures the scale of a problem instance. An appropriate measure depends on the problem: a one-dimensional array may use its number of elements, whereas a two-dimensional array may require both its numbers of rows and columns. Not every input is therefore adequately described by a single parameter.

{Time complexity} is a function describing how the number of executed basic operations grows with input size. {Space complexity} is a function describing growth in memory required during execution. When the analysis excludes storage occupied by the input itself and counts only additional storage, the measure is called {auxiliary-space complexity}.

3Variation among Inputs

Inputs of the same size can require different amounts of a resource.

  • {Best-case complexity} is the minimum resource use among inputs of size n.
  • {Worst-case complexity} is the maximum resource use among inputs of size n.
  • {Average-case complexity} is the expected resource use under a specified probability distribution on inputs of size n.

Average-case complexity is undefined until the probability distribution is specified. Moreover, best, average, and worst cases classify inputs, whereas O, Θ, and Ω describe asymptotic relations between functions. These are separate concepts.

4Asymptotic Notation

Let f(n) and g(n) be nonnegative for all sufficiently large n.

  • f(n)=O(g(n)) means that positive constants c,n0 exist such that f(n)[PARSE ERROR: Undefined("Command(\"le\")")]cg(n) for every n[PARSE ERROR: Undefined("Command(\"ge\")")]n0. It states an asymptotic upper bound.
  • f(n)=Ω(g(n)) means that positive constants c,n0 exist such that cg(n)[PARSE ERROR: Undefined("Command(\"le\")")]f(n) for every n[PARSE ERROR: Undefined("Command(\"ge\")")]n0. It states an asymptotic lower bound.
  • f(n)=Θ(g(n)) means that both f(n)=O(g(n)) and f(n)=Ω(g(n)) hold. It states equality of asymptotic growth rates up to constant factors.

Thus, O denotes an upper bound and does not necessarily assert a tight growth rate. For example, 3n+5=O(n2) is true, but 3n+5=Θ(n) is more precise.

5Examples

Let T(n)=3n+5. For n[PARSE ERROR: Undefined("Command(\"ge\")")]1,

3n[PARSE ERROR: Undefined("Command(\"le\")")]T(n)[PARSE ERROR: Undefined("Command(\"le\")")]8n,

so T(n)=Θ(n). Constant terms and constant factors do not alter the asymptotic growth rate.

For linear search on an array of length n, a match at the first element gives a best-case running time of Θ(1), while a match only at the final element or no match gives a worst-case running time of Θ(n). By contrast, binary search on a sorted array halves the candidate interval at each comparison. After k comparisons, at most n/2k candidates remain, yielding a worst-case running time of Θ(log(n+1)), including the empty-array case.

An algorithm that only reads its input and uses a fixed number of variables has auxiliary-space complexity Θ(1). Allocating another array of length n requires Θ(n) auxiliary space. Time and space must be analyzed separately.

6Connections to Subsequent Lectures

The next lecture derives the time and auxiliary-space complexity of recursion from the number of recursive calls and the maximum call depth. The subsequent sorting and search lectures compare applicability conditions, correctness, and complexity together.

data/lecture/information/algorithm/foundation/recursion-basics.lecture.n.md

7Summary

  • Input size must be defined by a measure appropriate to the problem.
  • Time and space complexity measure different computational resources.
  • Best, average, and worst cases classify inputs; O, Θ, and Ω describe asymptotic relations between functions.
  • O gives an upper bound, Ω gives a lower bound, and Θ gives a matching upper and lower bound.
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる