YS's develop story

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

Algorithm

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

Yusang 2021. 8. 13. 09:18

๐Ÿ‘ฉ‍๐Ÿ’ป ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (ํƒ์š•๋ฒ•) ์ •๋ฆฌ with Python

๐Ÿฅ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Greedy Algorithm)์ด๋ž€? 

1. ๋งค ์ˆœ๊ฐ„ ์ตœ์ ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•ด์„œ ์ตœ์ข…์ ์ธ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

 

2. ๊ฐ๊ฐ์˜ ์„ ํƒ์€ ๊ทธ ์ˆœ๊ฐ„ ๋ณด์•˜์„ ๋•Œ๋Š” ๊ฐ€์žฅ ์ข‹์•„ ๋ณด์ด์ง€๋งŒ ๋‚˜์ค‘์—๋Š” ์ตœ์ ์˜ ํ•ด๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

3. ์ฆ‰ ๋งค ์ˆœ๊ฐ„์˜ ์„ ํƒ์€ locally optimal ํ•˜์ง€๋งŒ globally optimalํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ์— ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ตœ์ ํ•ด๋ฅผ ์–ป์„ ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ‹ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ 1 - ๋™์ „ ๋ฌธ์ œ

์ง€๋ถˆํ•ด์•ผ ํ•˜๋Š” ๊ฐ’์ด 7870์› ์ผ ๋•Œ ๊ฐ€์žฅ ์ ์€ ์ˆ˜์˜ ๋ˆ์œผ๋กœ ์ง€๋ถˆํ•˜๋Š” ๋ฐฉ๋ฒ•์„

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฐ€์žฅ ํฐ๋ˆ๋ถ€ํ„ฐ ์ตœ๋Œ€ํ•œ ์ง€๋ถˆํ•ด์•ผ ํ•˜๋Š” ๊ฐ’์„ ์ฑ„์šฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋˜๋ฉฐ
๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋งค ์ˆœ๊ฐ„ ์ตœ์ ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

๐ŸŠ ๋™์ „ ๋ฌธ์ œ ์ฝ”๋“œ

# Greedy1 - Coin Problem

coinList = [5000, 1000, 500, 100, 50, 10]


def greedy1(conList, value):
    conList.sort(reverse=True)
    totalCount = 0
    details = list()

    for i in conList:
        count = value // i
        totalCount += count
        value -= count * i
        details.append([i, count])

    return totalCount, details


print(greedy1(coinList, 7870))

 

๐Ÿ‰ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ 2 -  Fractional Knapsack Problem

๋ฌด๊ฒŒ ์ œํ•œ์ด k์ธ ๋ฐฐ๋‚ญ์— ์ตœ๋Œ€ ๊ฐ€์น˜๋ฅผ ๊ฐ€์ง€๋„๋ก ๋ฌผ๊ฑด์„ ๋„ฃ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๊ฐ ๋ฌผ๊ฑด์€ ๋ฌด๊ฒŒ w์™€ ๊ฐ€์น˜ v๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌผ๊ฑด์„ ์ชผ๊ฐค ์ˆ˜ ์žˆ๊ณ  ๋ฌผ๊ฑด์˜ ์ผ๋ถ€๋ถ„์„ ๋ฐฐ๋‚ญ์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‹ Fractional Knapsack Problem ์ฝ”๋“œ

# Greedy2 - Fractional Knapsack Problem

dataList = [(10, 10), (15, 12), (20, 10), (25, 8), (30, 5)]


def knapsack(dataList, capacity):
    dataList = sorted(dataList, key=lambda x: x[1] / x[0], reverse=True)
    details = list()
    totalValue = 0

    for i in dataList:
        if capacity - i[0] >= 0:
            capacity -= i[0]
            totalValue += i[1]
            details.append([i[0], i[1], 1])
        else:
            fractions = capacity / i[0]
            totalValue += fractions * i[1]
            details.append([i[0], i[1], fractions])
            break
    return totalValue, details


print(knapsack(dataList, 30))

 

๐Ÿฅญ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•œ๊ณ„

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ฐ˜๋“œ์‹œ ์ตœ์ ์˜ ํ•ด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์•„๋‹™๋‹ˆ๋‹ค.

๊ทธ ์ˆœ๊ฐ„์— ๊ฐ€์žฅ ์ข‹์•„ ๋ณด์ด๋Š” ์„ ํƒ์„ ํ†ตํ•ด ์ตœ์ ์˜ ํ•ด์— ๊ฐ€๊นŒ์šด ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ์— ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋Š” ์ตœ์ ํ•ด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„ ๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ leaf node๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์—์„œ...

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋‹ค๋ฉด 7 -> 12 = 19 ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์€ 10 -> 5์ด๋ฉฐ 15๊ฐ€ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. 

Comments