• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Solve the 0/1 Knapsack Problem

laptop infographic online business 6087062

Introduction

Consider a set of items with a weight w and profit p associated with each one of them. If we have a knapsack of capacity W, we want to fill it with the maximum set of items that give us maximum profit. A fundamental constraint for this problem is that we can include an item or not, we cannot include fractions of it. This problem and its solution problem is the 0/1 Knapsack Problem. The 0/1 part highlights the fact that we can only include integral items.

Problem Example for 0/1 Knapsack Problem

We can consider the set of items as a bi-dimensional array, and in Java, we can write it for instance as an array literal like { {w1, p1}, {w2, p2}, …}. Consider the following input for the problem:

  • N = 3 items with the following values in terms of profit and weight:
    • { {1, 5}, {2, 15}, {3, 6} }
  • W = 10 capacity

We cannot include the {2, 15} item, because it exceeds the maximum weight. We cannot include both {1, 5} and {3, 6}, because the sum of their weights is greater than W. We can conclude that we can obtain the maximum profit by including {3, 6} alone.

Simple Solution for 0/1 Knapsack Problem

The most immediate solution that comes to mind is to consider all the possible sub-sets and choose the sub-set with the maximum profit. Performing such tasks recursively, in evaluating each single item we have to compare these two cases, to see which gives the maximum profit:

  • The current subset plus the current item if its weight is lower or equal to W (otherwise we continue with the next step of the recursion)
  • The current subset without the current item

The steps described above are shown in the following code snippet:

class KnapSack01UnitTest {

    @Test
    public void knapSack01Test() throws Exception {
        int items[][] = { { 1, 5 }, { 2, 15 }, { 3, 6 } };
        int W = 10;
        int N = items.length;
        System.out.println(knapSackEvaluation(W, items, N));
    }

    private int knapSackEvaluation(int W, int items[][], int N) {

        if (N == 0 || W == 0) {
            return 0;
        }

        if (items[N - 1][1] > W) {
            return knapSackEvaluation(W, items, N - 1);
        } else {
            return maxInteger(items[N - 1][0] + knapSackEvaluation(W - items[N - 1][1], items, N - 1), knapSackEvaluation(W, items, N - 1));
        }
    }

    private int maxInteger(int a, int b) { 
        return (a > b) ? a : b; 
    }

}

Performance Considerations for the Above 0/1 Knapsack Problem Algorithm

The time complexity of the above algorithm is O(2N).

Conclusion

In this article, we have seen a trivial way to solve the 0/1 Knapsack Problem. The example code is available on GitHub.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions