Software Development Exam  >  Software Development Notes  >  DSA in C++  >  0/1 Knapsack Problem

0/1 Knapsack Problem | DSA in C++ - Software Development PDF Download

Introduction

The 0/1 Knapsack Problem is a classic algorithmic problem in computer science. It involves choosing items with specific weights and values to maximize the total value while keeping the total weight within a given limit. In this article, we will explore how to solve the 0/1 Knapsack Problem using dynamic programming in C++. We will provide simple explanations, example codes, and problem-solving techniques to help beginners understand this concept.

Understanding the 0/1 Knapsack Problem


Definition: Given a set of items, each with a weight and a value, we aim to determine the maximum value that can be obtained by selecting items while keeping the total weight within a given limit.

Assumption: Each item can be either selected (1) or not selected (0), hence the name "0/1 Knapsack Problem."

Objective: Maximize the total value of the selected items.

Solving the 0/1 Knapsack Problem using Dynamic Programming


Approach: We can use a dynamic programming technique to solve the 0/1 Knapsack Problem efficiently.

Key Idea: We build a 2D matrix (DP table) to store the maximum value that can be obtained for different combinations of items and knapsack weight limits.

Recursive Formula: The maximum value at a given position in the DP table is determined by either including the current item or excluding it.

Implementation in C++: Code Explanation and Output


#include <iostream>

using namespace std;

int knapsack(int weights[], int values[], int n, int capacity) {

    int dp[n + 1][capacity + 1];

    for (int i = 0; i <= n; i++) {

        for (int w = 0; w <= capacity; w++) {

            if (i == 0 || w == 0)

                dp[i][w] = 0;

            else if (weights[i - 1] <= w)

                dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]);

            else

                dp[i][w] = dp[i - 1][w];

        }

    }

    return dp[n][capacity];

}

int main() {

    int weights[] = {2, 3, 4, 5};

    int values[] = {3, 4, 5, 6};

    int capacity = 5;

    int n = sizeof(weights) / sizeof(weights[0]);

    int max_value = knapsack(weights, values, n, capacity);

    cout << "Maximum value: " << max_value << endl;

    return 0;

}

Explanation: The code above demonstrates the implementation of the 0/1 Knapsack Problem using dynamic programming in C++. We define a 'knapsack' function that takes arrays of weights and values, the number of items (n), and the knapsack capacity. It creates a 2D DP table and fills it iteratively using a nested loop. Finally, the function returns the maximum value that can be achieved.

Output: The code outputs the maximum value that can be obtained with the given items and knapsack capacity.

Sample Problems with Solutions


Problem 1:

Input:

  • weights = {2, 3, 4, 5}
  • values = {3, 4, 5, 6}
  • capacity = 5

Maximum value: 7

Selected items: {2, 3}

Problem 2:

Input:

  • weights = {1, 3, 4, 5}
  • values = {1, 4, 5, 7}
  • capacity = 7

Maximum value: 9

Selected items: {3, 4}

Conclusion

In this article, we explored the 0/1 Knapsack Problem and learned how to solve it using dynamic programming in C++. We provided an easy-to-understand explanation, example code, and output for better comprehension. By practicing with sample problems and understanding the underlying concepts, you can apply this knowledge to solve more complex problems efficiently. The 0/1 Knapsack Problem serves as a fundamental algorithmic problem and forms the basis for various other optimization problems.

The document 0/1 Knapsack Problem | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Semester Notes

,

mock tests for examination

,

study material

,

past year papers

,

Viva Questions

,

video lectures

,

Sample Paper

,

Previous Year Questions with Solutions

,

Important questions

,

0/1 Knapsack Problem | DSA in C++ - Software Development

,

Extra Questions

,

pdf

,

Summary

,

shortcuts and tricks

,

ppt

,

0/1 Knapsack Problem | DSA in C++ - Software Development

,

MCQs

,

Objective type Questions

,

Free

,

Exam

,

0/1 Knapsack Problem | DSA in C++ - Software Development

,

practice quizzes

;