



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of dynamic programming and its application to the 0/1 knapsack problem. The sequence of decisions, problem state, principle of optimality, and dynamic programming recurrence equations. It also provides a recursive code implementation and discusses time complexity and reducing run time.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Let xi = 1 when item i is selected and let xi = 0 when item i is not selected.
i = 1
n maximize pi^ xi
i = 1
n subject to wi^ xi^ <= c
and xi = 0 or 1 for all i All profits and weights are positive.
the weights and profits of the available items the capacity of the knapsack
i = 2
n maximize pi^ xi
i = 2
n subject to wi^ xi^ <= c- w^1
and xi = 0 or 1 for all i
i = 2
n pi bi > i = 2
n pi ai
/** @return f(i,y) */ private static int f(int i, int y) { if (i == n) return (y < w[n])? 0 : p[n]; if (y < w[i]) return f(i + 1, y); return Math.max(f(i + 1, y), f(i + 1, y - w[i]) + p[i]); }
private static int f(int i, int y)
{
if (fArray[i][y] >= 0) return fArray[i][y]; if (i == n) {fArray[i][y] = (y < w[n])? 0 : p[n]; return fArray[i][y];} if (y < w[i]) fArray[i][y] = f(i + 1, y); else fArray[i][y] = Math.max(f(i + 1, y), f(i + 1, y - w[i]) + p[i]); return fArray[i][y];
}