빅 O 표기법 이해하기

Python으로 배우는 자료구조와 알고리즘

Miriam Antona

Software Engineer

빅 O 표기법

  • 알고리즘의 최악의 경우 복잡도 측정
    • 시간 복잡도: 실행 완료까지 걸리는 시간
    • 공간 복잡도: 추가 메모리 공간
  • 초/바이트 단위 미사용
    • 하드웨어에 따라 결과가 달라짐
  • 수학적 표현: $O(1)$, $O(n)$, $O(n^2)$...
Python으로 배우는 자료구조와 알고리즘

빅 O 표기법

빅 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으로 배우는 자료구조와 알고리즘

빅 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으로 배우는 자료구조와 알고리즘

빅 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으로 배우는 자료구조와 알고리즘

빅 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으로 배우는 자료구조와 알고리즘

빅 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으로 배우는 자료구조와 알고리즘

빅 O 표기법 단순화하기

  1. 상수 제거
    • $O(4 + 2n + 2m)$ -> $O(n + m)$
  2. 입력별로 다른 변수 사용
    • $O(n + m)$
  3. 작은 항 제거
    • $O(n + n^2)$
Python으로 배우는 자료구조와 알고리즘

빅 O 표기법 단순화하기

  1. 상수 제거
    • $O(4 + 2n + 2m)$ -> $O(n + m)$
  2. 입력별로 다른 변수 사용
    • $O(n + m)$
  3. 작은 항 제거
    • $O(n + n^2)$ -> $O(n^2)$
Python으로 배우는 자료구조와 알고리즘

연습해 봅시다!

Python으로 배우는 자료구조와 알고리즘

Preparing Video For Download...