๐Ÿ’ป [์ฝ”ํ…Œ11] 16์žฅ ๊ทธ๋ฆฌ๋””

๊น€๋ฏธ์—ฐยท2024๋…„ 3์›” 24์ผ
0

16์žฅ. ๊ทธ๋ฆฌ๋””

16-1. ๊ทธ๋ฆฌ๋”” ๊ฐœ๋…

  • ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •์—์„œ ๊ฒฐ์ • ์ˆœ๊ฐ„๋งˆ๋‹ค ๋ˆˆ ์•ž์— ๋ณด์ด๋Š” ์ตœ์„ ์˜ ์„ ํƒ์„ ํ•˜๋ฉฐ ์„ ํƒ์€ ๋ฒˆ๋ณตํ•˜์ง€ ์•Š๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์ง€์—ญ ์ตœ์ ํ•ด ์ถ”๊ตฌ(์ „์ฒด์—์„œ์˜ ์ตœ์ ํ•ด ๋ณด์žฅ X)

1) ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ตœ์ ํ•ด๋ฅผ ๋ณด์žฅํ•˜๋ ค๋ฉด?

  • ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ • ์ƒํ™ฉ์—์„œ ์ตœ์ ํ•ด ๋ณด์žฅ
    • ์ตœ์  ๋ถ€๋ถ„ ๊ตฌ์กฐ : ๋ถ€๋ถ„ํ•ด๋ฅผ ํ‘ธ๋Š” ๊ณผ์ •์ด ์ตœ์ ํ•ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •๊ณผ ์ผ์น˜
    • ๊ทธ๋ฆฌ๋”” ์„ ํƒ ์†์„ฑ : ์„ ํƒ ๊ณผ์ •์ด ๋‹ค๋ฅธ ๊ณผ์ •์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Œ
  • ํ•œ๊ณ„๋Š” ์žˆ์ง€๋งŒ, ๋น ๋ฅธ ์‹œ๊ฐ„ ๋‚ด์— ๊ทผ์‚ฌํ•ด๋ฅผ ์ œ๊ณต ๊ฐ€๋Šฅ

16-2. ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ

1) ์‹ ์žฅ ํŠธ๋ฆฌ๋ž€?

  • ๋ชจ๋“  ์ •์ ์ด ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ณ  ๊ฐ„์„  ๊ฐœ์ˆ˜๊ฐ€ ์ •์  ๊ฐœ์ˆ˜๋ณด๋‹ค ํ•˜๋‚˜ ์ ์€ ๊ทธ๋ž˜ํ”„

2) ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(Minimum Spanning Tree; MST)๋ž€?

  • ์‹ ์žฅ ํŠธ๋ฆฌ ์ค‘ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ๊ฒฝ์šฐ
  • ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜
    • ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ž„์˜ ์ •์ ์—์„œ ์ตœ์†Œ ์ธ์ ‘ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๋Š” ์ •์ ์„ ์ฐพ์•„ ํ™•์žฅํ•˜๋Š” ๋ฐฉ์‹
    • ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ„์„ ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹

16-3. ๋ฐฐ๋‚ญ ๋ฌธ์ œ(Knapsack Problem; ๋ƒ…์ƒ‰ ๋ฌธ์ œ)

  • ์ง๋“ค์„ ์ž˜ ์กฐํ•ฉํ•˜์—ฌ ๋ฐฐ๋‚ญ์˜ ๋ฌด๊ฒŒ๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋‹ด์€ ๊ฐ€์น˜๋ฅผ ์ตœ๋Œ€๋กœ ํ•˜๋Š” ๋ฌธ์ œ

1) ์ง์„ ์ชผ๊ฐค ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„ ๋ฐฐ๋‚ญ ๋ฌธ์ œ

  • ๋ถ€๋ถ„ ๋ฐฐ๋‚ญ ๋ฌธ์ œ(Fractional Knapsack Problem) ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• : ๋ฌด๊ฒŒ ๋‹น ๊ฐ€์น˜๊ฐ€ ๋†’์€ ์ง์„ ์ตœ๋Œ€ํ•œ ๋งŽ์ด ๋„ฃ๋Š” ๊ทธ๋ฆฌํ”ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ

2) ์ง์„ ์ชผ๊ฐค ์ˆ˜ ์—†๋Š” 0/1 ๋ฐฐ๋‚ญ ๋ฌธ์ œ

  • ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•œ ์ตœ์ ํ•ด ๊ตฌํ•˜๊ธฐ ๋ถˆ๊ฐ€๋Šฅ
  • ๋™์  ๊ณ„ํš๋ฒ•์œผ๋กœ ์ ‘๊ทผ ํ•„์š”

16-4. ๋ชธํ’€๊ธฐ ๋ฌธ์ œ

  • ๋ฌธ์ œ80_๊ฑฐ์Šค๋ฆ„๋ˆ ์ฃผ๊ธฐ
def solution(amount):
    units = [1, 10, 50, 100]
    units.sort(reverse=True)
    changes = []
    
    for coin in units:
        while amount >= coin:
            changes.append(coin)
            amount -= coin
    return changes

# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”
print(solution(123)) # ๋ฐ˜ํ™˜๊ฐ’ : [100, 10, 10, 1, 1, 1]
print(solution(350)) # ๋ฐ˜ํ™˜๊ฐ’ : [100, 100, 100, 50]
  • ๋ฌธ์ œ81_๋ถ€๋ถ„ ๋ฐฐ๋‚ญ ๋ฌธ์ œ
def solution(items, weight_limit):
    for item in items:
        item.append(item[1] / item[0])
    items = sorted(items, key=lambda x:x[2], reverse=True)
    
    sum_value = 0
    for item in items:
        if weight_limit >= item[0]:
            sum_value += item[1]
            weight_limit -= item[0]
        else:
            if weight_limit == 0:
                break
            value = item[1] * weight_limit / item[0]
            sum_value += value
            break
    return sum_value

# TEST ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ’€๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”
print(solution([[10, 19], [7, 10], [6, 10]], 15)) # ๋ฐ˜ํ™˜๊ฐ’ : 27.33
print(solution([[10, 60], [20, 100], [30, 120]], 50)) # ๋ฐ˜ํ™˜๊ฐ’ : 240

16-5. ํ•ฉ๊ฒฉ์ž๊ฐ€ ๋˜๋Š” ๋ชจ์˜ ํ…Œ์ŠคํŠธ

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