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

This doc is part of
153 videos|115 docs|24 tests
Join course for free

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

Download the notes
0/1 Knapsack Problem
Download as PDF
Download as PDF

#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

Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

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
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
153 videos|115 docs|24 tests

Up next

153 videos|115 docs|24 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

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

,

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

,

Free

,

practice quizzes

,

Previous Year Questions with Solutions

,

study material

,

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

,

shortcuts and tricks

,

Objective type Questions

,

Summary

,

ppt

,

Viva Questions

,

Sample Paper

,

past year papers

,

MCQs

,

Extra Questions

,

video lectures

,

mock tests for examination

,

Exam

,

Important questions

,

Semester Notes

,

pdf

;