let's get IT with DAVINA ๐Ÿ’ป

์•Œ๊ณ ๋ฆฌ์ฆ˜/์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ณธ๋ฌธ

DEV_IN/Algorithm

์•Œ๊ณ ๋ฆฌ์ฆ˜/์‹œ๊ฐ„ ๋ณต์žก๋„

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 24. 19:41

์‹œ๊ฐ„ ๋ณต์žก๋„ (Time Complextity)

  • ์ž…๋ ฅ๊ฐ’์ด ์ปค์ง์— ๋”ฐ๋ผ ์ฆ๊ฐ€ํ•˜๋Š” ์‹œ๊ฐ„์˜ ๋น„์œจ์„ ์ตœ์†Œํ™”ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์ž…๋ ฅ์˜ ํฌ๊ธฐ์™€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์˜ ์ƒ๊ด€๊ด€๊ณ„

Big-O ํ‘œ๊ธฐ๋ฒ•

  • ์ฃผ์–ด์ง„ ์‹์„ ๊ฐ’์ด ๊ฐ€์žฅ ํฐ ๋Œ€ํ‘œํ•ญ๋งŒ ๋‚จ๊ฒจ์„œ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•

  • Big-O(๋น…-์˜ค)
    • ์ตœ์•…์˜ ๊ฒฝ์šฐ 
    • ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๊ณผ์ •์—์„œ “์ด ์ •๋„ ์‹œ๊ฐ„๊นŒ์ง€ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค”๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค!
  • Big-Ω(๋น…-์˜ค๋ฉ”๊ฐ€)
    • ์ตœ์„ ์˜ ๊ฒฝ์šฐ
  • Big-θ(๋น…-์„ธํƒ€) 
    • ์ค‘๊ฐ„(ํ‰๊ท )์˜ ๊ฒฝ์šฐ

O(1)

  • constant complexity
  • ์ž…๋ ฅ๊ฐ’์ด ์ฆ๊ฐ€ํ•˜๋”๋ผ๋„ ์‹œ๊ฐ„์ด ๋Š˜์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ž…๋ ฅ๊ฐ’์˜ ํฌ๊ธฐ์™€ ๊ด€๊ณ„์—†์ด, ์ฆ‰์‹œ ์ถœ๋ ฅ๊ฐ’์„ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

function O_1_algorithm(arr, index) {
	return arr[index];
}

let arr = [1, 2, 3, 4, 5];
let index = 1;
let result = O_1_algorithm(arr, index);
console.log(result); // 2 (arr์˜ ๊ธธ์ด๊ฐ€ 100์ด๋ผ๋„ ์ฆ‰์‹œ index๋ฅผ ํ†ตํ•ด ๊ฐ’ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ)

O(n)

  • linear complexity
  • ์ž…๋ ฅ๊ฐ’์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์‹œ๊ฐ„ ๋˜ํ•œ ๊ฐ™์€ ๋น„์œจ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ (n์— ๋น„๋ก€ํ•œ๋‹ค)

//์ž…๋ ฅ๊ฐ’์ด 1์ผ ๋•Œ 1์ดˆ์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ณ , ์ž…๋ ฅ๊ฐ’์„ 100๋ฐฐ๋กœ ์ฆ๊ฐ€์‹œ์ผฐ์„ ๋•Œ 1์ดˆ์˜ 100๋ฐฐ์ธ 100์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
function O_n_algorithm(n) {
	for (let i = 0; i < n; i++) {
	// do something for 1 second
	}
}

//์ž…๋ ฅ๊ฐ’์ด 1 ์ฆ๊ฐ€ํ• ๋•Œ๋งˆ๋‹ค ์ฝ”๋“œ์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด 2์ดˆ์”ฉ ์ฆ๊ฐ€ (O(2n)์ด์ง€๋งŒ, ๊ฐ™์€ ๋น„์œจ๋กœ ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด 2๋ฐฐ,5๋ฐฐ,10๋ฐฐ ์ฆ๊ฐ€ํ•˜๋”๋ผ๋„ ๊ทธ๋Œ€๋กœ O(n)์œผ๋กœ ํ‘œ๊ธฐ)
function another_O_n_algorithm(n) {
	for (let i = 0; i < 2n; i++) {
	// do something for 1 second
	}
}

O(log n)

  • logarithmic complexity
  • O(1) ๋‹ค์Œ์œผ๋กœ ๋น ๋ฅธ ์‹œ๊ฐ„ ๋ณต์žก๋„ (log n์— ๋น„๋ก€ํ•œ๋‹ค)

ex) BST์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ํƒ์ƒ‰ํ•  ๋•Œ, ๋…ธ๋“œ๋ฅผ ์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์–ด๋“ ๋‹ค.

1. 1~100 ์ค‘ ํ•˜๋‚˜์˜ ์ˆซ์ž๋ฅผ ํ”Œ๋ ˆ์ด์–ด1์ด ๊ณ ๋ฅธ๋‹ค (30์„ ๊ณจ๋ž๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค).
2. 50(๊ฐ€์šด๋ฐ) ์ˆซ์ž๋ฅผ ์ œ์‹œํ•˜๋ฉด 50๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ down์„ ์™ธ์นœ๋‹ค.
3. 1~50์ค‘์˜ ํ•˜๋‚˜์˜ ์ˆซ์ž์ด๋ฏ€๋กœ ๋˜๋‹ค์‹œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์ด๊ธฐ ์œ„ํ•ด 25๋ฅผ ์ œ์‹œํ•œ๋‹ค.
4. 25๋ณด๋‹ค ํฌ๋ฏ€๋กœ up์„ ์™ธ์นœ๋‹ค.
5. ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์† ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์—ฌ๋‚˜๊ฐ€๋ฉฐ ์ •๋‹ต์„ ์ฐพ๋Š”๋‹ค. (์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋„ 7๋ฒˆ์ด๋ฉด ๋‹ต ์ฐพ๊ธฐ ๊ฐ€๋Šฅ)

O(n^2)

  • quadratic complexity
  • ์ž…๋ ฅ๊ฐ’์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์‹œ๊ฐ„์ด n์˜ ์ œ๊ณฑ์ˆ˜์˜ ๋น„์œจ๋กœ ์ฆ๊ฐ€
    • ์ž…๋ ฅ๊ฐ’์ด 1์ผ๋•Œ 1์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— 5๋ผ๋Š” ์ž…๋ ฅ๊ฐ’์ด ์ฃผ์–ด์ง€๋ฉด 25์ดˆ๊ฐ€ ๊ฑธ๋ ค์šฉ

function O_quadratic_algorithm(n) {
	for (let i = 0; i < n; i++) {
		for (let j = 0; j < n; j++) {
		// do something for 1 second
		}
	}
}

//2n, 5n ์„ ๋ชจ๋‘ O(n)์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, n^3๊ณผ n^5 ๋„ ๋ชจ๋‘ O(n^2)๋กœ ํ‘œ๊ธฐ
function another_O_quadratic_algorithm(n) {
	for (let i = 0; i < n; i++) {
		for (let j = 0; j < n; j++) {
			for (let k = 0; k < n; k++) {
			// do something for 1 second
			}
		}
	}
}

O(2^n)

  • exponential complexity
  • ๊ฐ€์žฅ ๋Š๋ฆฐ ์‹œ๊ฐ„ ๋ณต์žก๋„

function fibonacci(n) {
	if (n <= 1) {
		return 1;
	}
	return fibonacci(n - 1) + fibonacci(n - 2);
}

๋ฐ์ดํ„ฐ ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์‹œ๊ฐ„ ๋ณต์žก๋„

N์˜ ํฌ๊ธฐ ํ—ˆ์šฉ ์‹œ๊ฐ„ ๋ณต์žก๋„
N ≤ 11 O(N!)
N ≤ 25 O(2^N)
N ≤ 100 O(N^4)
N ≤ 500 O(N^3)
N ≤ 3,000 O(N^2lgN)
N ≤ 5,000 O(N^2)
N ≤ 1,000,000 O(Nlgn)
N ≤ 10,000,000 O(N)
๊ทธ ์ด์ƒ O(lgN),O(1)

 

Comments