1
1
<header >
2
2
<h1 align =" center " >
3
- <a href="https://github.com/guanghechen/algorithm.ts/tree/release-2 .x.x/packages/findset#readme">@algorithm.ts/findset</a>
3
+ <a href="https://github.com/guanghechen/algorithm.ts/tree/release-3 .x.x/packages/findset#readme">@algorithm.ts/findset</a>
4
4
</h1 >
5
5
<div align =" center " >
6
6
<a href="https://www.npmjs.com/package/@algorithm.ts/findset">
50
50
<br />
51
51
52
52
53
- A typescript implementation of the ** Findset** data structure, usually also
54
- known as [ Disjoint-set data structure] [ wiki-find-set ] .
53
+ A typescript implementation of the ** Findset** data structure, usually also known as
54
+ [ Disjoint-set data structure] [ wiki-find-set ] .
55
55
56
- The find-set is a data structure used to maintain the node relationship in a
57
- forest. Find set support to perform the following operations under the
58
- amortized constant time complexity:
56
+ The find-set is a data structure used to maintain the node relationship in a forest. Find set
57
+ support to perform the following operations under the amortized constant time complexity:
59
58
60
59
1 . Determine whether two nodes are in a synonymous tree.
61
60
2 . Merge two trees.
@@ -75,14 +74,15 @@ amortized constant time complexity:
75
74
yarn add @algorithm.ts/findset
76
75
```
77
76
77
+
78
78
## Usage
79
79
80
80
* Create a ordinary findset:
81
81
82
82
``` typescript
83
- import { createFindset } from ' @algorithm.ts/findset'
83
+ import { Findset } from ' @algorithm.ts/findset'
84
84
85
- const findset = createFindset ()
85
+ const findset = new Findset ()
86
86
87
87
// Initialize the findset with 1000 node.
88
88
findset .init (1000 )
@@ -105,9 +105,9 @@ amortized constant time complexity:
105
105
number of executions of subsequent queries.
106
106
107
107
``` typescript
108
- import { createHeuristicFindset } from ' @algorithm.ts/findset'
108
+ import { HeuristicFindset } from ' @algorithm.ts/findset'
109
109
110
- const findset = createHeuristicFindset ()
110
+ const findset = new HeuristicFindset ()
111
111
112
112
// Initialize the findset with 1000 node.
113
113
findset .init (1000 )
@@ -122,10 +122,10 @@ amortized constant time complexity:
122
122
assert (findset .root (2 ) === findset .root (3 ))
123
123
124
124
// Count the nodes of a tree.
125
- findset .size (1 ) // => 1
126
- findset .size (2 ) // => 2
127
- findset .size (3 ) // => 2
128
- findset .size (4 ) // => 1
125
+ findset .count (1 ) // => 1
126
+ findset .count (2 ) // => 2
127
+ findset .count (3 ) // => 2
128
+ findset .count (4 ) // => 1
129
129
```
130
130
131
131
* Create an enhanced findset:
@@ -134,48 +134,56 @@ amortized constant time complexity:
134
134
all the nodes on a given tree (access through the root node).
135
135
136
136
``` typescript
137
- import { createEnhancedFindset } from ' @algorithm.ts/findset'
137
+ import { EnhancedFindset } from ' @algorithm.ts/findset'
138
138
139
- const findset = createEnhancedFindset ( 100 )
139
+ const findset = new EnhancedFindset ( )
140
140
141
141
findset .init (100 )
142
- findset .size (1 ) // => 1
142
+ findset .count (1 ) // => 1
143
143
findset .merge (1 , 2 )
144
- findset .size (1 ) // => 2
145
- findset .size (2 ) // => 2
144
+ findset .count (1 ) // => 2
145
+ findset .count (2 ) // => 2
146
146
findset .getSetOf (1 ) // => Set {1, 2}
147
147
findset .getSetOf (2 ) // => Set {1, 2}
148
148
```
149
149
150
- ### Example
150
+
151
+ ## Example
151
152
152
153
* A solution for leetcode "Find All People With Secret"
153
154
(https://leetcode.com/problems/find-all-people-with-secret/ ):
154
155
155
156
``` typescript
156
- import { createEnhancedFindset } from ' @algorithm.ts/findset'
157
- import type { IEnhancedFindset } from ' @algorithm.ts/findset'
157
+ import { UnsafeEnhancedFindset } from ' @algorithm.ts/findset'
158
+
159
+ class MyFindset extends UnsafeEnhancedFindset {
160
+ public resetNode(x : number ): void {
161
+ this ._parent [x ] = 0
162
+ this ._sets [x ].clear ()
163
+ this ._sets [x ].add (x )
164
+ }
165
+ }
158
166
159
167
const MAX_N = 1e5 + 10
160
168
const answer: Set <number > = new Set ()
161
169
const nodes: Set <number > = new Set ()
162
170
const visited: Uint8Array = new Uint8Array (MAX_N )
163
- const findset: IEnhancedFindset = createEnhancedFindset ( MAX_N )
171
+ const findset = new MyFindset ( )
164
172
165
173
export function findAllPeople(N : number , meetings : number [][], firstPerson : number ): number [] {
166
- const M: number = meetings .length
167
-
168
174
answer .clear ()
169
175
answer .add (1 )
170
176
answer .add (firstPerson + 1 )
171
177
178
+ const M: number = meetings .length
172
179
meetings
173
180
.sort ((x , y ) => x [2 ] - y [2 ])
174
181
.forEach (item => {
175
182
item [0 ] += 1
176
183
item [1 ] += 1
177
184
})
178
185
186
+ findset .init (N )
179
187
for (let i = 0 , j: number ; i < M ; i = j ) {
180
188
const t: number = meetings [i ][2 ]
181
189
for (j = i + 1 ; j < M ; ++ j ) {
@@ -190,7 +198,7 @@ amortized constant time complexity:
190
198
}
191
199
192
200
for (const x of nodes ) {
193
- findset .initNode (x )
201
+ findset .resetNode (x )
194
202
visited [x ] = 0
195
203
}
196
204
@@ -206,7 +214,7 @@ amortized constant time complexity:
206
214
if (visited [xx ]) continue
207
215
visited [xx ] = 1
208
216
209
- const xxSet: Set < number > = findset .getSetOf (xx )!
217
+ const xxSet = findset .getSetOf (xx )!
210
218
for (const t of xxSet ) answer .add (t )
211
219
}
212
220
}
@@ -222,5 +230,5 @@ amortized constant time complexity:
222
230
* [ Disjoint-set data structure | Wikipedia] [ wiki-find-set ]
223
231
224
232
225
- [ homepage ] : https://github.com/guanghechen/algorithm.ts/tree/release-2 .x.x/packages/findset#readme
233
+ [ homepage ] : https://github.com/guanghechen/algorithm.ts/tree/release-3 .x.x/packages/findset#readme
226
234
[ wiki-find-set ] : https://en.wikipedia.org/wiki/Disjoint-set_data_structure
0 commit comments