Cartesian Product and java Algorithm Implementation

Source: Internet
Author: User

Cartesian Product and java Algorithm Implementation
In mathematics, Two SetsXAndYCartesian product, also known as the straight productX×YThe first object isXAnd the second object isYAll possible sequential pairs of a member. 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 )}.
Java implementation:

 
  1. Import java. util. ArrayList;
  2. Import java. util. List;

  3. Public class Descartes
  4. {

  5. Public static void run (List <String> dimvalue, List <String> result, int layer, String curstring)
  6. {
  7. // When the value is greater than one set:
  8. If (layer <dimvalue. size ()-1)
  9. {
  10. // When the value is greater than one set, the first set is empty.
  11. If (dimvalue. get (layer). size () = 0)
  12. Run (dimvalue, result, layer + 1, curstring );
  13. Else
  14. {
  15. For (int I = 0; I <dimvalue. get (layer). size (); I ++)
  16. {
  17. StringBuilder s1 = new StringBuilder ();
  18. S1.append (curstring );
  19. S1.append (dimvalue. get (layer). get (I ));
  20. Run (dimvalue, result, layer + 1, s1.toString ());
  21. }
  22. }
  23. }
  24. // When there is only one set:
  25. Else if (layer = dimvalue. size ()-1)
  26. {
  27. // There is only one set with no elements in the Set
  28. If (dimvalue. get (layer). size () = 0)
  29. Result. add (curstring );
  30. // When there is only one set and the set contains elements: Its Cartesian product is the set element itself.
  31. Else
  32. {
  33. For (int I = 0; I <dimvalue. get (layer). size (); I ++)
  34. {
  35. Result. add (curstring + dimvalue. get (layer). get (I ));
  36. }
  37. }
  38. }
  39. }


  40. /**
  41. * @ Param args
  42. */
  43. Public static void main (String [] args)
  44. {
  45. List <String> dimvalue = new ArrayList <List <String> ();
  46. List <String> v1 = new ArrayList <String> ();
  47. V1.add ("");
  48. V1.add ("B ");
  49. List <String> v2 = new ArrayList <String> ();
  50. V2.add ("c ");
  51. V2.add ("d ");
  52. V2.add ("e ");
  53. List <String> v3 = new ArrayList <String> ();
  54. V3.add ("f ");
  55. V3.add ("g ");

  56. Dimvalue. add (v1 );
  57. Dimvalue. add (v2 );
  58. Dimvalue. add (v3 );

  59. List <String> result = new ArrayList <String> ();

  60. Descartes. run (dimvalue, result, 0 ,"");

  61. Int I = 1;
  62. For (String s: result)
  63. {
  64. System. out. println (I ++ ":" + s );
  65. }
  66. }

  67. }
Output result:


 
  1. 1:acf
  2. 2:acg
  3. 3:adf
  4. 4:adg
  5. 5:aef
  6. 6:aeg
  7. 7:bcf
  8. 8:bcg
  9. 9:bdf
  10. 10:bdg
  11. 11:bef
  12. 12:beg
Program instructions (1) treat the elements of each dimension set as List <string>, multiple sets constitute List <string> dimvalue as input (2) put the result of the multi-dimensional Cartesian product into the List <string> result as the output (3) int layer. string curstring is only a parameter of two intermediate processes carrying the variable (4). The program uses recursive calls, an example of the initial call is as follows:

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.