Big O記法を理解する

Pythonで学ぶデータ構造とアルゴリズム

Miriam Antona

Software Engineer

Big O記法

  • アルゴリズムの最悪計算量を測定
    • 時間計算量: 完了までの時間
    • 空間計算量: 追加メモリ量
  • 秒/バイトは用いない
    • ハードウェアで結果が変わるため
  • 数式表記: $O(1)$、$O(n)$、$O(n^2)$...
Pythonで学ぶデータ構造とアルゴリズム

Big O記法

Big O記法における各種アルゴリズムのグラフ表示。

Pythonで学ぶデータ構造とアルゴリズム

$O(1)$

colors = ['green', 'yellow', 'blue', 'pink']

def constant(colors):
    print(colors[2])

constant(colors)
blue
Pythonで学ぶデータ構造とアルゴリズム

$O(1)$

colors = ['green', 'yellow', 'blue', 'pink', 'black', 'white', 'purple', 'red']

def constant(colors):
    print(colors[2])  # O(1)

constant(colors)
blue
Pythonで学ぶデータ構造とアルゴリズム

$O(n)$

colors = ['green', 'yellow', 'blue', 'pink']

def linear(colors):
  for color in colors:
    print(color)    

linear(colors)
green

yellow
blue
pink
Pythonで学ぶデータ構造とアルゴリズム

$O(n)$

colors = ['green', 'yellow', 'blue', 'pink'] # n=4

def linear(colors):
  for color in colors:
    print(color)    # O(4)

linear(colors)
  • n=4: 4回の処理
Pythonで学ぶデータ構造とアルゴリズム

$O(n)$

colors = ['green', 'yellow', 'blue', 'pink', 'black', 'white', 'purple'] # n=7

def linear(colors):
  for color in colors:
    print(color)    # O(7)

linear(colors)
  • n=4: 4回の処理
  • n=7: 7回の処理
  • n=100: 100回の処理
  • ...
  • $O(n)$ の計算量
Pythonで学ぶデータ構造とアルゴリズム

$O(n^2)$

colors = ['green', 'yellow', 'blue']

def quadratic(colors):  
  for first in colors:
      for second in colors:
          print(first, second)

quadratic(colors)
  • n=3: (3 x 3) 9回の処理
  • n=100: (100 x 100) 10,000回の処理
  • 二重ループのパターン
  • $O(n^2)$ の計算量
green green
green yellow
green blue
yellow green
yellow yellow
yellow blue
blue green
blue yellow
blue blue
Pythonで学ぶデータ構造とアルゴリズム

$O(n^3)$

colors = ['green', 'yellow', 'blue']

def cubic(colors):  
  for color1 in colors:
      for color2 in colors:
          for color3 in colors:
              print(color1, color2, color3)

cubic(colors)
  • n=3: (3 x 3 x 3) 27回の処理
  • n=10: (10 x 10 x 10) 1,000回の処理
  • 三重ループのパターン
  • $O(n^3)$ の計算量
Pythonで学ぶデータ構造とアルゴリズム

Big Oの計算方法

colors = ['green', 'yellow', 'blue', 'pink', 'black', 'white', 'purple']
other_colors = ['orange', 'brown']

def complex_algorithm(colors):
  color_count = 0

  for color in colors:
      print(color)
      color_count += 1

  for other_color in other_colors:
      print(other_color)
      color_count += 1

  print(color_count)

complex_algorithm(colors)
Pythonで学ぶデータ構造とアルゴリズム

Big Oの計算方法

colors = ['green', 'yellow', 'blue', 'pink', 'black', 'white', 'purple']  # O(1)
other_colors = ['orange', 'brown']  # O(1)


def complex_algorithm(colors): color_count = 0 # O(1)
for color in colors: print(color) # O(n) color_count += 1 # O(n)
for other_color in other_colors: print(other_color) # O(m) color_count += 1 # O(m)
print(color_count) # O(1)
complex_algorithm(colors) # O(4
Pythonで学ぶデータ構造とアルゴリズム

Big Oの計算方法

colors = ['green', 'yellow', 'blue', 'pink', 'black', 'white', 'purple']  # O(1)
other_colors = ['orange', 'brown']  # O(1)

def complex_algorithm(colors):
  color_count = 0          # O(1)

  for color in colors:
    print(color)           # O(n)
    color_count += 1       # O(n)

  for other_color in other_colors:
    print(other_color)     # O(m)
    color_count += 1       # O(m)

  print(color_count)       # O(1)

complex_algorithm(colors)  # O(4 + 2n
Pythonで学ぶデータ構造とアルゴリズム

Big Oの計算方法

colors = ['green', 'yellow', 'blue', 'pink', 'black', 'white', 'purple']  # O(1)
other_colors = ['orange', 'brown']  # O(1)

def complex_algorithm(colors):
  color_count = 0          # O(1)

  for color in colors:
    print(color)           # O(n)
    color_count += 1       # O(n)

  for other_color in other_colors:
    print(other_color)     # O(m)
    color_count += 1       # O(m)

  print(color_count)       # O(1)

complex_algorithm(colors)  # O(4 + 2n + 2m)
Pythonで学ぶデータ構造とアルゴリズム

Big Oの簡約化

  1. 定数を除く
    • $O(4 + 2n + 2m)$ -> $O(n + m)$
  2. 入力が異なるときは変数を分ける
    • $O(n + m)$
  3. 小さい項を除く
    • $O(n + n^2)$
Pythonで学ぶデータ構造とアルゴリズム

Big Oの簡約化

  1. 定数を除く
    • $O(4 + 2n + 2m)$ -> $O(n + m)$
  2. 入力が異なるときは変数を分ける
    • $O(n + m)$
  3. 小さい項を除く
    • $O(n + n^2)$ -> $O(n^2)$
Pythonで学ぶデータ構造とアルゴリズム

Let's practice!

Pythonで学ぶデータ構造とアルゴリズム

Preparing Video For Download...