The Knapsack Problem
What Is an Optimization Problem?
An optimization problem means finding the best possible solution from many choices.
Usually, “best” means:
Real-Life Examples
How Do We Solve Optimization Problems?
Ask these two questions:
A. What are we trying to optimize?
Are we trying to:
B. What are the constraints?
Constraints are the rules or limits.
Examples:
Example: Traffic Lights
A city wants traffic lights to move cars efficiently.
Goal:
Maximize the number of cars that pass each hour.
Constraints:
The Knapsack Problem
Imagine you have a backpack with a weight limit.
You have several items. Each item has:
You want to choose items so that:
Example
Question:
Which items should you take?
What are we maximizing?
→ Total value
What is the constraint?
→ Total weight ≤ limit
Backpack limit = 10 kg
| Item | Weight | Value |
| ------ | ------ | ----- |
| Laptop | 6 | 9 |
| Camera | 4 | 7 |
| Jacket | 3 | 5 |
| Snacks | 2 | 4 |
Challenge:
If you could only carry 7 kg, what items would you choose?
Solutions to the Knapsack Problem
Shown in class:
Watch on your own:
Now do Lab 7 - the Grocery Problem.