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
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
Related questions
Question
100%
1. Consider the following dynamic
#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
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
Knowledge Booster
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
Recommended textbooks for you
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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education