# Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 1.58 KB  |  hits: 13  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. PHP algorithm which calculates all possible combinations of dividing one set between another set
2. \$set1= array('A', 'B', 'C', 'D', 'E');
3. \$set2= array('One', 'Two', 'Three', 'Four', 'Five');
4.
5. \$possibilities= array(
6.   0 => array(
7.     'One' => array('A'),
8.     'Two' => array('B'),
9.     'Three' => array('C'),
10.     'Four' => array('D'),
11.     'Five' => array('E'),
12.   ),
13.   1 => array(
14.     'One' => array('A', 'B', 'C'),
15.     'Two' => array('D'),
16.     'Three' => array('E'),
17.     'Four' => array(),
18.     'Five' => array(),
19.   ),
20.   2 => array(
21.     'One' => array('C'),
22.     'Two' => array(),
23.     'Three' => array('A'),
24.     'Four' => array('B'),
25.     'Five' => array('D', 'E'),
26.   ),
27.   3 => array(
28.     'One' => array(),
29.     'Two' => array(),
30.     'Three' => array('A', 'B', 'C', 'D', 'E'),
31.     'Four' => array(),
32.     'Five' => array(),
33.   ),
34. );
35.
36. \$powerset= powerSet(array('A', 'B', 'C'));
37. \$cartesian = cartesian(array(
38.     'Set1' => \$powerset,
39.     'Set2' => array('One', 'Two', 'Three')
40. ));
41.
42. Array (
43.       [0] => Array ()
44.       [1] => Array (
45.         [0] => A
46.       )
47.       [2] => Array (
48.         [0] => B
49.       )
50.       [3] => Array (
51.         [0] => B
52.         [1] => A
53.       )
54.       ...
55.
56. Array (
57.       [0] => Array (
58.         [Set1] => Array ()
59.         [Set2] => One
60.       )
61.       [1] => Array (
62.         [Set1] => Array (
63.           [0] => A
64.         )
65.         [Set2] => One
66.       )
67.       [2] => Array (
68.         [Set1] => Array (
69.           [0] => B
70.         )
71.         [Set2] => One
72.       )
73.       [3] => Array (
74.         [Set1] => Array (
75.           [0] => B
76.           [1] => A
77.         )
78.         [Set2] => One
79.       )
80.       ...