Lesson 29 +30 XP

0/1 Knapsack, Branch & Bound and the NP Classes (Beyond Syllabus)

0/1 Knapsack, Branch & Bound and the NP Classes (Beyond Syllabus)

🧭 Beyond syllabus. These topics come from the notes' practice-question slide and connect the "types of algorithms" from Unit I (dynamic programming, branch and bound) to real problems. They are covered fully in the Design & Analysis of Algorithms course.

1. The 0/1 Knapsack Problem

You have n items, item i has weight wi and value vi, and a bag of capacity W. Choose items (each wholly or not at all) to maximise total value without exceeding W.

Greedy by value/weight ratio does not always work for 0/1 knapsack (it does for the fractional version).

Dynamic Programming Solution

K[i][c] = best value using the first i items with capacity c:

  • If wi > c: K[i][c] = K[i-1][c] (item i does not fit).
  • Else: K[i][c] = max(K[i-1][c], vi + K[i-1][c - wi]) (skip it, or take it).
C
int knapsack(int n, int W, int w[], int v[]) {
    int K[n + 1][W + 1];
    for (int i = 0; i <= n; i++)
        for (int c = 0; c <= W; c++) {
            if (i == 0 || c == 0) K[i][c] = 0;
            else if (w[i - 1] > c) K[i][c] = K[i - 1][c];
            else {
                int take = v[i - 1] + K[i - 1][c - w[i - 1]];
                K[i][c] = take > K[i - 1][c] ? take : K[i - 1][c];
            }
        }
    return K[n][W];
}

Time and space O(nW).

Worked Example

Items (w, v): 1 (1, 1), 2 (3, 4), 3 (4, 5), 4 (5, 7); capacity W = 7.

i / c01234567
000000000
1 (1, 1)01111111
2 (3, 4)01145555
3 (4, 5)01145669
4 (5, 7)01145789

Answer: 9. Tracing back: K[4][7] = K[3][7], so item 4 is not taken; K[3][7] ≠ K[2][7], so item 3 is taken (capacity left 3); K[2][3] ≠ K[1][3], so item 2 is taken. Items 2 and 3 (weights 3 + 4 = 7, values 4 + 5 = 9).

2. Branch and Bound

Branch and bound explores a state-space tree where each level decides &quot;take item i or not&quot;. For each node it computes an upper bound on the best value reachable from it (for knapsack, the fractional-knapsack value of the remaining capacity). A node is pruned when its bound is not better than the best complete solution found so far.

  • Sort items by vi / wi so that the bound is tight.
  • Explore the most promising node first (best-first search with a priority queue).
  • Worst case is still exponential, but pruning usually cuts most of the tree.

The same idea solves the travelling salesman problem, job assignment and integer programming.

3. P, NP, NP-Complete and NP-Hard

ClassMeaningExamples
PSolvable in polynomial time (O(nk))Sorting, shortest paths, MST
NPA proposed solution can be checked in polynomial timeEvery problem in P; Sudoku; subset sum
NP-completeIn NP, and every NP problem reduces to itSAT, 3-SAT, vertex cover, 0/1 knapsack (decision version), Hamiltonian cycle
NP-hardAt least as hard as every NP problem (need not be in NP)Optimisation TSP, halting problem

Whether P = NP is the most famous open question in computer science. The knapsack DP above is O(nW) - polynomial in the value of W but not in the number of bits needed to write W, so it is called pseudo-polynomial and does not prove P = NP.

📝 Practice Questions

  • Q1. Solve the 0/1 knapsack instance above using dynamic programming and identify the chosen items.
  • Q2. Explain the branch-and-bound technique with reference to the knapsack problem.
  • Q3. Define P, NP, NP-complete and NP-hard with one example each.