2021/1/22 ์ž๋ฃŒ๊ตฌ์กฐ(4) Complexity Analysis๐Ÿ”Ž

9rganizedChaosยท2021๋…„ 1์›” 21์ผ
0
post-thumbnail

Complexity Analysis

๋ณต์žก๋„ ๋ถ„์„์ด๋ž€? ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ‘ธ๋Š” ๋ฐ ์žˆ์–ด์„œ ์‹œ๊ฐ„๊ณผ ๊ณต๊ฐ„์„ ์–ผ๋งˆ๋‚˜ ์ฐจ์ง€ํ•˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ. ์ด๋Š” ๊ณง ํ•ด๋‹น ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์–ผ๋งˆ๋‚˜ ํšจ์œจ์ ์ธ๊ฐ€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

Big-O notation

Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity.


Big-O๋งˆ๋‹ค ์ด๋ฆ„์ด ๋‹ค ์žˆ์Œ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์‚ฌ๊ฐ€ ๊ธ‰๊ฒฉํ•  ์ˆ˜๋ก ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํšจ์œจ์ด ์•„์ฃผ ๋ณ„๋กœ์ธ ๊ฒƒ์ž„

1) O(1): constant time
2) O(log n): logarithmic time
3) O(n): linear time
4) O(n log n): log linear time
5) O(n^2): quadratic time
6) O(n^3): cubic time
7) O(c^n): exponential time
8) O(n^c): polynomial time

Common Data Structure Operation

  • ๋‹จ์ผ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์™€ ๋”๋ธ”์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์ฐจ์ด๋Š” ๋ฆฌ๋ฌด๋ธŒ์— ์žˆ์Œ. ๋”๋ธ”์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋Š” ์ง์ „์˜ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋…ธ๋“œ ์‚ญ์ œ์‹œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(1)์ž„. ๊ทธ๋Ÿฌ๋‚˜ ๋‹จ์ผ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋Š” ์ง์ „ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์—†์–ด์„œ, ์•ž์—์„œ ๋ถ€ํ„ฐ ์กฐํšŒํ•ด์™€์•ผ ํ•จ. ๊ทธ๋ž˜์„œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n)์ด ๋˜์–ด๋ฒ„๋ฆผ. ๋ฌผ๋ก  ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๋”๋ธ”์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋” ๋‚ฎ?์ง€๋งŒ, ๋ฐ์ดํ„ฐ๋ฅผ ๋” ์žก์•„๋จน์Œ... ์•ž ๋’ค ๋…ธ๋“œ ๋‹ค ์ €์žฅํ•˜๋‹ˆ๊นŒ...

  • constant O(1) ์˜ˆ์‹œ
    : ๋ฐฐ์—ด์—์„œ n๋ฒˆ์งธ ์š”์†Œ ์ฐพ๊ธฐ, ๋ฐฐ์—ด์—์„œ n๋ฒˆ์งธ์— ์–ด์‹ธ์ธ, ํ•ด์‰ฌํ…Œ์ด๋ธ” ์ถ”๊ฐ€, ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ(insert / ์–ด๋””์— ์ถ”๊ฐ€ํ• ์ง€ ์•ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ), ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ํ—ค๋“œ๋ฆฌ๋ฌด๋ธŒ

  • logarithmic O(log n) ์˜ˆ์‹œ
    : ๋ฐ”์ด๋„ˆ๋ฆฌ์„œ์น˜ํŠธ๋ฆฌ์—์„œ ์„œ์น˜ํ•  ๋•Œ (๋ฐธ๋Ÿฐ์Šค๊ฐ€ ์•ˆ๋งž๋Š” ์›Œ์ŠคํŠธ ์ผ€์ด์Šค(์•„๋ž˜ ์ด๋ฏธ์ง€)์˜ ๊ฒฝ์šฐ ์ด๊ฑฐ ๋ฆฌ๋‹ˆ์–ด์ž„)

  • linear O(n) ์˜ˆ์‹œ
    : ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ(Lookup, find, assign), ๋ฐฐ์—ด ์š”์†Œ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ, ๋ฐฐ์—ด ํŠน์ • ๊ฐ’์˜ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ, ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ์ค‘๊ฐ„์— ์žˆ๋Š” ์•  ๋ฆฌ๋ฌด๋ธŒ, ํŠธ๋ฆฌ find

  • quadratic O(n^2) ์˜ˆ์‹œ
    : ์ด์ค‘ํฌ๋ฌธ

  • exponential O(c^n) ์˜ˆ์‹œ
    : ์žฌ๊ท€๋กœ ํ”ผ๋ณด๋‚˜์น˜ ๊ตฌํ˜„ํ•˜๊ธฐ

profile
๋ถ€์ •ํ™•ํ•œ ์ •๋ณด๋‚˜ ์ž˜๋ชป๋œ ์ •๋ณด๋Š” ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๋น ๋ฅด๊ฒŒ ์ˆ˜์ •ํ† ๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

0๊ฐœ์˜ ๋Œ“๊ธ€