The Cartesian product of the unknown dimension set by recursive and cyclic two ways

Source: Internet
Author: User
Tags arrays

what is Cartesian product.

In mathematics, the Cartesian product of two sets X and Y (Cartesian product), also called the direct product, is represented as XXY, the first object is the member of X and the second object is one of all possible ordered pairs of Y.

Assuming collection a={a,b}, set b={0,1,2}, the Cartesian product of two sets is {(a,0), (a,1), (a,2), (b,0), (b,1), (b,2)}.


how to achieve Cartesian product with program algorithm.

If the number of collections is known before programming, the Cartesian product can be obtained through multiple loops of the program. But if you do not know the number of sets before programming, how to get the Cartesian product. For example, the collection represents the list < list<string>> list, and the list is unknown in the list before programming. The following code implements the Cartesian product of an unknown dimension collection using both recursive and cyclic methods:

Import java.util.ArrayList;
Import Java.util.Arrays;

Import java.util.List; /** * Loop and recursive two ways to implement the Cartesian product of an unknown dimension collection * Created on 2015-05-22 * @author Luweijie */public class Descartes {/** * recursive Implements the Cartesian product in Dimvalue, results in result * @param dimvalue raw Data * @param result data * @param layer dimvalue layers * @param curlist Results per Cartesian product */private static void recursive (List<list<string>> dimvalue, list<list& Lt
            string>> result, int layer, list<string> curlist) {if (Layer < Dimvalue.size ()-1) {
            if (dimvalue.get (layer). Size () = = 0) {Recursive (Dimvalue, result, layer + 1, curlist); } else {for (int i = 0; i < dimvalue.get (layer). Size (); i++) {list<string>
                    List = new arraylist<string> (curlist);
                    List.add (dimvalue.get (layer). Get (i));
                Recursive (dimvalue, result, layer + 1, list);
       }     }} else if (layer = = Dimvalue.size ()-1) {if (Dimvalue.get (layer). Size () = = 0) {
            Result.add (curlist); } else {for (int i = 0; i < dimvalue.get (layer). Size (); i++) {list<string>
                    List = new arraylist<string> (curlist);
                    List.add (dimvalue.get (layer). Get (i));
                Result.add (list); }}}}/** * Loop implements Cartesian product in Dimvalue, results are placed in result * @param dimvalue raw data * @param r Esult result data */private static void circulate (list<list<string>> dimvalue, list<list<string>& Gt
        Result) {int total = 1;
        for (list<string> list:dimvalue) {total *= list.size ();

        } string[] Myresult = new String[total];
        int itemloopnum = 1;
        int loopperitem = 1;
        int now = 1;
         for (list<string> List:dimvalue) {   Now *= list.size ();
            int index = 0;

            int currentsize = List.size ();
            Itemloopnum = Total/now;
            Loopperitem = Total/(Itemloopnum * currentsize);

            int myindex = 0;  for (String string:list) {for (int i = 0; i < Loopperitem; i++) {if (Myindex = =
                    List.size ()) {myindex = 0; } for (int j = 0; J < Itemloopnum; J + +) {Myresult[index] = (myresult[inde X] = = null?
                        "": Myresult[index] + ",") + List.get (myindex);
                    index++;
                } myindex++;
        }}} list<string> Stringresult = Arrays.aslist (Myresult);
            for (String String:stringresult) {string[] Stringarray = String.Split (",");
        Result.add (Arrays.aslist (Stringarray)); }}/** * program intoPort * @param args */public static void main (string[] args) {list<string> list1 = new Arraylis
        T<string> ();
        List1.add ("1");

        List1.add ("2");
        list<string> list2 = new arraylist<string> ();
        List2.add ("a");

        List2.add ("B");
        list<string> list3 = new arraylist<string> ();
        List3.add ("3");
        List3.add ("4");

        List3.add ("5");
        list<string> list4 = new arraylist<string> ();
        List4.add ("C");
        List4.add ("D");

        List4.add ("E");
        list<list<string>> dimvalue = new arraylist<list<string>> ();
        Dimvalue.add (List1);
        Dimvalue.add (LIST2);
        Dimvalue.add (LIST3);

        Dimvalue.add (LIST4);
        list<list<string>> Recursiveresult = new arraylist<list<string>> ();

      Recursive implementation of Cartesian product recursive (Dimvalue, Recursiveresult, 0, New arraylist<string> ());  SYSTEM.OUT.PRINTLN ("Recursive implementation of Cartesian product: Total" + recursiveresult.size () + "results"); for (list<string> List:recursiveresult) {for (String string:list) {System.out.prin
            T (string + "");
        } System.out.println ();
        } list<list<string>> Circulateresult = new arraylist<list<string>> ();
        Circulate (Dimvalue, circulateresult);
        System.out.println ("Loop to achieve Cartesian product: Total" + circulateresult.size () + "results"); for (list<string> List:circulateresult) {for (String string:list) {System.out.prin
            T (string + "");
        } System.out.println (); }
    }
}


the output is :

Recursive implementation of Cartesian product: A total of 36 results
1 A 3 C 
1 a 3 D 
1 a 3 e 
1 a 4 C 
1 a 4 D 
1 a 4 e 
1 a 5 c 
1 a 5 D 
1 A 5 e 
1 B 3 C 
1 B 3 D 
1 B 3 e 
1 B 4 C 
1 B 4 D 
1 B 4 E 
1 B 5 C 
1 B 5 D 
1 B 5 E 
2 a 3 C 
2 a 3 D 
2 a 3 e 
2 a 4 C 
2 a 4 D 
2 a 4 e 
2 a 5 C 
2 a 5 D 
2 a 5 e
  
   2 B 3 C 
2 B 3 D 
2 B 3 E 
2 b 4 C 
2 B 4 D 
2 B 4 E 
2 B 5 C 
2 B 5 D 
2 b 5 e 
loop implementation Cartesian product: A total of 36 results
1 A 3 C 
1 a 3 D 
1 a 3 e 
1 a 4 C 
1 a 4 D 
1 a 4 e 
1 a 5 c 
1 a 5 d
   1 a 5 e 
1 B 3 C 
1 B 3 D 
1 B 3 e 
1 B 4 C 
1 B 4 D 
1 B 4 E 
1 B 5 C 
1 B 5 D 
1 B 5 E 
2 a 3 C 
2 a 3 D 
2 a 3 e 
2 a 4 C 
2 a 4 D 
2 a 4 e 
2 a 5 C 
2 a 5 D 
2 a 5 E 
2 B 3 C 
2 B 3 D 
2 B 3 E 
2 b 4 C 
2 B 4 D 
2 B 4 E 
2 B 5 C 
2 B 5 D 

  



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.