Question 2  Using the incomplete programming code given, complete the code using dynamic programming with memory function, to reproduce the results in the following Table 1.  (C++)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Question 2 

Using the incomplete programming code given, complete the code using dynamic programming with memory function, to reproduce the results in the following Table 1
(C++)

#include<iostream>
using namespace std;

// max knapsack capacity       // *** WRITE YOUR CODE HERE ***
// num of items                // *** WRITE YOUR CODE HERE ***
// weight of each item         // *** WRITE YOUR CODE HERE ***
// value of each item         // *** WRITE YOUR CODE HERE ***
// variable for dynamic programming matrix  // *** WRITE YOUR CODE HERE ***

 

//==========================================
// Dynamic programming function: recursive
// =========================================

// ALGORITHM F(i,j)
    
    // int value
    
    // if F[i,j] is not filled yet (-1):
        
        // (start with j = W, i = n)
        // if capacity j < current item's weight w[i]:
            // value = recall F(i-1, j)
            
            
        // else:
            
            // we can include current item, 
            // but check whether including or not including gives the maximum value
            // value = max( F(i-1, j), itemValue[i] + F(i-1, j - itemWeight[i]));
        
        // F[i,j] = value;
    
    // return F[i,j]
    
    

// *** WRITE YOUR CODE HERE ***  (OR INSIDE THE PSEUDOCODE ABOVE)
 // *** WRITE YOUR CODE HERE *** (OR INSIDE THE PSEUDOCODE ABOVE)
 
 
 


int main(void){
    
    int i; // item: 1 <= i <= n  (row)
    int j; // capacity: 1 <= j <= W (column)
    
        
    // Initialization
    // *** WRITE YOUR CODE HERE ***
    
    // start from the goal F(n, W), then recursively calling dynamic programming function
    //i = ??; *** WRITE YOUR CODE HERE ***
    //j = ??; *** WRITE YOUR CODE HERE ***
    
    cout << "\n Memory Function Knapsack \n";
    cout << MFKnapsack(i,j) << endl;
    
    
    
    
    // show the result
    
    cout<<"\n----------------------------------------------"<<endl;
    cout<<"       Dynamic Programming List"<<endl;
    cout<<"----------------------------------------------"<<endl;
    
    
    for (i = 0; i <= n; i++){
        for (j = 0; j <= W; j++){    
            cout << "F(" << i << "," << j << ") = " << F[i][j] << endl;
        }
    }
    
    
    cout << endl;
    
    
    cout<<"\n----------------------------------------------"<<endl;
    cout<<"       Dynamic Programming Matrix"<<endl;
    cout<<"----------------------------------------------"<<endl;
    
    for (i = 0; i <= n; i++){
        
        for (j = 0; j <= W; j++){
            
            cout<<F[i][j]<<"\t";
            
        }
        cout<<endl;
    }
    
    
    return 0;
}

Dynamic Programming Table
capacity j
i
0
1
2
3
4 5
0
0
0 0 0
0
0
1
0
0 12 12 12 12
2
0
10 12
22 22 22
3
0 10 12 22
30
32
4 0
10
15
25 30
37
F(4,5) = 37
65
W₁ = 2, V₁ = 12
W₂ = 1, V₂ = 10
W3 = 3, V3 = 20
W4 = 2, V4 = 15
max
ⒸS. Turaev CSC 3102 DSA II
F(i-1, j),
v¡ + F(i − 1, j − w;)
Transcribed Image Text:Dynamic Programming Table capacity j i 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 12 12 12 12 2 0 10 12 22 22 22 3 0 10 12 22 30 32 4 0 10 15 25 30 37 F(4,5) = 37 65 W₁ = 2, V₁ = 12 W₂ = 1, V₂ = 10 W3 = 3, V3 = 20 W4 = 2, V4 = 15 max ⒸS. Turaev CSC 3102 DSA II F(i-1, j), v¡ + F(i − 1, j − w;)
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Fundamentals of Boolean Algebra and Digital Logics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education