[CreativeProgramming]Lecture12_Optimization Problems
[CreativeProgramming]Lecture12_Optimization Problems
Spring 2025
CUL1122 Lecture #12
Optimization Problems
Today
❖Computational Models
❖Optimization Models and their Solutions
▪ Brute Force Algorithm
▪ Greedy Algorithm
❖Knapsack Problem
▪ 0/1 Knapsack Problem vs. Fractional Knapsack Problem
▪ Solving the 0/1 Knapsack Problem: Using Brute Force and Greedy Algorithms
❖Exercise: Solving the 0/1 Knapsack Problem
3
Computational Models
4
Computational Models
5
Optimization Models
❖The goal is to find the optimal solution that satisfies given conditions.
❖You are given constraints and an objective function for optimal choices.
▪ Constraints must be strictly followed.
➢When going to Boston, arrive before 5 p.m. and spend $100 or less.
▪ The objective function should be maximized or minimized.
➢Minimizing travel costs from New York to Boston.
➢Minimizing travel time from New York to Boston.
6
Strategy 1: Brute Force Algorithm
7
Strategy 2: Greedy Algorithm
❖Selecting the ‘best’ option at each step to reach the optimal solution.
❖Advantages:
▪ Fast speed and simple implementation. ① ③ ②
▪ Can often find efficient solutions.
❖Disadvantages:
▪ No guarantee of global optimum;
may get stuck in local optima. ④
▪ Uncertain how good a local optimum is.
❖Example: Maximizing tutorials in 10 hours.
▪ Select tutorials with the shortest duration first.
Images © source: https://studyalgorithms.com/theory/algorithmic-paradigms-brute-force/
8
Brute Force Algorithm vs. Greedy Algorithm
9
Knapsack Problem: Motivation
❖Problem: A thief who has broken into someone’s house wants to pack
items, but the bag’s size is a concern.
❖Solution: Choose items with the highest value possible to pack.
10
Knapsack Problem: Definition
11
Knapsack Problem: Objective
12
Knapsack Problem: Types
13
0/1 Knapsack Problem: Design
14
0/1 Knapsack Problem: Objective
15
Strategy 1: Brute Force Algorithm
16
Strategy 1: An Often Unrealistic Approach
17
Strategy 2: Greedy Algorithm
❖If there’s still room in the knapsack,
▪ Let’s pack the ‘best’ item at the moment!
❖What does ‘best’ mean?
▪ Among the unpacked items:
▪ 1) By value: Choose the item with the
highest value within capacity.
▪ 2) By cost: Choose the item with the
lowest weight within capacity.
▪ 3) By value-to-weight ratio: Choose the
item with the highest value-to-weight
ratio within capacity. An example of selecting based on
value-to-weight ratio.
18
Strategy 2: A Practical Approach
❖The greedy algorithm always selects the ‘best’ option at the current
moment.
❖This approach allows us to find a local optimum, and it’s hoped that the
local optimum will be the global optimum.
▪ A local optimum doesn’t necessarily match the global optimum.
▪ However, it can still be a sufficiently good solution even if it’s not the global
optimum.
global optimal
local optimal local optimal
❖At a laundromat, you need to collect the exact amount of coins needed
to pay for the washing and drying machines.
❖Create a Python script that calculates the minimum number of coins
needed to reach a specific amount.
22
Exercise 1: Implementation
❖1) Generate a list of coins and their names based on the amount.
▪ coins = [25, 10, 5, 1]
❖2) Also, generate a list of the corresponding coin names.
▪ names = [‘Quarters’, ‘Dimes’, ‘Nickels’, ‘Pennies’]
❖3) Traverse the sorted list and if there is still an amount to fill,
▪ Calculate how many coins of the current amount are needed.
▪ Subtract the amount that can be paid with the current coin from the remaining
amount.
▪ For example, if it’s 98 cents, you’ll need 3 quarters, and the remainder will be
23 cents.
23
Exercise 2: Choosing Survival Items
24
Exercise 2: 1) Writing the Item Class
❖Create an Item object for each survival item and add it to the list.
def build_list(names, values, weights):
l = []
for i in range(len(names)):
l.append(Item(names[i], values[i], weights[i]))
return l
26
Exercise 2: 3) Defining and Calling Greedy Function
27
Exercise 2: 3) Defining and Calling Greedy Function
▪ While traversing the sorted list, if there is remaining capacity,
➢Add current item to the list of packed items, increase total value and weight.
28
수고하셨습니다!
29