You are not logged in.
Pages: 1
Oh ... forgot ... for printing the result ( THE "row" Vector) :
private static void print(Vector<Integer[]> holder)
{
for (int i = 0; i < holder.size(); i++)
{
Integer[] v = holder.get(i);
for (int j = 0; j < v.length; j++)
{
System.out.print("\t"+v[j]);
}
System.out.print("\n");
}
}
ok for anyone interested here is a java code that calculates such solutions ...
Vector<Integer> vr1 = new Vector<Integer>();
vr1.add(new Integer(1));
vr1.add(new Integer(2));
vr1.add(new Integer(3));
vr1.add(new Integer(4));
vr1.add(new Integer(5));
Vector<Integer> vr2 = new Vector<Integer>();
vr2.add(new Integer(1));
vr2.add(new Integer(2));
vr2.add(new Integer(3));
Vector<Integer> vr3 = new Vector<Integer>();
vr3.add(new Integer(1));
vr3.add(new Integer(5));
Vector<Vector<Integer>> vr = new Vector<Vector<Integer>>();
vr.add(vr1);
vr.add(vr2);
vr.add(vr3);
Vector<Integer[]> row = new Vector<Integer[]>();
for (int i = 0; i < vr.size(); i++)
{
if(i+1==vr.size())
break;
Vector<Integer> row1 = vr.get(i);
if(i==0)
{
// copy to row vector
for (int j = 0; j < row1.size(); j++)
{
Integer[] integer = new Integer[]{row1.get(j)};
row.add(integer);
}
}
Vector<Integer[]> newRow = new Vector<Integer[]>();
Vector<Integer> row2 = vr.get(i+1);
for (int j = 0; j < row.size(); j++)
{
Integer[] previousResult = row.get(j);
int resultSize = previousResult.length;
for (int j2 = 0; j2 < row2.size(); j2++)
{
Integer currentValue = row2.get(j2);
Integer[] tmpPreviousResults = new Integer[previousResult.length];
System.arraycopy(previousResult, 0, tmpPreviousResults, 0, previousResult.length);
Arrays.sort(tmpPreviousResults);
if(Arrays.binarySearch(tmpPreviousResults, currentValue)>=0)
{
// Already exist so skip
continue;
}
Integer[] newResult = new Integer[resultSize+1];
for (int k = 0; k < previousResult.length; k++)
{
newResult[k] = previousResult[k];
}
newResult[resultSize] = currentValue;
newRow.add(newResult);
}
}
row = newRow;
}
thank you very much for your effort.
Thank you for your responce .
I will try to see if the same logic can be applied to 4 sets since in my problem the number of sets can varry!
I have a prodlem that seems like it needs set theory consepts for solving it.
I would like to have a roadmap from where to start looking since set theory is a large field.
My problem is :
I have 3 sets
A:{1,2,3,4,5}
B:{1,2,3}
C:{1,5}
and i want to count all possible sets D {x,y,z} that x belongs to A, y belongs to B, z belongs to C (similar to the cardinality of AxBxC) BUT there souldn't be any number more than one time (i.e. (1,2,1) should not be counted)
I know the answer for this problem is 15 but i need a formula for solving such problems without having to enumerate each solution (since the numbers of each set can be really high)
Thank you in advance.
Pages: 1