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

Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character. Example: Input of “Hello,...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
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...
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to...
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to be maintained. Select one: a. recursion b. iteration c. recurrence d. stack e. array
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...
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
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT