-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
213 lines (173 loc) · 6.41 KB
/
Copy pathCombinations.java
File metadata and controls
213 lines (173 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package ssmdp.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Combinations {
private List<List<Integer>> totalGroups;
private Map<Integer, List<List<Integer>>> groupsContainingIndex = new HashMap<Integer, List<List<Integer>>>();
public Combinations(int numElems) {
this(numElems, numElems);
}
public Combinations(int groupSize, int numElems) {
if (groupSize > numElems) {
throw new IllegalArgumentException(
"groupSize cannot be greater than numElems");
}
totalGroups = generateCombinations(groupSize, numElems);
for (int i = 0; i < numElems; i++) {
groupsContainingIndex.put(i,
new ArrayList<List<Integer>>());
}
for (List<Integer> group : totalGroups) {
for (Integer i : group) {
groupsContainingIndex.get(i).add(group);
}
}
}
private List<List<Integer>> generateCombinations(
int groupSize, int numElems) {
List<List<Integer>> totalGroups = new ArrayList<List<Integer>>();
List<List<Integer>> groups2 = createGroupIterationInt(2,
numElems);
totalGroups.addAll(groups2);
if (groupSize > 2) {
List<List<Integer>> groups3 = createExpandedGroup(
numElems, groups2);
totalGroups.addAll(groups3);
if (groupSize > 3) {
List<List<Integer>> groups4 = createExpandedGroup(
numElems, groups3);
totalGroups.addAll(groups4);
if (groupSize > 4) {
for (int i = 5; i <= groupSize; i++) {
totalGroups.add(createGroup(i));
}
}
}
}
return totalGroups;
}
private List<Integer> createGroup(int numElems) {
List<Integer> group = new ArrayList<Integer>();
for (int i = 0; i < numElems; i++) {
group.add(i);
}
return group;
}
private List<List<Integer>> createExpandedGroup(int numElems,
List<List<Integer>> originalGroups) {
List<List<Integer>> expandedGroups = new ArrayList<List<Integer>>();
for (List<Integer> group : originalGroups) {
expandedGroups.add(new ArrayList<Integer>(group));
}
for (Iterator<List<Integer>> iter = expandedGroups
.iterator(); iter.hasNext();) {
List<Integer> expandedGroup = iter.next();
boolean groupSet = false;
for (int i = 0; i < numElems; i++) {
if (expandedGroup.contains(i)) {
continue;
}
expandedGroup.add(i);
if (existsPreviousGroup(expandedGroups, expandedGroup)) {
expandedGroup.remove(expandedGroup.size() - 1);
} else {
groupSet = true;
break;
}
}
if (!groupSet) {
iter.remove();
}
}
return expandedGroups;
}
private boolean existsPreviousGroup(
List<List<Integer>> groups, List<Integer> newGroup) {
for (List<Integer> group : groups) {
if (group == newGroup) {
return false;
}
List<Integer> oGroup = new ArrayList<Integer>(group);
List<Integer> oNewGroup = new ArrayList<Integer>(newGroup);
Collections.sort(oGroup);
Collections.sort(oNewGroup);
if (oGroup.equals(oNewGroup)) {
return true;
}
}
throw new Error();
}
private List<List<Integer>> createGroupIterationInt(
int numElements, int size) {
return createGroupIterationInt(numElements, 0, size);
}
private List<List<Integer>> createGroupIterationInt(
int numElements, int init, int size) {
List<List<Integer>> groups = new ArrayList<List<Integer>>();
if (numElements == 0) {
groups.add(new ArrayList<Integer>());
} else {
for (int i = init; i < size; i++) {
List<List<Integer>> tempGroups = createGroupIterationInt(
numElements - 1, i + 1, size);
for (List<Integer> g : tempGroups) {
g.add(0, i);
}
groups.addAll(tempGroups);
}
}
return groups;
}
public Collection<List<Integer>> getGroupsContainingIndexes(
Collection<Integer> indexes, int size) {
Set<List<Integer>> groups = new HashSet<List<Integer>>();
for (Integer i : indexes) {
for (List<Integer> group : groupsContainingIndex.get(i)) {
int maxGroupValue = 0;
for (int gIndex : group) {
maxGroupValue = Math.max(gIndex, maxGroupValue);
}
if (maxGroupValue < size) {
groups.add(group);
}
}
}
return groups;
}
public <T> List<List<T>> getGroupsContainingIndexes(
List<Integer> indexes, List<T> elems) {
List<List<T>> returnGroups = new ArrayList<List<T>>();
Collection<List<Integer>> indexGroups = getGroupsContainingIndexes(
indexes, elems.size());
for (List<Integer> indexGroup : indexGroups) {
List<T> returnGroup = new ArrayList<T>();
for (int index : indexGroup) {
returnGroup.add(elems.get(index));
}
returnGroups.add(returnGroup);
}
return returnGroups;
}
public <T> List<List<T>> getGroups(List<T> mdpGraphs) {
List<List<T>> groups = new ArrayList<List<T>>();
global: for (List<Integer> groupInt : totalGroups) {
List<T> group = new ArrayList<T>();
for (Integer i : groupInt) {
if (i > mdpGraphs.size() - 1) {
continue global;
} else {
group.add(mdpGraphs.get(i));
}
}
groups.add(group);
}
return groups;
}
}