Tuesday 15 January 2013

sorting - vb.net Pair Combinations to Create All Possible Sets -



sorting - vb.net Pair Combinations to Create All Possible Sets -

i need help figuring out how go programming problem. have unknown number of pairs. each pair length x width. want create sets of every possible combination of either length or width each pair. here illustration of i'm trying do:

if input 3 pairs, (1x2) (3x4) (5x6) next sets: (1,3,5) (1,3,6) (1,4,5) (1,4,6) (2,3,5) (2,3,6) (2,4,5) (2,4,6)

so if had 4 pairs, create total of 16 sets, etc. need able input each pair , after pairs have been entered, need print out sets. can never include both numbers given pair in same set. how create loop or there built in math function produce possible sets given number of pair inputs? hope described problem plenty if not, please inquire questions. thanks

this called cartesian product.

for example, if have 2 sets , b, such that

a = {1,2} b = {3,4}

then result of cartesian product x b equal to

a x b = {(1,3),(1,4),(2,3),(2,4)}

if want create cartesian product between result obtained above , new set, example:

n = {5,6}

the result of cartesian product x b x n, equal to

a x b = {(1,3),(1,4),(2,3),(2,4)} n = {5,6} ────────────────────────────────────────────────── x b x n = {(1,3,5),(1,3,6),(1,4,5),(1,4,6),(2,3,5),(2,3,6),(2,4,5),(2,4,6)}

each element of first set must paired each element of sec set.

i have developed 4 solutions cartesian product:

using mathematical model, without recursion. solution vectors using calculating each combination number. using recursion, collections class. using list (of ...) class, recursion.

these 3 solutions seemed me hard explain you. furthermore, hard me explain thoughts in english, because native language castilian.

so made effort create solution not utilize recursion, more simple , friendly programmer.

finally, create satisfactory solution. easy understand , without recursion. versatile. number of sets accepted, required, 2 onwards. can utilize number of items. depends on requirements of each developer.

i hope 4th. solution devised, please you, esteemed colleagues.

only need listbox1 within form4. here is:

public class form4 private sub form4_load(sender system.object, e system.eventargs) handles mybase.load '┌─────────── temporary code illustration ───────────┐ dim set_1 list(of string) = new list(of string) dim set_2 list(of string) = new list(of string) dim set_3 list(of string) = new list(of string) set_1.add("1") set_1.add("2") set_2.add("3") set_2.add("4") set_3.add("5") set_3.add("6") '└──────────────────────────────────────────────────┘ dim sets list(of object) = new list(of object) sets.add(set_1) sets.add(set_2) sets.add(set_3) dim product list(of string) = sets(0) = 1 sets.count - 1 product = cartesianproduct(product, sets(i)) next each element string in product me.listbox1.items.add(element) next end sub private function cartesianproduct(byval set_a list(of string), byval set_b list(of string)) list(of string) dim product list(of string) = new list(of string) each string in set_a each b string in set_b product.add(a & b) next next homecoming product end function end class

have nice day! :)

vb.net sorting

No comments:

Post a Comment