about cartesian product

hello!!I write to you in order to ask you some information about the cartesian product of n sets. The all n sets are equivalent; everyone contain matriz nxm.Please help me! Can I use array of array or Set?How can I do it! Some reference?Thanks.. grazie!
[288 byte] By [chongoa] at [2007-11-27 5:50:22]
# 1
The Cartesian product of n sets is a set of n-tuples.Refererence: http://en.wikipedia.org/wiki/Cartesian_Product
Hippolytea at 2007-7-12 15:37:46 > top of Java-index,Java Essentials,Java Programming...
# 2

Can you state your problem more clearly and completely? When you say n sets, do you mean n-sets (sets of n elements) or repetition of the operation of taking the product n-1 times? And because you are being ambiguous, what do you mean the sets are equivalent? I doubt that they all contain exactly the same elements.

Anyways, it sounds like you don't even know the definition of the Cartesian product.

Given sets A and B, the Cartesian product AxB is the set { (a, b) : a in A, b in B }.

In an array representation AxB would be represented by a 2-D array and AxBxC by a 3-D array... if the elements are matrices also represented by arrays, then you will be using 4-D and 5-D arrays respectively.

ProGrapea at 2007-7-12 15:37:46 > top of Java-index,Java Essentials,Java Programming...
# 3

It's not clear exactly what you're asking: what "information" about the product do you want?

Since a Cartesian product is ordered you could represent a typical element as an ArrayList of the base type. The complete product would then be a Set of ArrayLists of the base type - it would be rather large, and depending on what you are trying to do you might not need to ever form the complete set.

pbrockway2a at 2007-7-12 15:37:46 > top of Java-index,Java Essentials,Java Programming...
# 4

hello ProGrape. I ask excuse to you, because my english isn't correct and sufficient. I try write my problem more clearly.

However, each set contain m elements, that is array length m. There are identical n sets (they all contain exactly the same elements); and the cartesian product will be repetition of the operation of taking the product n-1 times.

Example: n=3, m=2.

Sets: {10,01} | {10,01} | {10,01}

Cartesian product = new Set (or matrix, array, I don't know)

{10,10,10} | {10,10,01}.... {01,01,01} there are 8 (m^n) new elements or sets.

For this problem, It is better use array of array or Sets in java?

thanks...

chongoa at 2007-7-12 15:37:46 > top of Java-index,Java Essentials,Java Programming...
# 5

chongo,

To begin with, it looks like your sets have binary strings as elemens, so (unless your binary strings are ever longer than 16 bits) you can use one integer to represent each element. For instance, the element 01101 is equivalent to 0000000000001101, which is 13 in base two, so it will be a 13 in the set. (All I'm saying is, you don't need to use a string representation.)

The sets themselves are probably most easily stored as arrays of ints. This gives us a notion of order that might be useful for calculating the cartesian product, and is quick and easy to implement.

For the product, I would not even bother calculating the whole thing at once unless you need acces to all of its elements more than a few times. If you represent your existing sets by arrays, you have constant-time random access to the elements in a definite order, so you could just write a method that returns specific elements of the Cartesian product. This will effectively give you access to elements in the Cartesian product in O(n) time. For an example of such a method (check this code before using it):

public static int[] getCartesianElement(int[][] sets, int[] coordinates) {

if (sets.length != coordinates.length)

throw new IllegalArgumentException("lengths do not match");

int[] element = new int[sets.length];

for (int i = 0; i < sets.length; i++)

element[i] = sets[i][coordinates];

return element;

}

or, if you know you're using only one set in the product,

public static int[] getCartesianElement(int[] set, int[] coordinates) {

int[] element = new int[coordinates.length];

for (int i = 0; i < coordintates.length; i++)

element[i] = set[coordinates];

return element;

}

I used the name "coordinates" for the int[] specifying which element we want because you can think of the elements of the product as occupying points in n-space, with the coordinates resulting form the handy ordering imposed by the array representation. In the second method, the number of operands is implicit in the length of coordinates.

If you really want to generate the whole product, the code above could be easily adapted.

ProGrapea at 2007-7-12 15:37:46 > top of Java-index,Java Essentials,Java Programming...
# 6
Maybe if you gave a little clue as to what you are trying to accomplish and what the context is, forum members could respond with concrete advice.
Hippolytea at 2007-7-12 15:37:46 > top of Java-index,Java Essentials,Java Programming...