c# - Finding a set of numbers equals to the number -


i trying write c# program following:

  1. takes 2 parameters: list of objects , number
  2. iterates through list , find first set of objects equals number.
  3. stop iteration if set found , return set.

so, have list of user defined object, person example. say, person object has 2 fields, name , age. example,

mylist -   person1: john, 10 -   person2: mary, 25 -   person3: mike, 35 -   person4: ann, 20 -   person5: joe, 5 

i want find set list equals number passing in. if passing in above list , 50, want return person2, person4, person5 list.

this subset sum problem.

unfortunately, np-complete, there no known polynomial solution it.

however, have pseudo-polynomial solution, using dyanamic programming, using next recursive function:

f(i,0) = true f(0,k) = false (k != 0) f(i,k) = f(i-1,k) or f(i-1,k-weight[i]) 

running f(n,w) yields true if such solution exists, , false otherwise.

the dynamic programming solution fills table, after table full, need "retrace" steps table[n][w] in order find items should included in set.
more information on getting actual elements table can found in this thread


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -