-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosureSetAttributesGenerator.js
More file actions
135 lines (121 loc) · 3.22 KB
/
ClosureSetAttributesGenerator.js
File metadata and controls
135 lines (121 loc) · 3.22 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
/**
*
* @Name: Closure of a set of Attributes Generator JS
* @Author: Max Base
* @Date: 2022/12/05
* @Repository: https://github.com/BaseMax/ClosureSetAttributesGeneratorJS
*
*/
//// Functions
const combine = (a, min) => {
const fn = (n, src, got, all) => {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
}
return;
}
for (let j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
return;
}
const all = [];
for (let i = min; i < a.length; i++) {
fn(i, a, [], all);
}
all.push(a);
return all;
};
const getCombinations = (arr) => {
const res = [];
for (let i = 1; i <= arr.length; i++) {
const combinations = combine(arr, i);
res.push(...combinations);
}
return res;
};
const finds = (F, Z) => {
// const nodes = new Set();
// for (const set of F) {
// for (const from of set.from) {
// nodes.add(from);
// }
// for (const to of set.to) {
// nodes.add(to);
// }
// }
// console.log(nodes);
const Zp = new Set();
for (const z of Z) {
Zp.add(z);
}
let something_changed = false;
do {
something_changed = false;
for (const set of F) {
const from_combinations = getCombinations(set.from);
for (const from_combination of from_combinations) {
for (const Zp_item of Zp) {
// check if from_combination is a sub set of Zp_item
if (from_combination.every(v => Zp_item.includes(v))) {
for (const to of set.to) {
if (Zp.has(to)) continue;
Zp.add(to);
something_changed = true;
}
}
}
}
}
} while (something_changed === true);
return Zp;
};
//// Test Caswes
let out;
out = finds([
{from: ["A"], to: ["B"]},
{from: ["B"], to: ["D"]},
{from: ["A"], to: ["K"]},
{from: ["K"], to: ["C"]},
], ["A"]);
out = finds([
{from: ["T"], to: ["U"]},
{from: ["V"], to: ["S", "W"]},
{from: ["S"], to: ["T"]},
], ["S", "V"]);
// S, V, W, T, U
out = finds([
{from: ["T"], to: ["U"]},
{from: ["V"], to: ["S", "W"]},
{from: ["S"], to: ["T"]},
], ["V"]);
// V, S, W, T, U
out = finds([
{from: ["T"], to: ["U"]},
{from: ["V"], to: ["S", "W"]},
{from: ["S"], to: ["T"]},
], ["S"]);
// S, T, U
out = finds([
{from: ["U"], to: ["V", "X", "Q"]},
{from: ["U", "V", "P"], to: ["O"]},
{from: ["O", "Q"], to: ["Y", "Z"]},
{from: ["U", "P"], to: ["X", "Y"]},
], ["U", "P"]);
// U, P, V, X, Q, O, Y, Z
out = finds([
{from: ["U"], to: ["V", "X", "Q"]},
{from: ["U", "V", "P"], to: ["O"]},
{from: ["O", "Q"], to: ["Y", "Z"]},
{from: ["U", "P"], to: ["X", "Y"]},
], ["O", "Q"]);
// O, Q, Y, Z
out = finds([
{from: ["U"], to: ["V", "X", "Q"]},
{from: ["U", "V", "P"], to: ["O"]},
{from: ["O", "Q"], to: ["Y", "Z"]},
{from: ["U", "P"], to: ["X", "Y"]},
], ["U", "P", "W"]);
// U, P, W, V, X, Q, O, Y, Z
console.log(out);