Java uses recursive and cyclic methods to implement the Cartesian Product algorithm example of an unknown dimension set.

Source: Internet
Author: User

Java uses recursive and cyclic methods to implement the Cartesian Product algorithm example of an unknown dimension set.

This example describes how Java implements Cartesian product of an unknown dimension set based on recursion and loops. We will share this with you for your reference. The details are as follows:

What is Cartesian product?

In mathematics, the Cartesian product of two sets of X and Y, also known as the straight product, is expressed as x y, the first object is a member of X, and the second object is a member of all possible ordered pairs of Y.

Assume that the Set A = {a, B}, and set B = {0, 1}, the Cartesian product of the Two sets is {(a, 0), (a, 1 ), (a, 2), (B, 0), (B, 1), (B, 2 )}.

How can we use a program algorithm to implement cartesian products?

If the number of sets is known before programming, the Cartesian product can be obtained through multiple cycles of the program. But if you do not know the number of sets before programming, how can we get cartesian products? For example, set RepresentationList < List<String>> listThe number of lists before programming is unknown. The following code uses recursive and cyclic methods to implement Cartesian product of an unknown dimension set:

Import java. util. arrayList; import java. util. arrays; import java. util. list; /*** perform the Cartesian Product * Created on * @ author luweijie */public class Descartes {/*** recursively implement Descartes in dimValue product, put the result in the result * @ param dimValue raw data * @ param result data * @ param layer dimValue layers * @ param curList results of each Cartesian Product */private static void recursive (List <List <String> dimValue, list <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) ;}}}/*** implement Cartesian Product in dimValue cyclically, put the result in the result * @ param dimValue raw data * @ param result data */private static void circulate (List <String> dimValue, List <Strin G> 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 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 [index] = 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 entry * @ param args */public static void main (String [] args) {List <String> list1 = new ArrayList <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 <String> dimValue = new ArrayList <List <String> (); dimValue. add (list1); dimValue. add (list2); dimValue. add (list3); dimValue. add (list4); List <String> recursiveResult = new ArrayList <List <String> (); // recursively implements Cartesian Product recursive (dimValue, recursiveResult, 0, new ArrayList <String> (); System. out. println ("Cartesian product of Recursive Implementation: total" + recursiveResult. size () + "result"); for (List <String> list: recursiveResult) {for (String string: list) {System. out. print (string + "");} System. out. println () ;}list <List <String> circulateResult = new ArrayList <List <String> (); circulate (dimValue, circulateResult); System. out. println ("cyclic Cartesian Product: total" + circulateResult. size () + "result"); for (List <String> list: circulateResult) {for (String string: list) {System. out. print (string + "");} System. out. println ();}}}

The output result is:

Recursively implement the Cartesian product: a total of 36 results 1 a 3 c1 a 3 d1 a 3 e1 a 4 c1 a 4 d1 a 4 e1 a 5 c1 a 5 d1 a 5 e1 B 3 c1 B 3 d1 B 3 e1 B 4 c1 B 4 d1 B 4 e1 B 5 c1 B 5 d1 B 5 e2 a 3 c2 a 3 d2 a 3 e2 a 4 c2 a 4 d2 a 4 e2 a 5 c2 a 5 d2 a 5 e2 B 3 c2 B 3 d2 B 3 e2 B 4 c2 B 4 d2 B 4 e2 B 5 c2 B 5 d2 B 5 e loop implements the Cartesian product: a total of 36 results 1 a 3 c1 a 3 d1 a 3 e1 a 4 c1 a 4 d1 a 4 e1 a 5 c1 a 5 d1 a 5 e1 B 3 c1 B 3 d1 B 3 e1 B 4 c1 B 4 d1 B 4 e1 B 5 c1 B 5 d1 B 5 e2 a 3 c2 a 3 d2 a 3 e2 a 4 c2 a 4 d2 a 4 e2 a 5 c2 a 5 d2 a 5 e2 B 3 c2 B 3 d2 B 3 e2 B 4 c2 B 4 d2 B 4 e2 B 5 c2 B 5 d2 B 5 e

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.