answersLogoWhite

0


Best Answer
Answer

The pseudocode listed below is for the unbounded knapsack problem.

operation ub-ks (n, K)
// n is the total number of items, K is the capacity of the knapsack
{
for (int h = 0; h < K; h++)
V[0, h] = 0; // initializes the bottom row of the table
for (int i = 0; i < n; i++) {
for (int kp = 0; kp < K; kp++) {
ans = V[i-1, kp]; // case 1: item i not included
if (size[i] <= kp) { // if the ith item's size is less than kp...
other = val[i] + V[i-1, kp - size[i]];
// ...then case 2: item i is included
if (other > ans) // case 3: both are possible, so take the max
ans = other;
V[i, kp] = ans;
}
}
}
return V[n, K];
} // end ub-ks

User Avatar

Wiki User

βˆ™ 15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 12y ago

Merkle-Hellman's Knapsack algorithm is based on the NP-class "knapsack" problem, in which a series of items with different weights are put into a knapsack capable of holding a certain weight S. As an example, take the objects of weight 1, 4, 6, 11, 17, and 29 where the S can be equal to 11 (1+4+6, or just 11) and not 13. The time necessary to solve this problem increases exponentially as the number of items increase, as the only conventional method being exhaustive search, and is easily solvable with 5 objects but not 1000.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write an algorithm for Knapsack Problem?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is knapsack algorithm is a public key encryption algorithm?

yes


What is the time complexity of algorithm to solve fractional knapsack problem using greedy paradigm?

if the objects in the knapsack are already being sorted then it requires only O(n) times to arrange the objects...so total time require by the knapsack problem is T(n)=(nlogn) because sorting the objects require O(nlogn) time...Remaining is to run for n objects O(n). Hence, bounded by O(nlogn)


What is algorithams?

Algarithm: Algorithm is process to solve the problem in a step by step order Algorithm is used to write the program in a computer language. thrinath.sachin@gmail.com


How do you write algorithm for java program?

An "algorithm" is a method to solve a problem. These methods are more or less independent of the language. First you think about how you will solve a certain problem, step by step. Then you translate this into a computer program.


Give an Example for knapsack problem?

pls soon answer my query....


What are the main steps involved in writing algorithm?

1 Define the problem 2 Analyze the problem 3 Develop an algorithm/method of solution 4 Write a computer program corresponding to the algorithm 5 Test and debug the program 6 Document the program (how it works and how to use it)


What steps are necessary to build an algorithms?

the number of steps of an algorithm will be countable and finite.


What is algorithm to write algorithm to the program to access a pointer variable in structure?

Here is the algorithm of the algorithm to write an algorithm to access a pointer in a variable. Algorithmically.name_of_the_structure dot name_of_the _field,eg:mystruct.pointerfield


Where do you run an algorithm?

By preparing test cases we can test an algorithm. The algorithm is tested with each test case.


Write an algorithm to find the root of quadratic equation?

Write an algorithm to find the root of quadratic equation


How do you write algorith of C programs?

There is no specific Hard and Fast rule for writing algorithm. The normal method is the following: 1. get a problem 2. find or invent an algorithm to solve it 3. implement the algorithm in a programming language (C, for example)


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string