
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Solve Knapsack Problem Using Dynamic Programming
This is a C++ program to solve 0-1 knapsack problem using dynamic programming. In 0-1 knapsack problem, a set of items are given, each with a weight and a value. We need to determine the number of each item to include in a collection so that the total weight is less than or equal to the given limit and the total value is large as possible.
Algorithm
Begin Input set of items each with a weight and a value Set knapsack capacity Create a function that returns maximum of two integers. Create a function which returns the maximum value that can be put in a knapsack of capacity W int knapSack(int W, int w[], int v[], int n) int i, wt; int K[n + 1][W + 1] for i = 0 to n for wt = 0 to W if (i == 0 or wt == 0) Do K[i][wt] = 0 else if (w[i - 1] <= wt) Compute: K[i][wt] = max(v[i - 1] + K[i - 1][wt - w[i - 1]], K[i -1][wt]) else K[i][wt] = K[i - 1][wt] return K[n][W] Call the function and print. End
Example Code
#include <iostream> using namespace std; int max(int x, int y) { return (x > y) ? x : y; } int knapSack(int W, int w[], int v[], int n) { int i, wt; int K[n + 1][W + 1]; for (i = 0; i <= n; i++) { for (wt = 0; wt <= W; wt++) { if (i == 0 || wt == 0) K[i][wt] = 0; else if (w[i - 1] <= wt) K[i][wt] = max(v[i - 1] + K[i - 1][wt - w[i - 1]], K[i - 1][wt]); else K[i][wt] = K[i - 1][wt]; } } return K[n][W]; } int main() { cout << "Enter the number of items in a Knapsack:"; int n, W; cin >> n; int v[n], w[n]; for (int i = 0; i < n; i++) { cout << "Enter value and weight for item " << i << ":"; cin >> v[i]; cin >> w[i]; } cout << "Enter the capacity of knapsack"; cin >> W; cout << knapSack(W, w, v, n); return 0; }
Output
Enter the number of items in a Knapsack:4 Enter value and weight for item 0:10 50 Enter value and weight for item 1:20 60 Enter value and weight for item 2:30 70 Enter value and weight for item 3:40 90 Enter the capacity of knapsack100 40
- Related Articles
- C++ Program to Solve the Fractional Knapsack Problem
- C++ Program to Solve the 0-1 Knapsack Problem
- Fractional Knapsack Problem
- Python Program for 0-1 Knapsack Problem
- Program to implement the fractional knapsack problem in Python
- C++ Program to Perform Optimal Paranthesization Using Dynamic Programming
- C++ Program to Find Fibonacci Numbers using Dynamic Programming
- 0-1 Knapsack Problem in C?
- C++ Program to Find Factorial of a Number using Dynamic Programming
- Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm
- C++ program to Solve Tower of Hanoi Problem using Binary Value
- Python Program to solve Maximum Subarray Problem using Divide and Conquer
- C++ Program to Solve N-Queen Problem
- Introduction to Dynamic Programming
- C++ Program to Solve the Dominating Set Problem

Advertisements