• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

ReviewsLion

Reviews of online services and software

  • Hosting
  • WordPress Themes
  • SEO Tools
  • Domains
  • Other Topics
    • WordPress Plugins
    • Server Tools
    • Developer Tools
    • Online Businesses
    • VPN
    • Content Delivery Networks

What is the formula for 0-1 knapsack?

The 0-1 Knapsack Problem is one of the most classic and well-known problems in computer science and optimization. It is a dynamic programming example where the objective is to determine the most valuable subset of items to include in a knapsack, given a constraint on the total weight the knapsack can carry. This problem has significant practical applications — from resource allocation to budgeting — and serves as a cornerstone in understanding computational efficiency and optimization techniques.

In the 0-1 Knapsack Problem, each item has a weight and a value. The goal is to maximize the sum of the values of the items selected without exceeding the knapsack’s capacity. The “0-1” part refers to the condition that each item can either be taken (1) or not taken (0), meaning you cannot take a fraction of an item nor take the same item multiple times.

Table of contents:
  • The Mathematical Formula
  • Tabular or Bottom-Up Implementation
  • Optimizing Space
  • Frequently Asked Questions (FAQ)

The Mathematical Formula

The 0-1 knapsack problem is usually expressed using a dynamic programming formula that builds an optimal solution progressively. Suppose:

  • n is the number of items
  • W is the maximum weight the knapsack can carry
  • w[i] is the weight of the i-th item
  • v[i] is the value of the i-th item
  • dp[i][j] represents the maximum value that can be obtained by choosing from the first i items when the available capacity is j

The recurrence relation for the 0-1 Knapsack Problem is as follows:


if w[i] > j:
    dp[i][j] = dp[i-1][j]
else:
    dp[i][j] = max(dp[i-1][j], dp[i-1][j - w[i]] + v[i])

This recurrence states that if the item’s weight is more than the current capacity j, we cannot include it. So, the best we can do is to carry forward the previous value without including this item. Otherwise, we check two scenarios — one where we include the item and one where we don’t — and take the maximum result.

Tabular or Bottom-Up Implementation

Using a 2D table to store subproblem results, we fill the table row by row. The dimensions will be (n+1) x (W+1). The table is initialized with zeros in the beginning.

Here’s a sample structure for building the solution:


for i in range(1, n+1):
    for j in range(1, W+1):
        if w[i] > j:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i])

Once the table is filled, the bottom-right cell will contain the maximum value that can be obtained within the given weight limit — i.e., dp[n][W].

Optimizing Space

The standard implementation uses O(nW) space. However, this can be optimized to O(W) by using a 1D array and updating it in reverse to ensure the dependencies from the previous iteration are maintained.

Here’s a basic concept of space optimization:


dp = [0 for j in range(W+1)]
for i in range(1, n+1):
    for j in range(W, w[i]-1, -1):
        dp[j] = max(dp[j], dp[j - w[i]] + v[i])

This method dramatically reduces memory usage, especially crucial in real-time systems or memory-constrained environments.

Frequently Asked Questions (FAQ)

  • Q: What is the difference between 0-1 Knapsack and Fractional Knapsack?
    A: In 0-1 Knapsack, an item can either be included fully or not at all. In Fractional Knapsack, items can be broken into smaller parts, and fractions can be taken to maximize value.
  • Q: Can the 0-1 Knapsack be solved using recursion?
    A: Yes, but it is inefficient without memoization due to repeated computations. It is better solved using Dynamic Programming.
  • Q: What is the time complexity of the dynamic programming solution?
    A: The basic dynamic programming approach has a time complexity of O(nW), where n is the number of items and W is the knapsack capacity.
  • Q: Is it possible to retrieve the selected items after solving?
    A: Yes. By tracing back the DP table from dp[n][W], one can determine which items are included in the optimal solution.

Filed Under: Blog

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent posts

Top 7 Lightweight Tools to Improve Video Playback & Media Handling on Facebook (background play, speed control, better video UI)

AlliedBarton jobs: Security Officer Career Opportunities

Jobs accounting jobs: Top Certifications to Boost Your Career

Best 5 Privacy-Focused Addons for Facebook Browsing That Combine Tracker-Blocking + Ad-Free Feed (uBlock Origin, Privacy Badger & More)

Top 9 Emerging AI Tools for Knowledge Management (tagging, search, summarization) That Remote Teams Use to Keep Docs Organized and Accessible

Top 5 AI Email/Cover-Letter/Bio Writers That Freelancers & Job-Seekers Use to Create Personalized Outreach Without Starting From Blank

Top 8 Editors With Easy Export & Format Support (4K, 60 FPS, Mobile-Ready) Preferred by Reddit YouTubers Running Multi-Platform Channels

Top 8 Code Snippet & Boilerplate Generators That Help Freelancers Quickly Bootstrap Projects Without Starting From Scratch Each Time

Top 5 Software for Editing Vertical Videos for TikTok / Reels — Reddit Users Who Switched From Desktop to Mobile Swear By These

What to Do When a TikTok Ads Manager Integration Fails to Sync Ad Spend — How Marketers Regained Analytics Accuracy

Footer

WebFactory’s WordPress Plugins

  • UnderConstructionPage
  • WP Reset
  • Google Maps Widget
  • Minimal Coming Soon & Maintenance Mode
  • WP 301 Redirects
  • WP Sticky

Articles you will like

  • 5,000+ Sites that Accept Guest Posts
  • WordPress Maintenance Services Roundup & Comparison
  • What Are the Best Selling WordPress Themes 2019?
  • The Ultimate Guide to WordPress Maintenance for Beginners
  • Ultimate Guide to Creating Redirects in WordPress

Join us

  • Facebook
  • Privacy Policy
  • Contact Us

Affiliate Disclosure: This page may have affiliate links. When you click the link and buy the product or service, I’ll receive a commission.

Copyright © 2025 · Reviewslion

  • Facebook
Like every other site, this one uses cookies too. Read the fine print to learn more. By continuing to browse, you agree to our use of cookies.X