typescript-dsa-stl 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +421 -354
- package/dist/algorithms/disjointSetUnion.d.ts +33 -0
- package/dist/algorithms/disjointSetUnion.d.ts.map +1 -0
- package/dist/algorithms/disjointSetUnion.js +93 -0
- package/dist/algorithms/disjointSetUnion.js.map +1 -0
- package/dist/algorithms/graph.d.ts +29 -0
- package/dist/algorithms/graph.d.ts.map +1 -0
- package/dist/algorithms/graph.js +57 -0
- package/dist/algorithms/graph.js.map +1 -0
- package/dist/algorithms/index.d.ts +3 -0
- package/dist/algorithms/index.d.ts.map +1 -1
- package/dist/algorithms/index.js +2 -0
- package/dist/algorithms/index.js.map +1 -1
- package/dist/collections/Graph.d.ts +10 -0
- package/dist/collections/Graph.d.ts.map +1 -1
- package/dist/collections/Graph.js +14 -0
- package/dist/collections/Graph.js.map +1 -1
- package/dist/collections/index.d.ts +1 -1
- package/dist/collections/index.d.ts.map +1 -1
- package/dist/collections/index.js +1 -1
- package/dist/collections/index.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,354 +1,421 @@
|
|
|
1
|
-
# TypeScript_DSA
|
|
2
|
-
|
|
3
|
-
**This is the GitHub repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)**.
|
|
4
|
-
|
|
5
|
-
STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **List**, **PriorityQueue**, **OrderedMap** (Map), **UnorderedMap**, **OrderedSet** (Set), **UnorderedSet**, **OrderedMultiMap**, **OrderedMultiSet**, and algorithms (`sort`, `binarySearch`, `lowerBound`, `min`, `max`, etc.). Install from npm to use in your project; this repo holds the source code.
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## Install
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm install typescript-dsa-stl
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
---
|
|
16
|
-
|
|
17
|
-
## Quick start
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
import {
|
|
21
|
-
Vector,
|
|
22
|
-
Stack,
|
|
23
|
-
Queue,
|
|
24
|
-
List,
|
|
25
|
-
PriorityQueue,
|
|
26
|
-
OrderedMap,
|
|
27
|
-
UnorderedMap,
|
|
28
|
-
OrderedSet,
|
|
29
|
-
UnorderedSet,
|
|
30
|
-
OrderedMultiMap,
|
|
31
|
-
OrderedMultiSet,
|
|
32
|
-
sort,
|
|
33
|
-
find,
|
|
34
|
-
binarySearch,
|
|
35
|
-
min,
|
|
36
|
-
max,
|
|
37
|
-
clamp,
|
|
38
|
-
range,
|
|
39
|
-
} from 'typescript-dsa-stl';
|
|
40
|
-
|
|
41
|
-
// Collections
|
|
42
|
-
const vec = new Vector<number>([1, 2, 3]);
|
|
43
|
-
vec.push(4);
|
|
44
|
-
console.log(vec.back()); // 4
|
|
45
|
-
|
|
46
|
-
const stack = new Stack<string>();
|
|
47
|
-
stack.push('a');
|
|
48
|
-
stack.push('b');
|
|
49
|
-
console.log(stack.top()); // 'b'
|
|
50
|
-
|
|
51
|
-
const queue = new Queue<number>();
|
|
52
|
-
queue.enqueue(1);
|
|
53
|
-
queue.enqueue(2);
|
|
54
|
-
console.log(queue.front()); // 1
|
|
55
|
-
|
|
56
|
-
const list = new List<number>();
|
|
57
|
-
list.pushBack(10);
|
|
58
|
-
const node = list.pushBack(20);
|
|
59
|
-
list.insertBefore(node, 15);
|
|
60
|
-
console.log(list.toArray()); // [10, 15, 20]
|
|
61
|
-
|
|
62
|
-
// PriorityQueue (max-heap by default)
|
|
63
|
-
const pq = new PriorityQueue<number>();
|
|
64
|
-
pq.push(3); pq.push(1); pq.push(4);
|
|
65
|
-
console.log(pq.top()); // 4
|
|
66
|
-
pq.pop();
|
|
67
|
-
|
|
68
|
-
// OrderedMap (keys sorted), UnorderedMap (hash)
|
|
69
|
-
const map = new UnorderedMap<string, number>();
|
|
70
|
-
map.set('a', 1); map.set('b', 2);
|
|
71
|
-
console.log(map.get('a')); // 1
|
|
72
|
-
|
|
73
|
-
// OrderedSet (sorted unique), UnorderedSet (hash)
|
|
74
|
-
const set = new UnorderedSet<number>([1, 2, 2, 3]);
|
|
75
|
-
console.log(set.size); // 3
|
|
76
|
-
|
|
77
|
-
// OrderedMultiSet (sorted, duplicates allowed)
|
|
78
|
-
const multiSet = new OrderedMultiSet<number>();
|
|
79
|
-
multiSet.add(3); multiSet.add(1); multiSet.add(2); multiSet.add(2);
|
|
80
|
-
console.log(multiSet.toArray()); // [1, 2, 2, 3]
|
|
81
|
-
console.log(multiSet.count(2)); // 2
|
|
82
|
-
|
|
83
|
-
// OrderedMultiMap (one key → multiple values, keys sorted)
|
|
84
|
-
const multiMap = new OrderedMultiMap<string, number>();
|
|
85
|
-
multiMap.set('scores', 90); multiMap.set('scores', 85); multiMap.set('scores', 95);
|
|
86
|
-
console.log(multiMap.getAll('scores')); // [90, 85, 95]
|
|
87
|
-
|
|
88
|
-
// Algorithms (work on arrays and iterables)
|
|
89
|
-
const arr = [3, 1, 4, 1, 5];
|
|
90
|
-
sort(arr); // [1, 1, 3, 4, 5]
|
|
91
|
-
find(arr, (n) => n > 3); // 4
|
|
92
|
-
min(arr); // 1
|
|
93
|
-
max(arr); // 5
|
|
94
|
-
|
|
95
|
-
const sorted = [1, 2, 4, 4, 5];
|
|
96
|
-
binarySearch(sorted, 4); // 2
|
|
97
|
-
lowerBound(sorted, 4); // 2
|
|
98
|
-
upperBound(sorted, 4); // 4
|
|
99
|
-
|
|
100
|
-
// Utils
|
|
101
|
-
clamp(42, 0, 10); // 10
|
|
102
|
-
range(0, 5); // [0, 1, 2, 3, 4]
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### 2D and 3D vectors (like C++ `vector<vector<int>>`)
|
|
106
|
-
|
|
107
|
-
`Vector<T>` is generic, so you can nest it for 2D/3D grids:
|
|
108
|
-
|
|
109
|
-
| C++ | TypeScript |
|
|
110
|
-
|-----|------------|
|
|
111
|
-
| `vector<int>` | `Vector<number>` |
|
|
112
|
-
| `vector<vector<int>>` | `Vector<Vector<number>>` |
|
|
113
|
-
| `vector<vector<vector<int>>>` | `Vector<Vector<Vector<number>>>` |
|
|
114
|
-
|
|
115
|
-
**2D example:**
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
import { Vector } from 'typescript-dsa-stl';
|
|
119
|
-
|
|
120
|
-
const grid = new Vector<Vector<number>>();
|
|
121
|
-
|
|
122
|
-
const row0 = new Vector<number>([1, 2, 3]);
|
|
123
|
-
const row1 = new Vector<number>([4, 5, 6]);
|
|
124
|
-
grid.push(row0);
|
|
125
|
-
grid.push(row1);
|
|
126
|
-
|
|
127
|
-
grid.at(0).at(1); // 2 (first row, second column)
|
|
128
|
-
grid.at(1).at(0); // 4 (second row, first column)
|
|
129
|
-
|
|
130
|
-
grid.at(0).set(1, 99);
|
|
131
|
-
grid.at(0).push(10);
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
**3D example:**
|
|
135
|
-
|
|
136
|
-
```ts
|
|
137
|
-
const cube = new Vector<Vector<Vector<number>>>();
|
|
138
|
-
|
|
139
|
-
const layer0 = new Vector<Vector<number>>();
|
|
140
|
-
layer0.push(new Vector<number>([1, 2]));
|
|
141
|
-
layer0.push(new Vector<number>([3, 4]));
|
|
142
|
-
cube.push(layer0);
|
|
143
|
-
|
|
144
|
-
cube.at(0).at(1).at(0); // 3 (layer 0, row 1, col 0)
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Graph adjacency list (like C++ `vector<vector<type>> graph(n)`)
|
|
148
|
-
|
|
149
|
-
You can model C++-style adjacency lists using the graph types and helpers exported from `typescript-dsa-stl/collections` (or the main package).
|
|
150
|
-
|
|
151
|
-
#### Unweighted adjacency list
|
|
152
|
-
|
|
153
|
-
C++:
|
|
154
|
-
|
|
155
|
-
```cpp
|
|
156
|
-
int n = 5;
|
|
157
|
-
vector<vector<int>> graph(n);
|
|
158
|
-
graph[u].push_back(v); // or graph[u].pb(v);
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
TypeScript (
|
|
162
|
-
|
|
163
|
-
```ts
|
|
164
|
-
import
|
|
165
|
-
|
|
166
|
-
const n = 5;
|
|
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
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
//
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
1
|
+
# TypeScript_DSA
|
|
2
|
+
|
|
3
|
+
**This is the GitHub repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)**.
|
|
4
|
+
|
|
5
|
+
STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **List**, **PriorityQueue**, **OrderedMap** (Map), **UnorderedMap**, **OrderedSet** (Set), **UnorderedSet**, **OrderedMultiMap**, **OrderedMultiSet**, and algorithms (`sort`, `binarySearch`, `lowerBound`, `min`, `max`, etc.). Install from npm to use in your project; this repo holds the source code.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install typescript-dsa-stl
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import {
|
|
21
|
+
Vector,
|
|
22
|
+
Stack,
|
|
23
|
+
Queue,
|
|
24
|
+
List,
|
|
25
|
+
PriorityQueue,
|
|
26
|
+
OrderedMap,
|
|
27
|
+
UnorderedMap,
|
|
28
|
+
OrderedSet,
|
|
29
|
+
UnorderedSet,
|
|
30
|
+
OrderedMultiMap,
|
|
31
|
+
OrderedMultiSet,
|
|
32
|
+
sort,
|
|
33
|
+
find,
|
|
34
|
+
binarySearch,
|
|
35
|
+
min,
|
|
36
|
+
max,
|
|
37
|
+
clamp,
|
|
38
|
+
range,
|
|
39
|
+
} from 'typescript-dsa-stl';
|
|
40
|
+
|
|
41
|
+
// Collections
|
|
42
|
+
const vec = new Vector<number>([1, 2, 3]);
|
|
43
|
+
vec.push(4);
|
|
44
|
+
console.log(vec.back()); // 4
|
|
45
|
+
|
|
46
|
+
const stack = new Stack<string>();
|
|
47
|
+
stack.push('a');
|
|
48
|
+
stack.push('b');
|
|
49
|
+
console.log(stack.top()); // 'b'
|
|
50
|
+
|
|
51
|
+
const queue = new Queue<number>();
|
|
52
|
+
queue.enqueue(1);
|
|
53
|
+
queue.enqueue(2);
|
|
54
|
+
console.log(queue.front()); // 1
|
|
55
|
+
|
|
56
|
+
const list = new List<number>();
|
|
57
|
+
list.pushBack(10);
|
|
58
|
+
const node = list.pushBack(20);
|
|
59
|
+
list.insertBefore(node, 15);
|
|
60
|
+
console.log(list.toArray()); // [10, 15, 20]
|
|
61
|
+
|
|
62
|
+
// PriorityQueue (max-heap by default)
|
|
63
|
+
const pq = new PriorityQueue<number>();
|
|
64
|
+
pq.push(3); pq.push(1); pq.push(4);
|
|
65
|
+
console.log(pq.top()); // 4
|
|
66
|
+
pq.pop();
|
|
67
|
+
|
|
68
|
+
// OrderedMap (keys sorted), UnorderedMap (hash)
|
|
69
|
+
const map = new UnorderedMap<string, number>();
|
|
70
|
+
map.set('a', 1); map.set('b', 2);
|
|
71
|
+
console.log(map.get('a')); // 1
|
|
72
|
+
|
|
73
|
+
// OrderedSet (sorted unique), UnorderedSet (hash)
|
|
74
|
+
const set = new UnorderedSet<number>([1, 2, 2, 3]);
|
|
75
|
+
console.log(set.size); // 3
|
|
76
|
+
|
|
77
|
+
// OrderedMultiSet (sorted, duplicates allowed)
|
|
78
|
+
const multiSet = new OrderedMultiSet<number>();
|
|
79
|
+
multiSet.add(3); multiSet.add(1); multiSet.add(2); multiSet.add(2);
|
|
80
|
+
console.log(multiSet.toArray()); // [1, 2, 2, 3]
|
|
81
|
+
console.log(multiSet.count(2)); // 2
|
|
82
|
+
|
|
83
|
+
// OrderedMultiMap (one key → multiple values, keys sorted)
|
|
84
|
+
const multiMap = new OrderedMultiMap<string, number>();
|
|
85
|
+
multiMap.set('scores', 90); multiMap.set('scores', 85); multiMap.set('scores', 95);
|
|
86
|
+
console.log(multiMap.getAll('scores')); // [90, 85, 95]
|
|
87
|
+
|
|
88
|
+
// Algorithms (work on arrays and iterables)
|
|
89
|
+
const arr = [3, 1, 4, 1, 5];
|
|
90
|
+
sort(arr); // [1, 1, 3, 4, 5]
|
|
91
|
+
find(arr, (n) => n > 3); // 4
|
|
92
|
+
min(arr); // 1
|
|
93
|
+
max(arr); // 5
|
|
94
|
+
|
|
95
|
+
const sorted = [1, 2, 4, 4, 5];
|
|
96
|
+
binarySearch(sorted, 4); // 2
|
|
97
|
+
lowerBound(sorted, 4); // 2
|
|
98
|
+
upperBound(sorted, 4); // 4
|
|
99
|
+
|
|
100
|
+
// Utils
|
|
101
|
+
clamp(42, 0, 10); // 10
|
|
102
|
+
range(0, 5); // [0, 1, 2, 3, 4]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 2D and 3D vectors (like C++ `vector<vector<int>>`)
|
|
106
|
+
|
|
107
|
+
`Vector<T>` is generic, so you can nest it for 2D/3D grids:
|
|
108
|
+
|
|
109
|
+
| C++ | TypeScript |
|
|
110
|
+
|-----|------------|
|
|
111
|
+
| `vector<int>` | `Vector<number>` |
|
|
112
|
+
| `vector<vector<int>>` | `Vector<Vector<number>>` |
|
|
113
|
+
| `vector<vector<vector<int>>>` | `Vector<Vector<Vector<number>>>` |
|
|
114
|
+
|
|
115
|
+
**2D example:**
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { Vector } from 'typescript-dsa-stl';
|
|
119
|
+
|
|
120
|
+
const grid = new Vector<Vector<number>>();
|
|
121
|
+
|
|
122
|
+
const row0 = new Vector<number>([1, 2, 3]);
|
|
123
|
+
const row1 = new Vector<number>([4, 5, 6]);
|
|
124
|
+
grid.push(row0);
|
|
125
|
+
grid.push(row1);
|
|
126
|
+
|
|
127
|
+
grid.at(0).at(1); // 2 (first row, second column)
|
|
128
|
+
grid.at(1).at(0); // 4 (second row, first column)
|
|
129
|
+
|
|
130
|
+
grid.at(0).set(1, 99);
|
|
131
|
+
grid.at(0).push(10);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**3D example:**
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const cube = new Vector<Vector<Vector<number>>>();
|
|
138
|
+
|
|
139
|
+
const layer0 = new Vector<Vector<number>>();
|
|
140
|
+
layer0.push(new Vector<number>([1, 2]));
|
|
141
|
+
layer0.push(new Vector<number>([3, 4]));
|
|
142
|
+
cube.push(layer0);
|
|
143
|
+
|
|
144
|
+
cube.at(0).at(1).at(0); // 3 (layer 0, row 1, col 0)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Graph adjacency list (like C++ `vector<vector<type>> graph(n)`)
|
|
148
|
+
|
|
149
|
+
You can model C++-style adjacency lists using the graph types and helpers exported from `typescript-dsa-stl/collections` (or the main package).
|
|
150
|
+
|
|
151
|
+
#### Unweighted adjacency list
|
|
152
|
+
|
|
153
|
+
C++:
|
|
154
|
+
|
|
155
|
+
```cpp
|
|
156
|
+
int n = 5;
|
|
157
|
+
vector<vector<int>> graph(n);
|
|
158
|
+
graph[u].push_back(v); // or graph[u].pb(v);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
TypeScript (easy declaration with `createAdjacencyList`):
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
import { createAdjacencyList } from 'typescript-dsa-stl/collections';
|
|
165
|
+
|
|
166
|
+
const n = 5;
|
|
167
|
+
const graph = createAdjacencyList(n); // empty graph with n vertices
|
|
168
|
+
|
|
169
|
+
// C++: graph[u].push_back(v);
|
|
170
|
+
graph[u].push(v);
|
|
171
|
+
|
|
172
|
+
// Iteration is the same idea as in C++
|
|
173
|
+
for (const v of graph[u]) {
|
|
174
|
+
// neighbor v
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Or with helpers `addEdge` / `deleteEdge`:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { createAdjacencyList, addEdge, deleteEdge } from 'typescript-dsa-stl/collections';
|
|
182
|
+
|
|
183
|
+
const graph = createAdjacencyList(5);
|
|
184
|
+
|
|
185
|
+
addEdge(graph, u, v); // add u -> v
|
|
186
|
+
deleteEdge(graph, u, v); // remove all edges u -> v
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### Weighted adjacency list
|
|
190
|
+
|
|
191
|
+
In C++ you might write:
|
|
192
|
+
|
|
193
|
+
```cpp
|
|
194
|
+
int n = 5;
|
|
195
|
+
vector<vector<pair<int,int>>> graph(n);
|
|
196
|
+
graph[u].push_back({v, w}); // edge u -> v with weight w
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
In TypeScript, use `createWeightedAdjacencyList` for easy declaration:
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
import { createWeightedAdjacencyList } from 'typescript-dsa-stl/collections';
|
|
203
|
+
|
|
204
|
+
const n = 5;
|
|
205
|
+
const graph = createWeightedAdjacencyList(n); // empty weighted graph with n vertices
|
|
206
|
+
|
|
207
|
+
// C++: graph[u].push_back({v, w});
|
|
208
|
+
graph[u].push({ to: v, weight: w });
|
|
209
|
+
|
|
210
|
+
// When iterating, you get both neighbor and weight
|
|
211
|
+
for (const { to, weight } of graph[u]) {
|
|
212
|
+
// edge u -> to with cost = weight
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Or with the helper functions `addEdge` / `deleteEdge`:
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { createWeightedAdjacencyList, addEdge, deleteEdge } from 'typescript-dsa-stl/collections';
|
|
220
|
+
|
|
221
|
+
const graph = createWeightedAdjacencyList(5);
|
|
222
|
+
|
|
223
|
+
addEdge(graph, u, v, w); // add u -> v with weight w
|
|
224
|
+
deleteEdge(graph, u, v, w); // delete all edges u -> v with weight w
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
#### Graph adjacency list — use cases
|
|
228
|
+
|
|
229
|
+
Use an **unweighted** graph (adjacency list) when you only care about connectivity; use a **weighted** graph when edges have costs (distance, time, capacity).
|
|
230
|
+
|
|
231
|
+
| Use case | When to use |
|
|
232
|
+
|----------|-------------|
|
|
233
|
+
| **BFS / DFS, connectivity** | Unweighted: shortest path in terms of hop count, connected components, cycle detection. |
|
|
234
|
+
| **Shortest path (Dijkstra), MST** | Weighted: edge weights as distances or costs; run Dijkstra, Prim, or Kruskal on the list. |
|
|
235
|
+
| **Social / dependency graphs** | Unweighted or weighted: followers, dependencies (e.g. build order), recommendation graphs. |
|
|
236
|
+
| **Grid / game graphs** | Unweighted: 4- or 8-neighbor grids; weighted if movement costs differ per cell. |
|
|
237
|
+
| **Network / flow** | Weighted: capacities or latencies on edges for max-flow or routing. |
|
|
238
|
+
|
|
239
|
+
#### Disjoint Set Union (Union-Find)
|
|
240
|
+
|
|
241
|
+
Use Union-Find (DSU) to compute connected components efficiently. It merges endpoints of every edge in the adjacency list, so for directed graphs it returns weak connectivity components.
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
import { createAdjacencyList, connectedComponents } from 'typescript-dsa-stl';
|
|
245
|
+
|
|
246
|
+
const n = 5;
|
|
247
|
+
const graph = createAdjacencyList(n);
|
|
248
|
+
graph[0].push(1);
|
|
249
|
+
graph[1].push(0);
|
|
250
|
+
graph[3].push(4);
|
|
251
|
+
graph[4].push(3);
|
|
252
|
+
|
|
253
|
+
const comps = connectedComponents(n, graph);
|
|
254
|
+
// e.g. [[0, 1], [2], [3, 4]]
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
##### Traverse the result
|
|
258
|
+
|
|
259
|
+
`connectedComponents(n, adj)` returns `number[][]` where each inner array is a component (list of vertices).
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
// 1) Iterate each component
|
|
263
|
+
for (const comp of comps) {
|
|
264
|
+
// comp is like [v0, v1, ...]
|
|
265
|
+
for (const v of comp) {
|
|
266
|
+
// do something with vertex v
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// 2) Component sizes
|
|
271
|
+
const sizes = comps.map(comp => comp.length);
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
#### Kruskal MST (uses DSU)
|
|
275
|
+
|
|
276
|
+
For a weighted graph, `kruskalMST` builds a Minimum Spanning Tree (MST) using DSU.
|
|
277
|
+
|
|
278
|
+
```ts
|
|
279
|
+
import {
|
|
280
|
+
createWeightedAdjacencyList,
|
|
281
|
+
addEdge,
|
|
282
|
+
kruskalMST,
|
|
283
|
+
} from 'typescript-dsa-stl';
|
|
284
|
+
|
|
285
|
+
const n = 4;
|
|
286
|
+
const wGraph = createWeightedAdjacencyList(n);
|
|
287
|
+
|
|
288
|
+
// Add undirected edges by adding both directions.
|
|
289
|
+
addEdge(wGraph, 0, 1, 1); addEdge(wGraph, 1, 0, 1);
|
|
290
|
+
addEdge(wGraph, 0, 2, 4); addEdge(wGraph, 2, 0, 4);
|
|
291
|
+
addEdge(wGraph, 1, 2, 2); addEdge(wGraph, 2, 1, 2);
|
|
292
|
+
addEdge(wGraph, 1, 3, 3); addEdge(wGraph, 3, 1, 3);
|
|
293
|
+
|
|
294
|
+
const { edges, totalWeight } = kruskalMST(n, wGraph, { undirected: true });
|
|
295
|
+
// edges: MST edges (chosen by weight), totalWeight: sum of weights
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
##### Traverse the MST
|
|
299
|
+
|
|
300
|
+
`kruskalMST(...)` returns `{ edges, totalWeight }`. To traverse the MST like a graph, convert `edges` into an adjacency list:
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
import { createWeightedAdjacencyList } from 'typescript-dsa-stl/collections';
|
|
304
|
+
|
|
305
|
+
const mstAdj = createWeightedAdjacencyList(n);
|
|
306
|
+
|
|
307
|
+
for (const { u, v, weight } of edges) {
|
|
308
|
+
// MST is undirected (we used { undirected: true })
|
|
309
|
+
mstAdj[u].push({ to: v, weight });
|
|
310
|
+
mstAdj[v].push({ to: u, weight });
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Example: iterate neighbors of vertex 0 in the MST
|
|
314
|
+
for (const { to, weight } of mstAdj[0]) {
|
|
315
|
+
// visit edge 0 -> to (weight)
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## API overview
|
|
322
|
+
|
|
323
|
+
| Module | Exports |
|
|
324
|
+
|--------|--------|
|
|
325
|
+
| **Collections** | `Vector`, `Stack`, `Queue`, `List`, `ListNode`, `PriorityQueue`, `OrderedMap`, `UnorderedMap`, `OrderedSet`, `UnorderedSet`, `OrderedMultiMap`, `OrderedMultiSet`, `WeightedEdge`, `AdjacencyList`, `WeightedAdjacencyList`, `createAdjacencyList`, `createWeightedAdjacencyList`, `addEdge`, `deleteEdge` |
|
|
326
|
+
| **Algorithms** | `sort`, `find`, `findIndex`, `transform`, `filter`, `reduce`, `reverse`, `unique`, `binarySearch`, `lowerBound`, `upperBound`, `min`, `max`, `partition`, `DisjointSetUnion`, `connectedComponents`, `kruskalMST` |
|
|
327
|
+
| **Utils** | `clamp`, `range`, `noop`, `identity`, `swap` |
|
|
328
|
+
| **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray` |
|
|
329
|
+
|
|
330
|
+
### Subpath imports (tree-shaking)
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
import { Vector, Stack } from 'typescript-dsa-stl/collections';
|
|
334
|
+
import { sort, binarySearch } from 'typescript-dsa-stl/algorithms';
|
|
335
|
+
import { clamp, range } from 'typescript-dsa-stl/utils';
|
|
336
|
+
import type { Comparator } from 'typescript-dsa-stl/types';
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
## Data structures
|
|
342
|
+
|
|
343
|
+
| Structure | Access | Insert end | Insert middle | Remove end | Remove middle |
|
|
344
|
+
|-----------|--------|------------|---------------|------------|---------------|
|
|
345
|
+
| **Vector** | O(1) | O(1)* | O(n) | O(1) | O(n) |
|
|
346
|
+
| **Stack** | — | O(1) | — | O(1) | — |
|
|
347
|
+
| **Queue** | — | O(1)* | — | O(1)* | — |
|
|
348
|
+
| **List** | O(n) | O(1) | O(1)** | O(1) | O(1)** |
|
|
349
|
+
| **PriorityQueue** | — | O(log n) | — | O(log n) | — |
|
|
350
|
+
| **OrderedMap** (Map) | O(log n) get | O(log n) set | — | O(log n) delete | — |
|
|
351
|
+
| **UnorderedMap** | O(1)* get/set | O(1)* | — | O(1)* delete | — |
|
|
352
|
+
| **OrderedSet** (Set) | O(log n) has | O(log n) add | — | O(log n) delete | — |
|
|
353
|
+
| **UnorderedSet** | O(1)* has/add | O(1)* | — | O(1)* delete | — |
|
|
354
|
+
| **OrderedMultiMap** | O(log n) get | O(log n) set | — | O(log n) delete | — |
|
|
355
|
+
| **OrderedMultiSet** | O(log n) has/count | O(log n) add | — | O(log n) delete | — |
|
|
356
|
+
|
|
357
|
+
\* Amortized (hash).
|
|
358
|
+
\** At a known node.
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
## OrderedMultiMap and OrderedMultiSet — use cases
|
|
363
|
+
|
|
364
|
+
**OrderedMultiSet** is a sorted collection that allows duplicate elements (like C++ `std::multiset`). Use it when you need ordering and multiple copies of the same value.
|
|
365
|
+
|
|
366
|
+
| Use case | Example |
|
|
367
|
+
|----------|---------|
|
|
368
|
+
| **Sorted runs / leaderboard with ties** | Store scores; multiple users can have the same score. Iterate in sorted order, use `count(score)` for ties. |
|
|
369
|
+
| **Event timeline with repeated timestamps** | Add events by time; several events can share the same time. `add(timestamp)`, iterate in order. |
|
|
370
|
+
| **K-th smallest in a multiset** | Keep elements sorted; k-th element is at index `k - 1` in iteration. |
|
|
371
|
+
| **Range counts** | Combined with binary search ideas: count elements in `[low, high]` using `count` and iteration. |
|
|
372
|
+
|
|
373
|
+
**OrderedMultiMap** maps one key to multiple values while keeping keys sorted (like C++ `std::multimap`). Use it when a key can have several associated values and you need key order.
|
|
374
|
+
|
|
375
|
+
| Use case | Example |
|
|
376
|
+
|----------|---------|
|
|
377
|
+
| **Inverted index** | Key = term, values = document IDs containing that term. `set(term, docId)` for each occurrence; `getAll(term)` returns all doc IDs. |
|
|
378
|
+
| **Grouping by key** | Key = category, values = items. `set(category, item)`; iterate keys in order, use `getAll(key)` per group. |
|
|
379
|
+
| **One-to-many relations** | Key = user ID, values = session IDs. `set(userId, sessionId)`; `getAll(userId)` lists all sessions. |
|
|
380
|
+
| **Time-series by bucket** | Key = time bucket, values = events. Sorted keys give chronological buckets; `getAll(bucket)` gets events in that bucket. |
|
|
381
|
+
|
|
382
|
+
### OrderedMultiSet example
|
|
383
|
+
|
|
384
|
+
```ts
|
|
385
|
+
import { OrderedMultiSet } from 'typescript-dsa-stl';
|
|
386
|
+
|
|
387
|
+
const scores = new OrderedMultiSet<number>();
|
|
388
|
+
scores.add(85); scores.add(92); scores.add(85); scores.add(78);
|
|
389
|
+
console.log(scores.toArray()); // [78, 85, 85, 92]
|
|
390
|
+
console.log(scores.count(85)); // 2
|
|
391
|
+
scores.delete(85); // remove one 85
|
|
392
|
+
console.log(scores.count(85)); // 1
|
|
393
|
+
scores.deleteAll(85); // remove all 85s
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### OrderedMultiMap example
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
import { OrderedMultiMap } from 'typescript-dsa-stl';
|
|
400
|
+
|
|
401
|
+
const index = new OrderedMultiMap<string, number>(); // term -> doc IDs
|
|
402
|
+
index.set('typescript', 1); index.set('typescript', 3); index.set('stl', 2);
|
|
403
|
+
console.log(index.getAll('typescript')); // [1, 3]
|
|
404
|
+
console.log(index.get('stl')); // 2
|
|
405
|
+
for (const [key, value] of index) {
|
|
406
|
+
console.log(key, value); // keys in sorted order
|
|
407
|
+
}
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
---
|
|
411
|
+
|
|
412
|
+
## For maintainers
|
|
413
|
+
|
|
414
|
+
- **Build:** `npm run build` (also runs before `npm publish` via `prepublishOnly`)
|
|
415
|
+
- **Publish:** `npm publish` (use `npm publish --access public` for a scoped package name)
|
|
416
|
+
|
|
417
|
+
---
|
|
418
|
+
|
|
419
|
+
## License
|
|
420
|
+
|
|
421
|
+
MIT
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disjoint Set Union (Union-Find) with:
|
|
3
|
+
* - Path compression (iterative)
|
|
4
|
+
* - Union by size
|
|
5
|
+
*
|
|
6
|
+
* Optimized for vertex indices in range [0..n-1].
|
|
7
|
+
*/
|
|
8
|
+
export declare class DisjointSetUnion {
|
|
9
|
+
private readonly parent;
|
|
10
|
+
private readonly size;
|
|
11
|
+
constructor(n: number);
|
|
12
|
+
/**
|
|
13
|
+
* Find the representative(root) of x.
|
|
14
|
+
* Amortized almost O(1) with path compression.
|
|
15
|
+
*/
|
|
16
|
+
find(x: number): number;
|
|
17
|
+
/** Returns true if a merge happened (were in different sets). */
|
|
18
|
+
union(a: number, b: number): boolean;
|
|
19
|
+
connected(a: number, b: number): boolean;
|
|
20
|
+
/** Size of the connected component containing x. */
|
|
21
|
+
componentSize(x: number): number;
|
|
22
|
+
/**
|
|
23
|
+
* Get component representative id for each vertex.
|
|
24
|
+
* Runs `find(i)` for each i (so it also compresses paths).
|
|
25
|
+
*/
|
|
26
|
+
roots(): Int32Array;
|
|
27
|
+
/**
|
|
28
|
+
* Group vertices into components.
|
|
29
|
+
* Complexity: O(n * alpha(n)).
|
|
30
|
+
*/
|
|
31
|
+
components(nVertices?: number): number[][];
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=disjointSetUnion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"disjointSetUnion.d.ts","sourceRoot":"","sources":["../../src/algorithms/disjointSetUnion.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IACpC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;gBAEtB,CAAC,EAAE,MAAM;IAUrB;;;OAGG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAiBvB,iEAAiE;IACjE,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO;IAiBpC,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO;IAIxC,oDAAoD;IACpD,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAIhC;;;OAGG;IACH,KAAK,IAAI,UAAU;IAQnB;;;OAGG;IACH,UAAU,CAAC,SAAS,SAAqB,GAAG,MAAM,EAAE,EAAE;CAqBvD"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disjoint Set Union (Union-Find) with:
|
|
3
|
+
* - Path compression (iterative)
|
|
4
|
+
* - Union by size
|
|
5
|
+
*
|
|
6
|
+
* Optimized for vertex indices in range [0..n-1].
|
|
7
|
+
*/
|
|
8
|
+
export class DisjointSetUnion {
|
|
9
|
+
constructor(n) {
|
|
10
|
+
// Assumes valid vertex count and indices are provided by the caller.
|
|
11
|
+
this.parent = new Int32Array(n);
|
|
12
|
+
this.size = new Int32Array(n);
|
|
13
|
+
for (let i = 0; i < n; i++) {
|
|
14
|
+
this.parent[i] = i;
|
|
15
|
+
this.size[i] = 1;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Find the representative(root) of x.
|
|
20
|
+
* Amortized almost O(1) with path compression.
|
|
21
|
+
*/
|
|
22
|
+
find(x) {
|
|
23
|
+
// Find root.
|
|
24
|
+
let root = x;
|
|
25
|
+
while (this.parent[root] !== root) {
|
|
26
|
+
root = this.parent[root];
|
|
27
|
+
}
|
|
28
|
+
// Compress path.
|
|
29
|
+
while (this.parent[x] !== x) {
|
|
30
|
+
const p = this.parent[x];
|
|
31
|
+
this.parent[x] = root;
|
|
32
|
+
x = p;
|
|
33
|
+
}
|
|
34
|
+
return root;
|
|
35
|
+
}
|
|
36
|
+
/** Returns true if a merge happened (were in different sets). */
|
|
37
|
+
union(a, b) {
|
|
38
|
+
let ra = this.find(a);
|
|
39
|
+
let rb = this.find(b);
|
|
40
|
+
if (ra === rb)
|
|
41
|
+
return false;
|
|
42
|
+
// Union by size: attach smaller tree under larger tree.
|
|
43
|
+
if (this.size[ra] < this.size[rb]) {
|
|
44
|
+
const tmp = ra;
|
|
45
|
+
ra = rb;
|
|
46
|
+
rb = tmp;
|
|
47
|
+
}
|
|
48
|
+
this.parent[rb] = ra;
|
|
49
|
+
this.size[ra] += this.size[rb];
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
connected(a, b) {
|
|
53
|
+
return this.find(a) === this.find(b);
|
|
54
|
+
}
|
|
55
|
+
/** Size of the connected component containing x. */
|
|
56
|
+
componentSize(x) {
|
|
57
|
+
return this.size[this.find(x)];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get component representative id for each vertex.
|
|
61
|
+
* Runs `find(i)` for each i (so it also compresses paths).
|
|
62
|
+
*/
|
|
63
|
+
roots() {
|
|
64
|
+
const out = new Int32Array(this.parent.length);
|
|
65
|
+
for (let i = 0; i < out.length; i++) {
|
|
66
|
+
out[i] = this.find(i);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Group vertices into components.
|
|
72
|
+
* Complexity: O(n * alpha(n)).
|
|
73
|
+
*/
|
|
74
|
+
components(nVertices = this.parent.length) {
|
|
75
|
+
// Use an array lookup instead of Map for faster grouping.
|
|
76
|
+
const rootToIndex = new Int32Array(this.parent.length);
|
|
77
|
+
rootToIndex.fill(-1);
|
|
78
|
+
const groups = [];
|
|
79
|
+
const n = Math.min(nVertices, this.parent.length);
|
|
80
|
+
for (let i = 0; i < n; i++) {
|
|
81
|
+
const r = this.find(i);
|
|
82
|
+
let idx = rootToIndex[r];
|
|
83
|
+
if (idx === -1) {
|
|
84
|
+
idx = groups.length;
|
|
85
|
+
rootToIndex[r] = idx;
|
|
86
|
+
groups.push([]);
|
|
87
|
+
}
|
|
88
|
+
groups[idx].push(i);
|
|
89
|
+
}
|
|
90
|
+
return groups;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=disjointSetUnion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"disjointSetUnion.js","sourceRoot":"","sources":["../../src/algorithms/disjointSetUnion.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,OAAO,gBAAgB;IAI3B,YAAY,CAAS;QACnB,qEAAqE;QACrE,IAAI,CAAC,MAAM,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;QAC9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3B,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;YACnB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,CAAS;QACZ,aAAa;QACb,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YAClC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;QAED,iBAAiB;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YACzB,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;YACtB,CAAC,GAAG,CA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C,IAAI,CAAC,CAAC,CAAC,CAAC;QACxB,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IAED;;;OAGG;IACH,UAAU,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM;QACvC,0DAA0D;QAC1D,MAAM,WAAW,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACvD,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAErB,MAAM,MAAM,GAAe,EAAE,CAAC;QAC9B,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAElD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3B,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACvB,IAAI,GAAG,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;YACzB,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC;gBACf,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC;gBACpB,WAAW,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;gBACrB,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAClB,CAAC;YACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACtB,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { AdjacencyList, WeightedAdjacencyList } from '../collections/index.js';
|
|
2
|
+
export interface WeightedUndirectedEdge {
|
|
3
|
+
u: number;
|
|
4
|
+
v: number;
|
|
5
|
+
weight: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Connected components in an (unweighted) graph using DSU.
|
|
9
|
+
* Complexity: O((n + m) * alpha(n)).
|
|
10
|
+
*
|
|
11
|
+
* Notes:
|
|
12
|
+
* - Assumes vertices are numbered `0..n-1`.
|
|
13
|
+
* - If your adjacency list stores each edge once or twice, DSU will still work.
|
|
14
|
+
*/
|
|
15
|
+
export declare function connectedComponents(n: number, adj: AdjacencyList<number>): number[][];
|
|
16
|
+
/**
|
|
17
|
+
* Kruskal's algorithm for Minimum Spanning Tree (MST) using DSU.
|
|
18
|
+
*
|
|
19
|
+
* Complexity: O(m log m) for sorting edges + O(m * alpha(n)).
|
|
20
|
+
*
|
|
21
|
+
* @returns a spanning forest if the graph is disconnected.
|
|
22
|
+
*/
|
|
23
|
+
export declare function kruskalMST(n: number, adj: WeightedAdjacencyList<number, number>, options?: {
|
|
24
|
+
undirected?: boolean;
|
|
25
|
+
}): {
|
|
26
|
+
edges: WeightedUndirectedEdge[];
|
|
27
|
+
totalWeight: number;
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../src/algorithms/graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAGpF,MAAM,WAAW,sBAAsB;IACrC,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,CAAC,EAAE,MAAM,EACT,GAAG,EAAE,aAAa,CAAC,MAAM,CAAC,GACzB,MAAM,EAAE,EAAE,CAQZ;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,CAAC,EAAE,MAAM,EACT,GAAG,EAAE,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,OAAO,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GACjC;IAAE,KAAK,EAAE,sBAAsB,EAAE,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAgC1D"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { DisjointSetUnion } from './disjointSetUnion.js';
|
|
2
|
+
/**
|
|
3
|
+
* Connected components in an (unweighted) graph using DSU.
|
|
4
|
+
* Complexity: O((n + m) * alpha(n)).
|
|
5
|
+
*
|
|
6
|
+
* Notes:
|
|
7
|
+
* - Assumes vertices are numbered `0..n-1`.
|
|
8
|
+
* - If your adjacency list stores each edge once or twice, DSU will still work.
|
|
9
|
+
*/
|
|
10
|
+
export function connectedComponents(n, adj) {
|
|
11
|
+
const dsu = new DisjointSetUnion(n);
|
|
12
|
+
for (let u = 0; u < n; u++) {
|
|
13
|
+
for (const v of adj[u] ?? []) {
|
|
14
|
+
dsu.union(u, v);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return dsu.components(n);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Kruskal's algorithm for Minimum Spanning Tree (MST) using DSU.
|
|
21
|
+
*
|
|
22
|
+
* Complexity: O(m log m) for sorting edges + O(m * alpha(n)).
|
|
23
|
+
*
|
|
24
|
+
* @returns a spanning forest if the graph is disconnected.
|
|
25
|
+
*/
|
|
26
|
+
export function kruskalMST(n, adj, options) {
|
|
27
|
+
const undirected = options?.undirected ?? true;
|
|
28
|
+
// Extract edges from adjacency list.
|
|
29
|
+
// For undirected graphs represented as both-direction adjacency,
|
|
30
|
+
// take only u < v to avoid duplicates.
|
|
31
|
+
const edges = [];
|
|
32
|
+
for (let u = 0; u < n; u++) {
|
|
33
|
+
for (const { to: v, weight } of adj[u] ?? []) {
|
|
34
|
+
if (undirected) {
|
|
35
|
+
if (u < v)
|
|
36
|
+
edges.push({ u, v, weight });
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
edges.push({ u, v, weight });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
edges.sort((a, b) => a.weight - b.weight);
|
|
44
|
+
const dsu = new DisjointSetUnion(n);
|
|
45
|
+
const mstEdges = [];
|
|
46
|
+
let totalWeight = 0;
|
|
47
|
+
for (const e of edges) {
|
|
48
|
+
if (dsu.union(e.u, e.v)) {
|
|
49
|
+
mstEdges.push(e);
|
|
50
|
+
totalWeight += e.weight;
|
|
51
|
+
if (mstEdges.length === n - 1)
|
|
52
|
+
break; // MST complete for connected graphs.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { edges: mstEdges, totalWeight };
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=graph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/algorithms/graph.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAQzD;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CACjC,CAAS,EACT,GAA0B;IAE1B,MAAM,GAAG,GAAG,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC;IACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;YAC7B,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAClB,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAC3B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CACxB,CAAS,EACT,GAA0C,EAC1C,OAAkC;IAElC,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,IAAI,CAAC;IAE/C,qCAAqC;IACrC,iEAAiE;IACjE,uCAAuC;IACvC,MAAM,KAAK,GAA6B,EAAE,CAAC;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Index, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './array.js';
|
|
2
|
+
export { DisjointSetUnion } from './disjointSetUnion.js';
|
|
3
|
+
export type { WeightedUndirectedEdge } from './graph.js';
|
|
4
|
+
export { connectedComponents, kruskalMST } from './graph.js';
|
|
2
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,GACV,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,GACV,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,YAAY,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/algorithms/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './array.js';
|
|
2
|
+
export { DisjointSetUnion } from './disjointSetUnion.js';
|
|
3
|
+
export { connectedComponents, kruskalMST } from './graph.js';
|
|
2
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,GACV,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,GACV,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -30,6 +30,16 @@ export type AdjacencyList<Vertex = number> = Vertex[][];
|
|
|
30
30
|
* adj[u].push({ to: v, weight: w });
|
|
31
31
|
*/
|
|
32
32
|
export type WeightedAdjacencyList<Vertex = number, Weight = number> = WeightedEdge<Vertex, Weight>[][];
|
|
33
|
+
/**
|
|
34
|
+
* Create an empty unweighted graph with `n` vertices (0..n-1).
|
|
35
|
+
* Equivalent to C++: `vector<vector<int>> graph(n);`
|
|
36
|
+
*/
|
|
37
|
+
export declare function createAdjacencyList(n: number): AdjacencyList<number>;
|
|
38
|
+
/**
|
|
39
|
+
* Create an empty weighted graph with `n` vertices (0..n-1).
|
|
40
|
+
* Equivalent to C++: `vector<vector<pair<int,int>>> graph(n);`
|
|
41
|
+
*/
|
|
42
|
+
export declare function createWeightedAdjacencyList(n: number): WeightedAdjacencyList<number, number>;
|
|
33
43
|
/**
|
|
34
44
|
* Add an edge to a graph adjacency list.
|
|
35
45
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Graph.d.ts","sourceRoot":"","sources":["../../src/collections/Graph.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,yDAAyD;AACzD,MAAM,WAAW,YAAY,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM;IAC5D,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;GAKG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,GAAG,MAAM,IAAI,MAAM,EAAE,EAAE,CAAC;AAExD;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,qBAAqB,CAC/B,MAAM,GAAG,MAAM,EACf,MAAM,GAAG,MAAM,IACb,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC;AAErC;;;;;;GAMG;AACH,wBAAgB,OAAO,CACrB,GAAG,EAAE,aAAa,CAAC,MAAM,CAAC,EAC1B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACR,IAAI,CAAC;AACR,wBAAgB,OAAO,CACrB,GAAG,EAAE,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACR,IAAI,CAAC;AAgBR;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,GAAG,EAAE,aAAa,CAAC,MAAM,CAAC,EAC1B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACR,IAAI,CAAC;AACR,wBAAgB,UAAU,CACxB,GAAG,EAAE,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACR,IAAI,CAAC"}
|
|
1
|
+
{"version":3,"file":"Graph.d.ts","sourceRoot":"","sources":["../../src/collections/Graph.ts"],"names":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AM,GACR,IAAI,CAAC"}
|
|
@@ -5,6 +5,20 @@
|
|
|
5
5
|
* vector<vector<int>> adj(n);
|
|
6
6
|
* vector<vector<pair<int,int>>> adj(n);
|
|
7
7
|
*/
|
|
8
|
+
/**
|
|
9
|
+
* Create an empty unweighted graph with `n` vertices (0..n-1).
|
|
10
|
+
* Equivalent to C++: `vector<vector<int>> graph(n);`
|
|
11
|
+
*/
|
|
12
|
+
export function createAdjacencyList(n) {
|
|
13
|
+
return Array.from({ length: n }, () => []);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create an empty weighted graph with `n` vertices (0..n-1).
|
|
17
|
+
* Equivalent to C++: `vector<vector<pair<int,int>>> graph(n);`
|
|
18
|
+
*/
|
|
19
|
+
export function createWeightedAdjacencyList(n) {
|
|
20
|
+
return Array.from({ length: n }, () => []);
|
|
21
|
+
}
|
|
8
22
|
export function addEdge(adj, u, v, w) {
|
|
9
23
|
if (w === undefined) {
|
|
10
24
|
adj[u].push(v);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Graph.js","sourceRoot":"","sources":["../../src/collections/Graph.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"Graph.js","sourceRoot":"","sources":["../../src/collections/Graph.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAiCH;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,CAAS;IAC3C,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;AAC7C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,2BAA2B,CACzC,CAAS;IAET,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;AAC7C,CAAC;AAoBD,MAAM,UAAU,OAAO,CACrB,GAEyC,EACzC,CAA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iSet.js';
|
|
|
11
11
|
export { OrderedMultiMap } from './OrderedMultiMap.js';
|
|
12
12
|
export { RedBlackTree, RBNode } from './RedBlackTree.js';
|
|
13
13
|
export type { WeightedEdge, AdjacencyList, WeightedAdjacencyList } from './Graph.js';
|
|
14
|
-
export { addEdge, deleteEdge } from './Graph.js';
|
|
14
|
+
export { createAdjacencyList, createWeightedAdjacencyList, addEdge, deleteEdge } from './Graph.js';
|
|
15
15
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACzD,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACzD,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACrF,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -10,5 +10,5 @@ export { UnorderedSet } from './UnorderedSet.js';
|
|
|
10
10
|
export { OrderedMultiSet } from './OrderedMultiSet.js';
|
|
11
11
|
export { OrderedMultiMap } from './OrderedMultiMap.js';
|
|
12
12
|
export { RedBlackTree, RBNode } from './RedBlackTree.js';
|
|
13
|
-
export { addEdge, deleteEdge } from './Graph.js';
|
|
13
|
+
export { createAdjacencyList, createWeightedAdjacencyList, addEdge, deleteEdge } from './Graph.js';
|
|
14
14
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OA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E,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
* @example
|
|
6
6
|
* import { Vector, Stack, Queue, List, sort, binarySearch, clamp } from 'typescript-dsa-stl';
|
|
7
7
|
*/
|
|
8
|
-
export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, } from './collections/index.js';
|
|
8
|
+
export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, } from './collections/index.js';
|
|
9
9
|
export type { WeightedEdge, AdjacencyList, WeightedAdjacencyList } from './collections/index.js';
|
|
10
|
-
export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './algorithms/index.js';
|
|
10
|
+
export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, connectedComponents, kruskalMST, } from './algorithms/index.js';
|
|
11
|
+
export type { WeightedUndirectedEdge } from './algorithms/index.js';
|
|
11
12
|
export { clamp, range, noop, identity, swap } from './utils/index.js';
|
|
12
13
|
export type { Comparator, Predicate, UnaryFn, Reducer, IterableLike } from './types/index.js';
|
|
13
14
|
export { toArray } from './types/index.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,GAC5B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AACjG,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,EACT,gBAAgB,EAChB,mBAAmB,EACnB,UAAU,GACX,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACtE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @example
|
|
6
6
|
* import { Vector, Stack, Queue, List, sort, binarySearch, clamp } from 'typescript-dsa-stl';
|
|
7
7
|
*/
|
|
8
|
-
export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, } from './collections/index.js';
|
|
9
|
-
export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, } from './algorithms/index.js';
|
|
8
|
+
export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, } from './collections/index.js';
|
|
9
|
+
export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, connectedComponents, kruskalMST, } from './algorithms/index.js';
|
|
10
10
|
export { clamp, range, noop, identity, swap } from './utils/index.js';
|
|
11
11
|
export { toArray } from './types/index.js';
|
|
12
12
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,GAC5B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,EACT,gBAAgB,EAChB,mBAAmB,EACnB,UAAU,GACX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-dsa-stl",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, List, PriorityQueue, Map, Set, sort, binarySearch. Use like C++ STL.",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, List, PriorityQueue, Map, Set, sort, binarySearch, graph utilities. Use like C++ STL.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|