Class KSAlgo

java.lang.Object
org.ddolib.examples.layered.knapsack.KSAlgo

public class KSAlgo extends Object
Utility class providing algorithms to solve the Knapsack Problem.
  • Method Details

    • greedyKS

      public static Solution greedyKS(KSProblem problem)
      Provides a greedy approximation for the Knapsack Problem.

      The algorithm sorts items in descending order of their profit-to-weight ratio ( profit / weight) and selects items sequentially as long as they fit within the remaining capacity.

      Parameters:
      problem - the Knapsack Problem instance to solve
      Returns:
      the selected items (1 if item i is taken, 0 otherwise). As the problem is modeled as a minimization, its value is minus the total profit and provides an upper bound on the optimal value.