Memahami Notasi Big O

Struktur Data dan Algoritma di Python

Miriam Antona

Software Engineer

Notasi Big O

  • Mengukur kompleksitas kasus terburuk algoritma
    • Kompleksitas waktu: waktu hingga selesai
    • Kompleksitas ruang: memori tambahan
  • Tidak memakai detik/byte
    • Hasil berbeda tergantung perangkat keras
  • Ekspresi matematis: $O(1)$, $O(n)$, $O(n^2)$...
Struktur Data dan Algoritma di Python

Notasi Big O

Representasi grafis berbagai jenis algoritma dalam Notasi Big O.

Struktur Data dan Algoritma di Python

$O(1)$

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

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

constant(colors)
blue
Struktur Data dan Algoritma di Python

$O(1)$

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

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

constant(colors)
blue
Struktur Data dan Algoritma di Python

$O(n)$

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

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

linear(colors)
green

yellow
blue
pink
Struktur Data dan Algoritma di 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 operasi
Struktur Data dan Algoritma di 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 operasi
  • n=7: 7 operasi
  • n=100: 100 operasi
  • ...
  • kompleksitas $O(n)$
Struktur Data dan Algoritma di 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 operasi
  • n=100: (100 x 100) 10.000 operasi
  • pola kuadratik
  • kompleksitas $O(n^2)$
green green
green yellow
green blue
yellow green
yellow yellow
yellow blue
blue green
blue yellow
blue blue
Struktur Data dan Algoritma di 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 operasi
  • n=10: (10 x 10 x 10) 1.000 operasi
  • pola kubik
  • kompleksitas $O(n^3)$
Struktur Data dan Algoritma di Python

Menghitung Notasi 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)
Struktur Data dan Algoritma di Python

Menghitung Notasi 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
Struktur Data dan Algoritma di Python

Menghitung Notasi 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
Struktur Data dan Algoritma di Python

Menghitung Notasi 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)
Struktur Data dan Algoritma di Python

Menyederhanakan Notasi Big O

  1. Hapus konstanta
    • $O(4 + 2n + 2m)$ -> $O(n + m)$
  2. Variabel berbeda untuk input berbeda
    • $O(n + m)$
  3. Hapus suku lebih kecil
    • $O(n + n^2)$
Struktur Data dan Algoritma di Python

Menyederhanakan Notasi Big O

  1. Hapus konstanta
    • $O(4 + 2n + 2m)$ -> $O(n + m)$
  2. Variabel berbeda untuk input berbeda
    • $O(n + m)$
  3. Hapus suku lebih kecil
    • $O(n + n^2)$ -> $O(n^2)$
Struktur Data dan Algoritma di Python

Ayo berlatih!

Struktur Data dan Algoritma di Python

Preparing Video For Download...