Question

In: Computer Science

C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and...

C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and no vectors. Thank you

Write a program that solves the knapsack problem recursively for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. Hint: The arguments to the knapsack function are target weight and the array index where the remaining items start. The knapsack problem in its simplest form involves trying to fit items of different weights into a knapsack so that the knapsack ends up with a specified total weight. You need to fit in all items. For example if you want your knapsack to weigh exactly 20 pounds and you have five items with weights of 11,8,7,6 and 5 pounds. In this case only combination that works is 8, 7 and 5 pounds.

Solutions

Expert Solution

The Knapsack problem is a combitoraial optimization problem used to show  both problem and solution.It can be solved by greedy method approach.

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 Weight and the total value is large as possible.

/* A  recursive implementation of Knapsack problem using array */

#include <bits/stdc++.h> 
using namespace std; 
  

int maximum(int a, int b) { return (a > b) ? a : b; } 
  
// Returns the maximum value that 
 
int knapSack(int W, int weight[], int values[], int n) 
{ 
  
    // Base Case 
    
    if (n == 0 || W == 0) 
        return 0; 
  
    // If weight of the nth item is more 
    // than Knapsack capacity W, then 
    // this item cannot be included 
    // in the optimal solution 
    
    if (weight[n] > W) 
        return knapSack(W, weight, values, n - 1); 
  
     
    else
        return maximum( 
            values[n] + knapSack(W - weight[n],  
                                    weight, values, n - 1), 
            knapSack(W, weight, values, n - 1)); 
} 
  
// Driver code 

int main() 
{ 
    int values[] = { 10, 20, 30, 10, 50 };  //values of the items
    int weight[] = { 11, 8, 7, 6, 5 };       // values of the weight given
    
    int W = 20;    //capacity of weight
    
    int n = sizeof(values) / sizeof(values[0]); 
    cout << " knapSack value is " << knapSack(W, weight, values, n); 
    
    return 0; 
}

OUTPUT:-

value[] = { 10, 20, 30, 10, 50} Weight= 11; Value=10; And here the Combination that works is--

weight[] = { 11, 8, 7, 6, 5 }   Weight= 8; Value=20; Weight=( 8+7+5 ); Value=(20+30+50);

W = 20   Weight= 7; Value=30;

solution : 100   Weight= 6; Value=10;

  Weight= 5; Value=50;

  

--------------------------------------------------Thank You---------------------------------------------------------------------------   

  

  


Related Solutions

C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array of 10 characters storing a gene sequence. You will be given a subsequence of 3 characters to look for within this array. If the subsequence is found, print the message: Subsequence <XXX> found at index <i>. Where i is the starting index of the subsequence in the array. Otherwise, print Subsequence <XXX> not found. The array of characters and the subsequence will be given through standard input. Read them and...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print Make your unorderedList a list of characters instead of integers Insert ten characters into the list and print it out both ways #include <iostream> #include <string> #include <cstdlib> using namespace std; struct node { int info; node* next; }; class unorderedList { private: int length; node* listPtr; public: unorderedList() {length = 0; listPtr = NULL;} void makeEmpty(); void insertItem(int item); void printList(); bool isFull()...
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements. Requirements: Must be at least 6 characters long (but not limited to 6, can be greater) Contains one upper case letter Contains one lower case letter Contains one digit Complete the code attached. Must use pointer while checking the characters. Download Source Lab 3 File: #include using...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
(C++ Programming) Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS) ·In “stackfib.cpp”, write a non-recursive...
(C++ Programming) Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS) ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the stack is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT