-
Notifications
You must be signed in to change notification settings - Fork 0
/
getNonOverlappingSubsets.js
169 lines (155 loc) · 5.17 KB
/
getNonOverlappingSubsets.js
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
function compareSets(setA, setB) {
if (setA.length === 0) {
return setB.length === 0 ? 0 : 1;
} else if (setB.length === 0) {
return -1;
} else {
const [itemA, ...restA] = setA;
const [itemB, ...restB] = setB;
const sign = Math.sign(itemA - itemB);
return (sign === 0) ?
compareSets(restA, restB) :
sign;
}
}
function getSubsets(items, minItems = 0, maxItems = Infinity) {
return items.reduce((subsets, item) => [
...subsets,
...subsets.filter(subset => subset.length < maxItems).map(subset => [
...subset,
item
])
], [[]]).filter(subset => subset.length >= minItems);
}
function lookup(sets, newSet, start, end) {
if (start === undefined) {
start = 0;
}
if (end === undefined) {
end = sets.length;
}
if (start === end) {
return start;
} else {
const middle = Math.floor((end - 1 - start) / 2) + start;
return (compareSets(newSet, sets[middle]) < 0) ?
lookup(sets, newSet, start, middle) :
lookup(sets, newSet, middle + 1, end);
}
}
function insertInOrder(sets, newSet) {
if (newSet.length === 0) {
return sets;
}
const index = lookup(sets, newSet);
return [
...sets.slice(0, index),
newSet,
...sets.slice(index)
]
}
function order(unorderedSets) {
return unorderedSets.reduce(insertInOrder, []);
}
function minus(setA, setB) {
return setA.filter(item => !setB.includes(item))
}
function reduce(family) {
return [
...family.unassignedClusters.flatMap((cluster, index) =>
getSubsets(cluster, 1).map(clusterSubset => ({
unassignedClusters: insertInOrder([
...family.unassignedClusters.slice(0, index),
...family.unassignedClusters.slice(index + 1)
], minus(cluster, clusterSubset)),
subsets: insertInOrder(family.subsets, clusterSubset)
}))),
...family.subsets.flatMap((set, setIndex) => {
const [setHead, ...setTail] = set;
return getSubsets(setTail, 1).map(subset => ({
unassignedClusters: family.unassignedClusters,
subsets: [
...family.subsets.slice(0, setIndex),
minus(set,subset),
...insertInOrder(family.subsets.slice(setIndex + 1), subset)
]
}));
})
];
}
function reduceTree(family, tree = {}, options = {}) {
const familyStr = JSON.stringify(family);
if (!(familyStr in tree)) {
const reductions = ('maxSubsets' in options) && family.subsets.length === options.maxSubsets ? [] : reduce(family);
tree[familyStr] = {
...family,
reductions: reductions.map(JSON.stringify)
}
reductions.forEach(reduction => reduceTree(reduction, tree, options));
}
return tree;
}
// const cache = {};
const cache = new Map();
export default function getNonOverlappingFamilies(clusters, options = {}) {
const argStr = options.memoized ? JSON.stringify({clusters, options}) : undefined;
// if (options.memoized) {
// const existingResult = cache.get(argStr);
// if (existingResult !== undefined) return existingResult;
// // if (argStr in cache) {
// // return cache[argStr];
// // }
// }
if (options.memoized && cache.has(argStr)) {
return cache.get(argStr);
// if (argStr in cache) {
// return cache[argStr];
// }
}
const emptyFamily = {
unassignedClusters: clusters,
subsets: []
};
var reduceTreeEntries = Object.entries(reduceTree(emptyFamily, {}, options));
if (options.excludeEmptyFamily) {
reduceTreeEntries = reduceTreeEntries.filter(([id, ...family]) => id !== emptyFamily);
}
const numItems = clusters.flat().length;
const getNonOverlappingFamilies = Object.fromEntries(
reduceTreeEntries.map(([id, {reductions, subsets}]) => [id, {
reductions,
subsets,
vector: [...Array(numItems).keys()].map(item => subsets.findIndex(subset => subset.includes(item)))
}]));
if (options.memoized) {
// cache[argStr] = getNonOverlappingFamilies;
cache.set(argStr, getNonOverlappingFamilies);
}
return getNonOverlappingFamilies;
}
// function test() {
// // console.log(subsets([10,20,30], 2, 2));
// // const result = reduce( {
// // unassignedClusters: [
// // [1, 5, 6],
// // [2],
// // [3, 7],
// // [4],
// // [8]
// // ],
// // subsets: []
// // });
// // console.log(JSON.stringify(result, null, 4));
// // const result2 = result.map(reduce);
// // console.log('result2');
// // console.log(JSON.stringify(result2, null, 4));
// const reductionTree = getNonOverlappingFamilies([
// [0],
// [1, 3],
// [2]
// ], {
// // maxSubsets: 2
// });
// console.log(reductionTree);
// }
// test();