Skip to content

[Rule] Knapsack to ILP #639

@GiggleLiu

Description

@GiggleLiu

Source

Knapsack

Target

ILP (Integer Linear Programming)

Motivation

  • Connects the orphan Knapsack problem to the main reduction graph (ILP has 13 inbound reductions)
  • Enables solving Knapsack instances using any ILP solver
  • Classic, well-known textbook reduction

Reference

Papadimitriou, C. H., & Steiglitz, K. (1982). Combinatorial Optimization: Algorithms and Complexity. Prentice-Hall. (Section on Integer Programming formulations of combinatorial problems)

Reduction Algorithm

Given a Knapsack instance with n items, weights $w_1, \ldots, w_n$, values $v_1, \ldots, v_n$, and capacity $C$:

  1. Create $n$ binary variables $x_1, \ldots, x_n$, where $x_i = 1$ means item $i$ is selected.
  2. Set the objective function to maximize $v_1 x_1 + v_2 x_2 + \cdots + v_n x_n$.
  3. Add one constraint: $w_1 x_1 + w_2 x_2 + \cdots + w_n x_n \le C$.
  4. Each variable is bounded: $0 \le x_i \le 1$ (binary).

Solution extraction: The binary vector $(x_1, \ldots, x_n)$ directly maps back — item $i$ is selected if $x_i = 1$.

Size Overhead

Target metric Formula
num_vars num_items
num_constraints 1

Validation Method

Closed-loop test: construct a Knapsack instance, reduce to ILP, solve ILP with brute force, extract solution back to Knapsack, and verify optimality.

Example

Source (Knapsack): 4 items, weights = (1, 3, 4, 5), values = (1, 4, 5, 7), capacity = 7.

Target (ILP):

  • Variables: $x_1, x_2, x_3, x_4 \in {0, 1}$
  • Maximize: $x_1 + 4x_2 + 5x_3 + 7x_4$
  • Subject to: $x_1 + 3x_2 + 4x_3 + 5x_4 \le 7$

Optimal ILP solution: $(0, 1, 1, 0)$ with objective value 9.
Extracted Knapsack solution: Select items 2 and 3, total weight = 7, total value = 9.

This example is non-trivial because a greedy-by-value approach would pick item 4 (highest value = 7), leaving only room for item 1, yielding value = 8 < 9.

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoodAn issue passed all checks.ruleA new reduction rule to be added.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions