Programming question

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
Post Reply
User avatar
Bartic
Frequent Member
Posts: 1284
Joined: Sat Sep 01, 2012 10:45 pm
Quick Reply: Yes
Location: SuddenDeath

Programming question

Post by Bartic »

Hey guys. I was wondering if anyone here knows something about programming (I assume at least someone does). I was trying to figure Knapsack problem out, and I did. I figured out the 0/1 Knapsack problem, as I watched an explanation on YouTube. I did write a code myself after hearing an explanation (there was no pseudo code). Here it is (it's in Pascal, don't flame me):
Spoiler!


Now, my question is: Does anybody here has a link to the explanation of Knapsack problem with infinite number of items or has a code for it? It doesn't matter what programming language it is, although I prefer pseudo code.
Image
[SuddenDeath*PlsMeRectal]
TickleMeAnal

Image


Spoiler!

User avatar
EvGa
Addicted Member
Posts: 2612
Joined: Wed Apr 23, 2008 4:33 am
Quick Reply: Yes
Location: Texas

Re: Programming question

Post by EvGa »

Let

M = Amount need to fill
w[] = Array of weights
dp[] = Array of optimal fill(dp[i] contains minimum number of items needed to fill weight i).

Code: Select all

 initialize the dp array with INFINITY, dp[0] = 0;
 for(i = 0;i<size of w;i++) {
    for(j = 1;j<=M;j++) {
       if(j-w[i] >= 0) {
          dp[j] = min(dp[j], dp[j-w[i]]+1);
       }
    }
 }

 final solution is the value of dp[M];


http://people.csail.mit.edu/bdean/6.046/dp/dp_2.swf

The above may help. I grabbed it off stackoverflow.
Image

User avatar
Bartic
Frequent Member
Posts: 1284
Joined: Sat Sep 01, 2012 10:45 pm
Quick Reply: Yes
Location: SuddenDeath

Re: Programming question

Post by Bartic »

EvGa wrote:
Let

M = Amount need to fill
w[] = Array of weights
dp[] = Array of optimal fill(dp[i] contains minimum number of items needed to fill weight i).

Code: Select all

 initialize the dp array with INFINITY, dp[0] = 0;
 for(i = 0;i<size of w;i++) {
    for(j = 1;j<=M;j++) {
       if(j-w[i] >= 0) {
          dp[j] = min(dp[j], dp[j-w[i]]+1);
       }
    }
 }

 final solution is the value of dp[M];


http://people.csail.mit.edu/bdean/6.046/dp/dp_2.swf

The above may help. I grabbed it off stackoverflow.


I thought of something similar. I will check it. Thanks.
Image
[SuddenDeath*PlsMeRectal]
TickleMeAnal

Image


Spoiler!

Post Reply

Return to “Off Topic Lounge”