Knapsack Problem Example: Simple Explanation and Solution

Logan Jul 13, 2026

Ever found yourself in a situation where you have a limited capacity, say a backpack, and you need to pack items of varying sizes and weights? You want to maximize the use of your space, carrying the most valuable items. This is a real-world example of the Knapsack Problem, a classic optimization challenge in Computer Science.

0-1 Knapsack Problem
0-1 Knapsack Problem

The Knapsack Problem, introduced by American mathematician Donald Knuth in 1974, is a combinatorial optimization problem where you're given a set of items, each with a weight and a value, and you want to determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit, and the total value is as large as possible.

a man with a backpack is looking at the water
a man with a backpack is looking at the water

Understanding the Knapsack Problem

The Knapsack Problem can be categorized into two types: 0/1 Knapsack and Unbounded Knapsack. In the 0/1 variant, you can only take each item once, while in the Unbounded variant, you can take as many instances of an item as you want.

Solving the Knapsack Problem with Code Examples
Solving the Knapsack Problem with Code Examples

Let's break down the problem further with examples for better understanding.

0/1 Knapsack Problem

Fractional Knapsack Problem Example Solution — Greedy Algorithm for Maximum Profit
Fractional Knapsack Problem Example Solution — Greedy Algorithm for Maximum Profit

In this version, each item can only be included or excluded from the knapsack. Imagine you're going on a hiking trip and you can only pack one of each item. Here's how it might look:

  • Item: Water bottle, Raincoat, Tent, Sleeping bag, First aid kit
  • Weight (in lbs): 2, 1.5, 7, 5, 3
  • Value (in utils): 3, 2, 30, 15, 4

You have a backpack with a weight limit of 10 lbs. The goal is to maximize the total value you can carry while keeping the total weight under 10 lbs. One possible solution could be to carry the Water bottle, Raincoat, and First aid kit, giving a total value of 9 utils.

How to solve KnapSack problem in Java using Dynamic Programming? Example Tutorial
How to solve KnapSack problem in Java using Dynamic Programming? Example Tutorial

Unbounded Knapsack Problem

In this version, you can take as many instances of an item as you want. Now, imagine you're packing for a week-long hiking trip and you want to maximize the total value of items you can carry, without exceeding your weight limit.

Using the same items as above, you'll now consider how many of each item you need. For instance, you might choose 4 water bottles, 1 raincoat, 1 tent, 1 sleeping bag, and 1 first aid kit, for a total value of 24 utils while keeping the weight under 10 lbs.

Improvised Backpack, Survival Pants, Apocalypse Survival Guide, How To Pack A Survival Backpack, Backpack Survival, Survival Bag Drawing, Survival Backpack Drawing, How To Pack A Rucksack Diagram, Wilderness Survival Tools
Improvised Backpack, Survival Pants, Apocalypse Survival Guide, How To Pack A Survival Backpack, Backpack Survival, Survival Bag Drawing, Survival Backpack Drawing, How To Pack A Rucksack Diagram, Wilderness Survival Tools

Solving the Knapsack Problem

Solving the Knapsack Problem involves finding the optimal combination of items that maximizes the total value without exceeding the weight limit. Algorithms like Dynamic Programming and Greedy algorithms can be used to solve this problem efficiently.

Safety Backpack For Toddlers, Kids Backpack Buying Guide, Kids Safety Gear Ideas, Backpack Safety Chiropractic, Backpack Parts Names, School Safety Equipment Guide, How To Choose A School Backpack, Back Safety, How To Protect Backpacks
Safety Backpack For Toddlers, Kids Backpack Buying Guide, Kids Safety Gear Ideas, Backpack Safety Chiropractic, Backpack Parts Names, School Safety Equipment Guide, How To Choose A School Backpack, Back Safety, How To Protect Backpacks
Anatomy Of A Backpack, Anatomy Of A Bag, Backpack Parts Names, Backpack Customization Tips, Military-style Backpack Modifications, Designing A Backpack, How To Design A Backpack, What’s In Your Backpack, How To Choose A Military Backpack
Anatomy Of A Backpack, Anatomy Of A Bag, Backpack Parts Names, Backpack Customization Tips, Military-style Backpack Modifications, Designing A Backpack, How To Design A Backpack, What’s In Your Backpack, How To Choose A Military Backpack
Knapsack Problem — Dynamic Programming Examples Solved Step by Step
Knapsack Problem — Dynamic Programming Examples Solved Step by Step
Makeshift Backpack, How To Make Backpacking Gear, How To Make Your Own Backpack, Diy Hiking Backpack, How To Pack A Rucksack, How To Pack Backpack, How To Backpack, Diy Military Backpack Modifications, Backpack Diagram
Makeshift Backpack, How To Make Backpacking Gear, How To Make Your Own Backpack, Diy Hiking Backpack, How To Pack A Rucksack, How To Pack Backpack, How To Backpack, Diy Military Backpack Modifications, Backpack Diagram
Unheralded Hero: The Inventor Whose Knapsack Saved Union Soldiers’ Backs
Unheralded Hero: The Inventor Whose Knapsack Saved Union Soldiers’ Backs
Fractional Knapsack | Solution in C++ and Python | Example
Fractional Knapsack | Solution in C++ and Python | Example
Cool Knapsacks. Vintage Army Backpack Collection, Vintage Army Backpack Set, Ww2 Backpacks, Ww2 Backpack, Military Backpack Collection, British Military Knapsack, Collection Of Military Pouches, Military Haversack Contents, French Army Backpack Military
Cool Knapsacks. Vintage Army Backpack Collection, Vintage Army Backpack Set, Ww2 Backpacks, Ww2 Backpack, Military Backpack Collection, British Military Knapsack, Collection Of Military Pouches, Military Haversack Contents, French Army Backpack Military
an image of a diagram of the inside of a human body with parts labeled in red
an image of a diagram of the inside of a human body with parts labeled in red
an image of a green backpack and its parts labeled in the diagram above it,
an image of a green backpack and its parts labeled in the diagram above it,
5 dumb things backpack designers need to stop doing
5 dumb things backpack designers need to stop doing
the back and side view of a man's body, with instructions on how to use
the back and side view of a man's body, with instructions on how to use
10 Rules of Good Backpack Ergonomics Illustrated (Just in Time for School) Backpack Ideas For Students, Backpack Packing Tips For Students, Functional Backpack For On-the-go, High School Hacks Backpack Essentials, Functional Large Capacity Backpack For On-the-go, Choosing The Right Backpack Size, How To Choose School Backpacks, How To Choose A School Backpack, Functional Backpack With Large Capacity For On-the-go
10 Rules of Good Backpack Ergonomics Illustrated (Just in Time for School) Backpack Ideas For Students, Backpack Packing Tips For Students, Functional Backpack For On-the-go, High School Hacks Backpack Essentials, Functional Large Capacity Backpack For On-the-go, Choosing The Right Backpack Size, How To Choose School Backpacks, How To Choose A School Backpack, Functional Backpack With Large Capacity For On-the-go
Custom Knitted Beanies Manufacturer | Huayi Yuanmeng Factory China
Custom Knitted Beanies Manufacturer | Huayi Yuanmeng Factory China
four pictures of backpacks with various items attached to the front and back straps on them
four pictures of backpacks with various items attached to the front and back straps on them
Hidden Zipper, Waterproof, & Kevlar Reinforced Backpack
Hidden Zipper, Waterproof, & Kevlar Reinforced Backpack
Why a MOLLE Assault Pack Is the Best Solution for Tactical Versatility
Why a MOLLE Assault Pack Is the Best Solution for Tactical Versatility
What are the parts of a backpack? A bag anatomy with diagrams.
What are the parts of a backpack? A bag anatomy with diagrams.
asonepict illustration
asonepict illustration
How Much Weight Should You Carry for Rucking?
How Much Weight Should You Carry for Rucking?
How To Turn Trash Into Fashion
How To Turn Trash Into Fashion

The complexity of the problem grows exponentially with the number of items, so it's critical to use effective algorithms. For instance, the 0/1 Knapsack Problem is NP-complete, meaning there's no known efficient solution. However, Dynamic Programming can help reduce the time complexity from exponential to polynomial.

Dynamic Programming Approach

Dynamic Programming is a technique for solving complex problems by breaking them down into simpler subproblems. In the context of the Knapsack Problem, it involves creating a table to store the maximum value that can be achieved with a certain weight, then iterating through all the items, updating the table based on whether or not to include the current item.

For example, using the 0/1 Knapsack Problem and the provided items, you'd start by initializing a 2D table with zeros, then iterate through the items and weights, updating the table based on whether including the current item would increase the total value without exceeding the weight limit. The final answer would be the maximum value obtained for the given weight limit.

Greedy Approach

Greedy algorithms make locally optimal choices with the hope of finding a globally optimal solution. For the Unbounded Knapsack Problem, a greedy approach could start by packing the item with the highest value-to-weight ratio first, then move on to the next item, and so on, until the weight limit is reached. This approach doesn't guarantee an optimal solution, as it may miss better combinations later in the list, but it can be a good heuristic for certain problems.

For example, using the Unbounded Knapsack Problem and the provided items, you'd first pack 5 Water bottles (for a total value of 6 utils and weight of 10 lbs), then move on to other items, but since the weight limit is already reached, no other items can be packed. This might not be the optimal solution, but it's a quick and efficient heuristic.

In the world of logistics, manufacturing, and trading, the Knapsack Problem is not just a theoretical exercise. It's a real challenge that companies face every day. From shipment planning to inventory management, the ability to find the optimal combination of items to maximize value while respecting constraints can mean significant savings and increased efficiency.

So, the next time you're packing your backpack, remember you're engaged in a simpler version of the Knapsack Problem. And if you're a decision-maker in a business, consider the deeper implications of the Knapsack Problem for your operations. The quest for efficiency and effectiveness never ends, and the insights from this classic problem can help guide you there.