1. Consider the following dynamic programming implementation of the Knapsack problem:   #include int find_max(int a, int b) {       if(a > b)          return a;       return b; } int knapsack(int W, int *wt, int *val,int n) {      int ans[n + 1][W + 1];      int itm,w;      for(itm = 0; itm <= n; itm++)          ans[itm][0] = 0;      for(w = 0;w <= W; w++)         ans[0][w] = 0;      for(itm = 1; itm <= n; itm++)      {           for(w = 1; w <= W; w++)           {                if(wt[itm - 1] <= w)                   ans[itm][w] = ______________;                else                   ans[itm][w] = ans[itm - 1][w];           }      }      return ans[n][W]; } int main() {      int w[] = {10,20,30}, v[] = {60, 100, 120}, W = 50;      int ans = knapsack(W, w, v, 3);      printf("%d",ans);      return 0; }   Which of the following lines completes the above code? A. find_max(ans[itm – 1][w – wt[itm – 1]] + val[itm – 1], ans[itm – 1][w]) B. find_max(ans[itm – 1][w – wt[itm – 1]], ans[itm – 1][w]) C. ans[itm][w] = ans[itm – 1][w]; D. none of the mentioned

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
100%

1. Consider the following dynamic programming implementation of the Knapsack problem:

 
#include
int find_max(int a, int b)
{
      if(a > b)
         return a;
      return b;
}
int knapsack(int W, int *wt, int *val,int n)
{
     int ans[n + 1][W + 1];
     int itm,w;
     for(itm = 0; itm <= n; itm++)
         ans[itm][0] = 0;
     for(w = 0;w <= W; w++)
        ans[0][w] = 0;
     for(itm = 1; itm <= n; itm++)
     {
          for(w = 1; w <= W; w++)
          {
               if(wt[itm - 1] <= w)
                  ans[itm][w] = ______________;
               else
                  ans[itm][w] = ans[itm - 1][w];
          }
     }
     return ans[n][W];
}
int main()
{
     int w[] = {10,20,30}, v[] = {60, 100, 120}, W = 50;
     int ans = knapsack(W, w, v, 3);
     printf("%d",ans);
     return 0;
}
 
Which of the following lines completes the above code?

A. find_max(ans[itm – 1][w – wt[itm – 1]] + val[itm – 1], ans[itm – 1][w])

B. find_max(ans[itm – 1][w – wt[itm – 1]], ans[itm – 1][w])

C. ans[itm][w] = ans[itm – 1][w];

D. none of the mentioned

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Uncertainty Problems
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