-
Notifications
You must be signed in to change notification settings - Fork 2
/
disjoint.spec.ts
162 lines (136 loc) · 4.53 KB
/
disjoint.spec.ts
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
import chai = require("chai");
const expect = chai.expect;
import DisjointSet from ".";
describe("DisjointSet", function () {
describe("case 1", function () {
// Example from http://algs4.cs.princeton.edu/15uf/
const set = new DisjointSet(10);
before(function () {
set.union(4, 3);
set.union(3, 8);
set.union(6, 5);
set.union(9, 4);
set.union(2, 1);
set.union(8, 9);
set.union(5, 0);
set.union(7, 2);
set.union(6, 1);
set.union(1, 0);
set.union(6, 7);
});
it("should contain 2 subsets", function () {
expect(set.numSubsets()).to.eql(2);
});
it("should group elements correctly", function () {
expect(set.isConnected(0, 1)).to.be.true;
expect(set.isConnected(0, 2)).to.be.true;
expect(set.isConnected(0, 5)).to.be.true;
expect(set.isConnected(0, 6)).to.be.true;
expect(set.isConnected(0, 7)).to.be.true;
expect(set.isConnected(3, 4)).to.be.true;
expect(set.isConnected(3, 8)).to.be.true;
expect(set.isConnected(3, 9)).to.be.true;
expect(set.isConnected(0, 3)).to.be.false;
expect(set.subsets()).to.eql([
[0, 1, 2, 5, 6, 7],
[3, 4, 8, 9],
]);
});
it("should identify the subset containing a particular element", function () {
expect(set.subset(2)).to.eql([0, 1, 2, 5, 6, 7]);
expect(set.subset(8)).to.eql([3, 4, 8, 9]);
});
});
describe("max weight example", function () {
type SubsetProps = {
maxWeight: number;
};
type EdgeProps = {
weight: number;
};
const set = new DisjointSet<SubsetProps, EdgeProps>(
6,
function (
s1: SubsetProps,
s2: SubsetProps,
edge?: EdgeProps
): SubsetProps {
return {
maxWeight: Math.max(s1.maxWeight, s2.maxWeight, edge?.weight || 0),
};
},
{ maxWeight: 0 }
);
before(function () {
set.union(0, 1, { weight: 2 });
set.union(1, 2, { weight: 4 });
set.union(3, 4, { weight: 6 });
});
it("should calculate max weight of subset", function () {
expect(set.subsetProps(0).maxWeight).to.eql(4);
expect(set.subsetProps(3).maxWeight).to.eql(6);
expect(set.subsetProps(5).maxWeight).to.eql(0);
});
});
describe("subset properties initialization", function () {
it("should initialize using an object", function () {
type SubsetProps = {
prop: number;
};
const set = new DisjointSet<SubsetProps>(5, undefined, { prop: 1 });
expect(set.subsetProps(1)).to.eql({ prop: 1 });
expect(set.subsetProps(3)).to.eql({ prop: 1 });
});
it("should initialize using a function", function () {
const set = new DisjointSet(6, undefined, function (i) {
return { prop: i };
});
expect(set.subsetProps(1)).to.eql({ prop: 1 });
expect(set.subsetProps(3)).to.eql({ prop: 3 });
});
});
describe("without a subset properties reducer", function () {
it("should group elements correctly", function () {
const set = new DisjointSet(5);
expect(set.union.bind(set, 0, 1)).to.not.throw(Error);
expect(set.union.bind(set, 0, 3)).to.not.throw(Error);
expect(set.union.bind(set, 2, 4)).to.not.throw(Error);
expect(set.isConnected(1, 3)).to.be.true;
expect(set.isConnected(2, 4)).to.be.true;
expect(set.isConnected(0, 2)).to.be.false;
expect(set.subset(0)).to.eql([0, 1, 3]);
expect(set.subset(4)).to.eql([2, 4]);
});
it("should have empty props for all subsets", function () {
const set = new DisjointSet(5);
expect(set.subsetProps(0)).to.eql({});
expect(set.union.bind(set, 1, 3, { w: 10 })).to.not.throw(Error);
expect(set.subsetProps(0)).to.eql({});
});
});
describe("without initial subset properties", function () {
it("should default to {}", function () {
const set = new DisjointSet(5, (s1, s2, e) =>
Object.assign({}, s1, s2, e)
);
expect(set.subsetProps(0)).to.eql({});
});
it("should reduce subset props", function () {
const set = new DisjointSet<
Record<string, number>,
Record<string, number>
>(
5,
(
s1: Record<string, number>,
s2: Record<string, number>,
e?: Record<string, number>
): Record<string, number> => Object.assign({}, s1, s2, e)
);
set.union(0, 1, { w: 10 });
set.union(0, 3, { u: 2 });
set.union(1, 4, { w: 5 });
expect(set.subsetProps(1)).to.eql({ u: 2, w: 5 });
});
});
});