!DOCTYPE html> 0/1 Knapsack Visualizer

Main Panel

Use the tabs to switch between focused page-like views.

DP Visualizer

Press Build Steps to generate both phases: table fill and traceback.
Optimal Value
—
Total Weight
—
Chosen Items
—
0/1 Vector
—

Plain-English Step Explanation

Build the steps first. Then this box will explain the forward table fill and backward traceback.

Pseudocode

Step History

StepPhaseCellItemDecisionValueExplanation

Complexity

This page teaches what the runtime means, why DP is faster than brute force here, and how to read the chart without heavy math words.

What is the algorithm doing?

Big idea first

n = 4 W = 8 states = 45 pseudo-polynomial
✓
Best Case
O(n × W)
All (n+1)×(W+1) cells are still filled in the standard bottom-up DP table.
≈
Average Case
O(n × W)
Cell count stays tied to the table size, so runtime remains proportional to nW.
↑
Worst Case
O(n × W)
Even harder inputs still scan the same DP grid, with no exponential branching in this method.
□
Space
O(n × W)
This page stores the full matrix. A rolling-array variant can reduce space to O(W).
2ⁿ
Brute Force
O(2ⁿ)
It checks every subset and sums weights and values, so it becomes impractical very quickly.

Algorithm Comparison Summary

This short table makes the CLO-1 comparison explicit: the current project shows why DP is correct for 0/1 knapsack, why greedy is only a heuristic here, and why brute force becomes expensive.
ApproachMain ideaTimeSpaceOptimal for 0/1 knapsack?What this project shows

What the chart is trying to show

n = 4
W = 8
n decreases—
n increases—
W decreases—
W increases—
measured DP operation count in this app
DP state count ≈ nW
fractional greedy sort ≈ n log n
subset count for brute force = 2^n

How to understand the chart

Why the runtime label does not change

Plain-English explanation

Think of the DP table like a grid. The algorithm still moves across the whole grid, so the main runtime stays O(nW).
Some cases look slower only because more cells need the extra "include this item" check. That changes the amount of work inside the loop, not the overall runtime category.
When the table is done, the algorithm walks backward to recover the answer. That traceback part is much smaller: O(n).

Keep this in mind

These explanations match the actual version shown in this visualizer: a full 2D DP table is stored.
You may learn another knapsack version later that uses only O(W) space. That is real, but it is not the one being visualized here.
For brute force, the important idea is simple: the number of possible subsets explodes as 2^n, which is why it becomes impractical much faster.

Text Explanation

This page is organized for teaching: start with the quick guide, open the demos, then use the worked example and paradigm block to explain why DP is the correct method.

1. Start here

Open the first demo and press Step through greedy a few times. Students will see the tempting local choices first.

2. Reveal the answer

Then press Show DP decision. This lets you explain that DP checks both include and exclude before storing the better value.

3. Use the rest of the page

After the first demo, move to the worked example, sorting/searching section, and paradigm demo to reinforce the same idea with simpler wording.

Worked Example: one state explained in plain English

How to read one DP state

Sorting / Searching in this Algorithm

How this implementation uses them

Why that matters

A greedy ratio order is a good first thought, but it is not a correctness proof for 0/1 knapsack.
DP compares include and exclude at every relevant state, so it can undo an attractive-looking local choice.
After the table is finished, traceback performs a search through the table to recover the chosen subset.

Sorting / Searching Role Demo

Impact analysis

The broken traceback option is a teaching demo only. It is intentionally wrong and is never used by the real solver.

Algorithmic Paradigm Demo

Label: this algorithm uses Dynamic Programming.
Why: it fills a table of overlapping subproblems and reuses earlier answers instead of recomputing them.

Comparison / counterexample

Current / active state
Include / chosen branch
Exclude / alternative branch
Overlapping / reused subproblem
Greedy failure / bad branch

Brute Force

Exhaustive subset search is contrasted here with the decision tree.

Brute-Force Result and Decision Tree

Brute-force result for current input

Why brute force is bad here

Tree controls

Idea: brute force branches on every item: include it or exclude it.
Worst-case size: for n items, the tree has about 2^(n+1)-1 nodes and 2ⁿ leaves.

Depth shown

Showing first 4 decisions