[์ด์ฐ์ํ] Chapter 4. Algorithms
1. Introduction
- ์๊ณ ๋ฆฌ์ฆ์ ๋ค์๊ณผ ๊ฐ์ ํน์ง๋ค์ ๊ฐ์ง๊ณ ์๋ค.
1) input
2) output
3) precision : ์ ํํ๊ฒ ์์ ๋์ด์ผ ํ๋ค
4) determinism : ์ํ์๊ฒฐ๊ณผ๋ uniqueํ๋ค. ๋ํ input๊ณผ ์ ํ๋ step์ ๋ฐ๋ผ ๊ฒฐ์ ๋๋ค.
5) finiteness : ์๊ณ ๋ฆฌ์ฆ์ ๋๋์ผ ํ๋ค. (์ ํ๊ฐ์ instruction์ด ์ํ๋ ์ดํ์ ๋ฉ์ถฐ์ผ ํจ)
6) correctness : ์๊ณ ๋ฆฌ์ฆ์ ์ํด ๋์ถ๋ output์ ๋ง์์ผ ํ๋ค.
7) generality : ๋ชจ๋ input์ ๋ํด ์ ์ฉ๋์ด์ผ ํ๋ค.
2. Examples of Algorithms
- ํฐ ์ ์ฐพ๊ธฐ ์๊ณ ๋ฆฌ์ฆ, ๋ฌธ์์ด ์ฐพ๊ธฐ ์๊ณ ๋ฆฌ์ฆ, ์ฝ์ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ
- Randomized Algorithms (๋ฌด์์ ์๊ณ ๋ฆฌ์ฆ) : ๋์๋ฅผ ๋ฐ์์์ผ ์งํ๊ณผ์ ์ ๊ฒฐ์ ํ๋ ์๊ณ ๋ฆฌ์ฆ
3. Analysis of Algorithms
- Useless program : ๋๋ฌด ์ค๋ ์๊ฐ์ด ๊ฑธ๋ฆฌ๊ฑฐ๋ ๋๋ฌด ๋ง์ space๋ฅผ ์ฐจ์งํด์๋ ์๋จ.
- Complexity of algorithms : ์ค์ ๋ก ์ด ์๊ณ ๋ฆฌ์ฆ์ด ์ํ๋๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ์ ํํ ๊ตฌํ๊ธฐ ํ๋ค๋ค. ๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ ์ง์ ์๊ฐ์ ์ฌ๋ ๊ฒ ๋ณด๋ค๋ ์๊ณ ๋ฆฌ์ฆ์ ์๊ฐ์ estimate, ์ถ์ ํ ๊ฒ์ด๋ค! ์ด ๋, ์๊ณ ๋ฆฌ์ฆ์ด ์ํ๋๋๋ฐ ํ์ํ time ํน์ space๋ฅผ ๋ฐ๋ก ์๊ณ ๋ฆฌ์ฆ์ Complexity๋ผ๊ณ ํ๋ค.
(ํ๋ก๊ทธ๋จ์ด ๋์๊ฐ๋ ์ปดํจํฐ, data๊ฐ ํํ๋๋ ๋ฐฉ์, ํ๋ก๊ทธ๋จ์ด ๊ธฐ๊ณ์ธ์ด๋ก ์ด๋ป๊ฒ ๋ฒ์ญ๋์๋์ง, ๋ฌด์จ ์ธ์ด๊ฐ ์ฐ์๋์ง ๋ฑ์ด Performance์ ์ํฅ์ ๋ฏธ์น๋ parameter๋ค์ด๋ค)
1) Time complexity
- Best-case time
- Worst-case time
- Average-case time
- f(n) = O(g(n)) : g(n)์ f(n)์ ์ํ์ , asymptotic upper bound for f (f์ ์ํ์ ๊ทผ์ )
|f(n)| <= C1|g(n)| (C1์ ์์ ์์)
ex) 60n^2 + 5n + 1 <= 60n^2 + 5n^2 + 1n^2 = 66n^2
์ฆ, C1 = 66 -> 60n^2 + 5n + 1 = O(n^2)
2n + 3logn <= 2n + 3n = 5n
C1 = 5 -> 2n + 3logn = O(n)
- f(n) = Ω(g(n)) : g(n)์ f(n)์ ํํ์ , asymptotic lower bound for f (f์ ํํ ์ ๊ทผ์ )
|f(n)| >= C2|g(n)| (C1์ ์์ ์์)
ex) 60n^2 + 5n + 1 >= 60n^2
์ฆ, C2 = 60 -> 60n^2 + 5n + 1 = Ω(n^2)
2n + 3logn <= 2n
C2 = 2-> 2n + 3logn = Ω(n)
- f(n) = Θ(g(n)) : g(n)์ f(n)๊ณผ ๊ฐ์ฅ ๊ฐ๊น์ด ์ ๊ทผ์ , asymptotic tight bound for f
ex) 60n^2 + 5n + 1 = O(n^2) ์ด๊ณ 60n^2 + 5n + 1 = Ω(n^2) ์ด๋๊น
60n^2 + 5n + 1 = Θ(n^2)
2n + 3logn = O(n) ์ด๊ณ 2n + 3logn = Ω(n) ์ด๋๊น
2n + 3logn = Θ(n)
4. Recursive Algorithms : ์ฌ๊ท์ ์๊ณ ๋ฆฌ์ฆ, ์๊ธฐ ์์ ์ ํธ์ถํ๋ ์๊ณ ๋ฆฌ์ฆ
ex ) n! = n(n-1)! = n(n-1)(n-2)! ...
fibonacci sequence : f1 = 1, f2 = 2, fn = fn-1 + fn-2 for n>= 3