Ludium
Sign In
Introduction to Computational Thinking and Data Science
Optimization
01Optimization Models and the Knapsack Problem02Brute-Force Knapsack and the Exponential Power Set03Greedy Algorithms: One Function, Any Definition of Best04Lambda Keys and Why Greedy Can Miss the OptimumProblem set0/10MIT problem set0/2
01The Knapsack Search Tree: Every Combination, 2ⁿ Nodes02Recursive maxVal: An Exhaustive Search That Beats Greedy03Memoization: Turning Slow Fibonacci into fastFib04Overlapping Subproblems and the Memoized KnapsackProblem set0/10MIT problem set0/2

The Knapsack Search Tree: Every Combination, 2ⁿ Nodes

Why can't greedy promise the best knapsack, and how does a take/don't-take tree check every combination at a cost of 2ⁿ⁺¹ − 1 nodes?


Finger exercises

Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.

0 / 5 passed
Exercise 1 of 5

Nodes at each depth

A take/don't-take search tree is built over 5 items. Build the list level_counts whose entry at index iii is the number of nodes at depth iii, from the root (depth 0) down to the leaves (depth 5). Then assign the total number of nodes in the tree to total.

1
2
3
4
5
n = 5
level_counts = ...  # nodes at depth 0, 1, ..., n
total = ...  # your code here

Python runs in your browser
⌘/Ctrl + Enter runs
?
level_counts is a list with one entry per level
?
each level doubles, starting from one root
?
total counts every node in the tree
←Previous Lambda Keys and Why Greedy Can Miss the OptimumNext Recursive maxVal: An Exhaustive Search That Beats Greedy →