๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปAlgorithm/์ด๋ก 

๊ทธ๋ฆฌ๋””(Greedy)/ํƒ์š• ๊ธฐ๋ฒ•

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

๊ฐ€์žฅ ์ง๊ด€์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„ ํŒจ๋Ÿฌ๋‹ค์ž„์œผ๋กœ ์ตœ์ ํ™” ๋ฌธ์ œ(๊ฐ€๋Šฅํ•œ ํ•ด๋“ค ์ค‘ ๊ฐ€์žฅ ์ข‹์€(์ตœ๋Œ€/์ตœ์†Œ) ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ)๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค. 

๋ชจ๋“  ์„ ํƒ์ง€๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•๋งŒ์„ ์„ ํƒ (๊ทผ์‹œ์•ˆ์  ํƒœ๋„)

-> ๊ทผ์‹œ์•ˆ์ ์ธ ์„ ํƒ์œผ๋กœ ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด๋ฅผ ์ฐพ๊ณ , ์ด๋“ค์„ ๋ชจ์•„์„œ(์ถ•์ ํ•˜์—ฌ) ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด๋ฅผ ์–ป๋Š”๋‹ค.

 

๊ทผ์‹œ์•ˆ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ๊ฒฝ์šฐ ์ตœ์ ์˜ ์ •๋‹ต์„ ์ฐพ์•„์ฃผ์ง€ ๋ชปํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ •๋‹น์„ฑ ์ฆ๋ช…์ด ํ•„์ˆ˜์ !(ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ๊ฐ€ ์ œํ•œ์ )

-ํƒ์š•์  ์„ ํƒ ์†์„ฑ : ํƒ์š•์ ์œผ๋กœ ์„ ํƒํ•˜๋”๋ผ๋„ ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค

-์ตœ์  ๋ถ€๋ถ„ ๊ตฌ์กฐ : ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด์—์„œ ์ „์ฒด ๋ฌธ์ œ์˜ ์ตœ์ ํ•ด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

 

๊ทธ๋ฆฌ๋””์˜ ์‚ฌ์šฉ

 

1. ๋™์ „ ๊ฑฐ์Šค๋ฆ„๋ˆ ๋ฌธ์ œ

 

๊ฑฐ์Šค๋ฆ„๋ˆ ์•ก์ˆ˜์— ๋Œ€ํ•œ ์ตœ์†Œ ๋™์ „ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ

-๊ฑฐ์Šค๋ฆ„๋ˆ๋ณด๋‹ค ์ž‘์€ ์•ก์ˆ˜์˜ ๋™์ „ ์ค‘ ๊ฐ€์žฅ ๋†’์€ ์•ก์ˆ˜์˜ ๋™์ „์„ ์„ ํƒํ•˜๊ณ  ๊ฑฐ์Šค๋ฆ„๋ˆ ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

ex) ๊ฑฐ์Šค๋ฆ„๋ˆ์ด 720์›์ด๋ผ๋ฉด,

    500์› 1๊ฐœ ์„ ํƒ -> ๊ฑฐ์Šค๋ฆ„๋ˆ 220

    100์› 1๊ฐœ ์„ ํƒ -> ๊ฑฐ์Šค๋ฆ„๋ˆ 120 , 100์› 1๊ฐœ ์„ ํƒ -> ๊ฑฐ์Šค๋ฆ„๋ˆ 20

    10์› 1๊ฐœ ์„ ํƒ -> ๊ฑฐ์Šค๋ฆ„๋ˆ 10,     10์› 1๊ฐœ ์„ ํƒ -> ๊ฑฐ์Šค๋ฆ„๋ˆ 0 -> ์ข…๋ฃŒ.  ์ด ๋™์ „ 5๊ฐœ

๋™์ „์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€์ด์ง€๋งŒ ๊ฐ€์žฅ ๋†’์€ ์•ก์ˆ˜์˜ ๋™์ „์„ ํ•ญ์ƒ ์„ ํƒํ•˜๋ฉด ๋™์ „์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ•œ์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๋™์ „ ๊ฑฐ์Šค๋ฆ„๋ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ฌธ์ œ์ 

๋งŒ์•ฝ, 160์›์งœ๋ฆฌ ๋™์ „์ด ์žˆ์„ ๊ฒฝ์šฐ 200์›์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€

์œ„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์˜ํ•˜๋ฉด 200์›์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 160์›์งœ๋ฆฌ 1๊ฐœ, 10์›์งœ๋ฆฌ 4๊ฐœ๋กœ ์ด 5๊ฐœ๋กœ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ

100์›์งœ๋ฆฌ ๋™์ „ 2๊ฐœ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ตœ์ ์˜ ํ•ด์ด๋‹ค.

-> ํ•ญ์ƒ ์ตœ์ ์˜ ๋‹ต์„ ์ฃผ์ง€ ๋ชปํ•œ๋‹ค(์‹ค์ œ๋กœ๋Š” ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ ์šฉ๋˜๋„๋ก ๋™์ „์ด ๋ฐœํ–‰๋จ.160์›์งœ๋ฆฌ ๋™์ „ ์กด์žฌX)

 

 

2. ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ (Mininum Spanning Tree/MST)

 

์ฃผ์–ด์ง„ ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์—์„œ ์‚ฌ์ดํด์ด ์—†์ด ๋ชจ๋“  ์ ๋“ค์„ ์—ฐ๊ฒฐ์‹œํ‚จ ํŠธ๋ฆฌ(์‹ ์žฅํŠธ๋ฆฌ)๋“ค ์ค‘ ๊ฐ„์„ ๋“ค์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ํŠธ๋ฆฌ

-> ํฌ๋ฃจ์Šค์นผ(Kruskal) ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ„์„ ์ด ์‚ฌ์ดํด์„ ๋งŒ๋“ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ๊ทธ ๊ฐ„์„ ์„ ์ถ”๊ฐ€์‹œํ‚จ๋‹ค.

-> ํ”„๋ฆผ(Prim) ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ž„์˜์˜ ์  ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•œ ํ›„, (n-1)๊ฐœ์˜ ๊ฐ„์„ ์„ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€์‹œ์ผœ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค.

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ n๊ฐœ์˜ ํŠธ๋ฆฌ๋“ค์ด ์ ์ฐจ ํ•ฉ์ณ์ ธ์„œ 1๊ฐœ์˜ ์‹ ์žฅ ํŠธ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ 

ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 1๊ฐœ์˜ ํŠธ๋ฆฌ๊ฐ€ ์ž๋ผ๋‚˜์„œ ์‹ ์žฅ ํŠธ๋ฆฌ๊ฐ€ ๋œ๋‹ค.

*์ฝ”๋“œ ๊ตฌํ˜„์€ ๋‹ค์Œ ํฌ์ŠคํŒ…์—

 

3. ์ตœ๋‹จ ๊ฒฝ๋กœ ์ฐพ๊ธฐ(Shortest Path)

 

์ฃผ์–ด์ง„ ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์—์„œ ์–ด๋Š ํ•œ ์ถœ๋ฐœ์ ์—์„œ ๋˜ ๋‹ค๋ฅธ ๋„์ฐฉ์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ(DIjstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กด์žฌํ•œ๋‹ค.

-์ฃผ์–ด์ง„ ์ถœ๋ฐœ์ ์—์„œ ์‹œ์ž‘

-์ถœ๋ฐœ์ ์œผ๋กœ๋ถ€ํ„ฐ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ํ™•์ •๋˜์ง€ ์•Š์€ ์ ๋“ค ์ค‘์—์„œ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ทธ ์ ์—์„œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ํ™•์ •ํ•œ๋‹ค.

-์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ถœ๋ฐœ์ ์œผ๋กœ๋ถ€ํ„ฐ ๋ชจ๋“  ์ ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ํ™•์ •

 

 

4. ๋ถ€๋ถ„ ๋ฐฐ๋‚ญ ๋ฌธ์ œ(Fractional Knapsack)

 

n๊ฐœ์˜ ๋ฌผ๊ฑด๊ณผ ๊ฐ ๋ฌผ๊ฑด์— ๋Œ€ํ•œ ๋ฌด๊ฒŒ์™€ ๊ฐ€์น˜์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์งˆ๋•Œ, ์ตœ๋Œ€์˜ ๊ฐ€์น˜๋ฅผ ๊ฐ–๋„๋ก ๋ฐฐ๋‚ญ์— ๋„ฃ์„ ๋ฌผ๊ฑด๋“ค์„ ์ •ํ•˜๋Š” ๋ฌธ์ œ.(๋ฐฐ๋‚ญ์—๋Š” ํ•œ์ •๋œ ๋ฌด๊ฒŒ์˜ ๋ฌผ๊ฑด๋“ค์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค)

๋ถ€๋ถ„ ๋ฐฐ๋‚ญ ๋ฌธ์ œ์—์„œ๋Š” ๋ฌผ๊ฑด์„ ๋ถ€๋ถ„์ ์œผ๋กœ ๋‹ด๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค.(์–ด๋– ํ•œ ๋ฌผ๊ฑด์„ 1/3๋งŒํผ ๋‹ด์„ ์ˆ˜ ์žˆ์Œ)

-๋‹จ์œ„ ๋ฌด๊ฒŒ ๋‹น ๊ฐ€์น˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

-๋ฐฐ๋‚ญ์— ์—ฌ์œ ๊ฐ€ ์žˆ๋Š” ๋™์•ˆ ๊ฐ€์น˜๊ฐ€ ๋†’์€ ๋ฌผ๊ฑด๋“ค๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๊ฐ€๋ฐฉ์— ๋„ฃ๋Š”๋‹ค.

 

 

5. ์ง‘ํ•ฉ ์ปค๋ฒ„ ๋ฌธ์ œ(Set Cover)

 

n๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ฐ€์ง„ ์ง‘ํ•ฉ U์™€ U์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ๋“ค์„ ์›์†Œ๋กœ ํ•˜๋Š” ์ง‘ํ•ฉ F๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œํ•œ์œผ๋กœ F์˜ ์›์†Œ๋“ค์„ ์„ ํƒํ•˜์—ฌ U๋ฅผ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ

 

์ตœ์ ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€

-๋ชจ๋“  ์กฐํ•ฉ์„ 1๊ฐœ์”ฉ ํ•ฉ์ง‘ํ•ฉํ•˜์—ฌ U๊ฐ€ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์ง‘ํ•ฉ ์ˆ˜๊ฐ€ ์ตœ์†Œ์ธ ๊ฒƒ์„ ์ฐพ๋Š”๋‹ค 

 ex) F={s1,s2,s3} ์ผ ๊ฒฝ์šฐ -> ์ง‘ํ•ฉ์ด 1๊ฐœ์ธ ๊ฒฝ์šฐ 3C1

                                -> ์ง‘ํ•ฉ์ด 2๊ฐœ์ธ ๊ฒฝ์šฐ 3C2

                                -> ์ง‘ํ•ฉ์ด 3๊ฐœ์ธ ๊ฒฝ์šฐ 3C3    => 3+3+1= 2^3-1 = 7

    => O(2^n) : NP์˜ ๋Œ€ํ‘œ์ ์ธ ์œ ํ˜• (์ตœ์ ํ•ด๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ์‚ฌ์‹ค์ƒ ๋ถˆ๊ฐ€๋Šฅ)

-U์˜ ์›์†Œ๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ๊ฐ€์ง„ ์ง‘ํ•ฉ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ U์—์„œ ์ œ๊ฑฐ, ์ œ๊ฑฐํ•œ ์ง‘ํ•ฉ์„ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์— ์ถ”๊ฐ€ 

 ์œ„์˜ ๊ณผ์ •์„ U๊ฐ€ ๊ณต์ง‘ํ•ฉ์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ทผ์‚ฌํ•ด๋ฅผ ์ฐพ๋Š”๋‹ค.

 

์‹œ๊ฐ„๋ณต์žก๋„

while์„ ํ•œ๋ฒˆ ๋Œ ๋•Œ(O(1))๋งˆ๋‹ค 

๋ถ€๋ถ„์ง‘ํ•ฉ Si ๋“ค์„ U์™€ ๊ฐ๊ฐ ๋น„๊ต - n*O(n) = O(n^2)

์ง‘ํ•ฉ U์—์„œ Si์˜ ์›์†Œ๋ฅผ ์ œ๊ฑฐ - O(n)

Si๋ฅผ C์— ์ถ”๊ฐ€ - O(1)

-> O(1)+O(n^2)+O(n)+O(1) = O(n^2)

while๋ฌธ์„ ์ตœ๋Œ€ n๋ฒˆ ๋ฐ˜๋ณต

=>n*O(n^2) = O(n^3)

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„(python)

Cities = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Schools = {}
Schools[1] = set([1, 2, 3, 8])
Schools[2] = set([1, 2, 3, 4, 8])
Schools[3] = set([1, 2, 3, 4])
Schools[4] = set([2, 3, 4, 5, 7, 8])
Schools[5] = set([4, 5, 6, 7])
Schools[6] = set([5, 6, 7, 9, 10])
Schools[7] = set([4, 5, 6, 7])
Schools[8] = set([1, 2, 4, 8])
Schools[9] = set([6, 9])
Schools[10] = set([6, 10])

finalCities=set()

while Cities:
    bestCity =None##์‹œํ‹ฐ ๋ฒˆํ˜ธ 
    schoolCovered = set()

    for school,cities in Schools.items():
        covered=Cities & cities #๊ต์ง‘ํ•ฉ ์—ฐ์‚ฐ
        if len(covered)>len(schoolCovered):
            bestCity=school
            schoolCovered=covered
    ##๊ฐ€์žฅ ๋งŽ์ด ์›์†Œ๋ฅผ ์ง€๋‹Œ ์ง‘ํ•ฉ ์ฐพ์Œ
    Cities-=schoolCovered #์ฐจ์ง‘ํ•ฉ
    finalCities.add(bestCity)

print(finalCities)

 

 

6-1. ๊ตฌ๊ฐ„ ์Šค์ผ€์ค„๋ง(Interval scheduling)

 

n๊ฐœ์˜ task ์™€ 1๊ฐœ์˜ ํ”„๋กœ์„ธ์„œ๊ฐ€ ์žˆ์„ ๋•Œ, ์ˆ˜ํ–‰์‹œ๊ฐ„์ด ๊ฒน์น˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ฐ€์žฅ ๋งŽ์€ taksk๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก task๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฌธ์ œ (๊ฐ task๋Š” ์‹œ์ž‘,์ข…๋ฃŒ ์‹œ๊ฐ„์„ ๊ฐ–๋Š”๋‹ค)

์ข…๋ฃŒ ์‹œ๊ฐ„์ด ๋น ๋ฅธ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์—ฌ ์„ ํƒํ•œ๋‹ค. 

-๊ฐ€์žฅ ์ผ์ฐ ์ข…๋ฃŒํ•˜๋Š” task ์„ ํƒ

-๊ทธ ๋‹ค์Œ์œผ๋กœ ์„ ํƒ๋œ ๋™์•„๋ฆฌ์˜ ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ์ง์ „์— ์„ ํƒ๋œ ๋™์•„๋ฆฌ์˜ ์ข…๋ฃŒ์‹œ๊ฐ„์„ ๋น„๊ตํ•˜์—ฌ ์„ ํƒ/ํฌ๊ธฐ

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„(python)

meeting = [['C1', 8.0, 13.0], ['C2', 9.0, 11.0], ['C3', 10.5, 11.5], ['C4', 11.0, 14.0],
     ['C5', 13.0, 16.0], ['C6', 14.0, 15.0], ['C7', 15.0, 17.0]]

meeting.sort(key=lambda x: x[2])

schedule=[meeting[0]]
i=0
for j in range(1,len(meeting)):
    if meeting[i][2]<=meeting[j][1]: #๊ทธ ์ „ ์ข…๋ฃŒ์‹œ๊ฐ๋ณด๋‹ค ์ง€๊ธˆ ์‹œ์ž‘์‹œ๊ฐ„์ด ๋Š๋ฆฌ๋ฉด
        schedule.append(meeting[j])
        i=j
print(schedule)

 

6-2. ๊ตฌ๊ฐ„ ๋ถ„ํ• (Interval Partitioning)

 

n๊ฐœ์˜ task๊ฐ€ ์žˆ์„ ๋•Œ, ๋ชจ๋“  Task๋ฅผ ์ˆ˜ํ–‰์‹œ๊ฐ„์ด ๊ฒน์น˜์ง€ ์•Š์œผ๋ฉด์„œ ์ตœ์†Œํ•œ์˜ ํ”„๋กœ์„ธ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ฐฐ์ •ํ•˜๋Š” ๋ฌธ์ œ (๊ฐ task๋Š” ์‹œ์ž‘,์ข…๋ฃŒ ์‹œ๊ฐ„์„ ๊ฐ–๋Š”๋‹ค)

์‹œ์ž‘ ์‹œ๊ฐ„์ด ๋น ๋ฅธ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์—ฌ ์„ ํƒํ•œ๋‹ค.

-๊ฐ€์žฅ ๋นจ๋ฆฌ ์‹œ์ž‘ํ•˜๋Š” task ์„ ํƒ(์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์„œ)

-๊ทธ ๋‹ค์Œ์œผ๋กœ ์„ ํƒ๋œ ๋™์•„๋ฆฌ์˜ ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ๋งŒ๋“ค์–ด์ง„ ํ”„๋กœ์„ธ์„œ๋“ค์˜ task ์ข…๋ฃŒ์‹œ๊ฐ„์„ ๋น„๊ตํ•˜์—ฌ ๋“ค์–ด๊ฐˆ ํ”„๋กœ์„ธ์„œ๊ฐ€ ์žˆ์œผ๋ฉด task ์ถ”๊ฐ€ ํ›„ ์ข…๋ฃŒ ์‹œ๊ฐ„ ๊ฐฑ์‹ , ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์„œ ์ƒ์„ฑ

 

์‹œ๊ฐ„ ๋ณต์žก๋„ 

task ์ •๋ ฌ - O(nlogn)

n๊ฐœ์˜ task, m๊ฐœ์˜ ํ”„๋กœ์„ธ์„œ - O(m)*n=O(mn)

=> O(nlogn)+O(mn)

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„(python)

meeting = [['C1', 9.0, 11.0], ['C2', 9.0, 12.5], ['C3', 13.0, 14.5],
     ['C4', 14.0, 17.0], ['C5', 11.0, 14.0], ['C6', 9.0, 11.0],
     ['C7', 15.0, 16.5], ['C8', 15.0, 16.5], ['C9', 11.0, 12.5],
     ['C10', 13.0, 14.5]]

meeting.sort(key=lambda x:x[1])

schedule=[[meeting[0]]]
finishTime=[meeting[0][2]]
k=0 #๋ฏธํŒ…๋ฃธ ๋ฒˆํ˜ธ

for i in range(1,len(meeting)):
    flag=False
    for j in range(k+1):
        if meeting[i][1]>=finishTime[j]:
            #๋‹ค๋ฅธ ํšŒ์˜ ์‹œ์ž‘์‹œ๊ฐ„์ด ์ข…๋ฃŒ์‹œ๊ฐ๋ฐฐ์—ด ๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„๋•Œ
            flag=True
            finishTime[j]=meeting[i][2]
            schedule[j].append(meeting[i])
            break
    
    if not flag: ##๋ฐฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์ด ์—†์œผ๋ฉด
        schedule.append(meeting[i])
        k+=1
        finishTime.append(meeting[i][2])

for i in range(k+1):
    print('Room',i+1,':',schedule[i])

 

 

7.ํ—ˆํ”„๋งŒ ์••์ถ•(Huffman)

 

ํ—ˆํ”„๋งŒ ์••์ถ• : ํŒŒ์ผ์— ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž์—๋Š” ์งง์€ ์ด์ง„์ฝ”๋“œ๋ฅผ ํ• ๋‹นํ•˜๊ณ , ๋“œ๋ฌผ๊ฒŒ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž์—๋Š” ๊ธด ์ด์ง„์ฝ”๋“œ๋ฅผ ํ• ๋‹นํ•˜์—ฌ ์••์ถ•. ์••์ถ• ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€ํ™˜์‹œํ‚จ ๋ฌธ์ž ์ฝ”๋“œ๋“ค ์‚ฌ์ด์—๋Š” ์ ‘๋‘๋ถ€ ํŠน์„ฑ(prefix property: ํ• ๋‹น๋œ ์ด์ง„์ฝ”๋“œ๋Š” ๋‹ค๋ฅธ ๋ฌธ์ž์— ํ• ๋‹น๋œ ์ด์ง„์ฝ”๋“œ์˜ ์ ‘๋‘๋ถ€๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค)์ด ์กด์žฌ. ๋ฌธ์ž์˜ ๋นˆ๋„์ˆ˜์— ๊ธฐ๋ฐ˜์„ ๋‘” ์ด์ง„ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋Œ€์‘๋˜๋Š” ์ด์ง„์ฝ”๋“œ๋ฅผ ํ• ๋‹นํ•œ๋‹ค.(ํ—ˆํ”„๋งŒ ์ฝ”๋“œ)

 

ํ—ˆํ”„๋งŒ ์ฝ”๋“œ ๋ฌธ์ œ๋Š” n๊ฐœ์˜ ๋ฌธ์ž์— ๋Œ€ํ•œ ๊ฐ๊ฐ์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ—ˆํ”„๋งŒ ํŠธ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

-๊ฐ ๋ฌธ์ž ๋‹น ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋นˆ๋„์ˆ˜๋ฅผ ์ €์žฅ

-๋นˆ๋„์ˆ˜์— ๋Œ€ํ•ด ์šฐ์„ ์ˆœ์œ„ํ(PQ)๋ฅผ ์ƒ์„ฑ

-PQ์— ์žˆ๋Š” ๋…ธ๋“œ์ˆ˜๊ฐ€ 2์ด์ƒ์ด๋ฉด ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ ๋…ธ๋“œ(A,B) 2๊ฐœ๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋…ธ๋“œ N์„ ๋งŒ๋“ค์–ด ์ž์‹๋…ธ๋“œ๋กœ ๋งŒ๋“ ๋‹ค (N์˜ ๋นˆ๋„์ˆ˜=A์˜ ๋นˆ๋„์ˆ˜ + B์˜ ๋นˆ๋„์ˆ˜) 

-PQ์— ์ถ”๊ฐ€

 

์‹œ๊ฐ„๋ณต์žก๋„

๋…ธ๋“œ ์ƒ์„ฑ ๋ฐ ๋นˆ๋„์ˆ˜ ์ €์žฅ -> O(n)

์šฐ์„ ์ˆœ์œ„ Q ์ƒ์„ฑ -> O(n) (์ด์ง„ ํž™ ์ž๋ฃŒ๊ตฌ์กฐ)

๋…ธ๋“œ 2๊ฐœ ์ œ๊ฑฐ ๋ฐ N ๋…ธ๋“œ ์‚ฝ์ž… * n-1๋ฒˆ ๋ฐ˜๋ณต-> O(logn)*(n-1) = O(nlogn)

ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ๋ฐ˜ํ™˜ -> O(1)

=> O(n)+O(n)+O(nlogn)+O(1) = O(nlogn)