Skip to content

Commit c1f8e99

Browse files
committed
✨ feat: refactor findset
1 parent 615025d commit c1f8e99

16 files changed

+422
-621
lines changed

Diff for: .eslintrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"files": ["**/*.ts"],
2727
"extends": ["@guanghechen", "@guanghechen/ts", "prettier"],
2828
"rules": {
29-
"import/no-named-as-default": 0
29+
"import/no-named-as-default": 0,
30+
"no-plusplus": 0,
31+
"no-return-assign": 0
3032
}
3133
},
3234
{

Diff for: MIGRATION.md

+10
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,13 @@ import { dijkstraBigint } from '@algorithm.ts/dijkstra'
135135
### @algorithm.ts/dlx
136136

137137
1. Use `new DancingLinkX({ MAX_N: <number> })` instead of `createDLX(<number>)`
138+
139+
140+
### @algorithm.ts/findset
141+
142+
1. Use `new Findset()` instead of `createFindset()`.
143+
2. Use `new HeuristicFindset()` instead of `createHeuristicFindset()`.
144+
3. Use `new EnhancedFindset()` instead of `createEnhancedFindset()`.
145+
4. `.size(<number>)` is renamed to `.count(<number>)`.
146+
5. `.resetNode(<number>)` is removed.
147+

Diff for: packages/findset/README-zh.md

-153
This file was deleted.

Diff for: packages/findset/README.md

+36-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<header>
22
<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>
44
</h1>
55
<div align="center">
66
<a href="https://www.npmjs.com/package/@algorithm.ts/findset">
@@ -50,12 +50,11 @@
5050
<br/>
5151

5252

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].
5555

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:
5958

6059
1. Determine whether two nodes are in a synonymous tree.
6160
2. Merge two trees.
@@ -75,14 +74,15 @@ amortized constant time complexity:
7574
yarn add @algorithm.ts/findset
7675
```
7776

77+
7878
## Usage
7979

8080
* Create a ordinary findset:
8181

8282
```typescript
83-
import { createFindset } from '@algorithm.ts/findset'
83+
import { Findset } from '@algorithm.ts/findset'
8484

85-
const findset = createFindset()
85+
const findset = new Findset()
8686

8787
// Initialize the findset with 1000 node.
8888
findset.init(1000)
@@ -105,9 +105,9 @@ amortized constant time complexity:
105105
number of executions of subsequent queries.
106106

107107
```typescript
108-
import { createHeuristicFindset } from '@algorithm.ts/findset'
108+
import { HeuristicFindset } from '@algorithm.ts/findset'
109109

110-
const findset = createHeuristicFindset()
110+
const findset = new HeuristicFindset()
111111

112112
// Initialize the findset with 1000 node.
113113
findset.init(1000)
@@ -122,10 +122,10 @@ amortized constant time complexity:
122122
assert(findset.root(2) === findset.root(3))
123123

124124
// 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
129129
```
130130

131131
* Create an enhanced findset:
@@ -134,48 +134,56 @@ amortized constant time complexity:
134134
all the nodes on a given tree (access through the root node).
135135

136136
```typescript
137-
import { createEnhancedFindset } from '@algorithm.ts/findset'
137+
import { EnhancedFindset } from '@algorithm.ts/findset'
138138

139-
const findset = createEnhancedFindset(100)
139+
const findset = new EnhancedFindset()
140140

141141
findset.init(100)
142-
findset.size(1) // => 1
142+
findset.count(1) // => 1
143143
findset.merge(1, 2)
144-
findset.size(1) // => 2
145-
findset.size(2) // => 2
144+
findset.count(1) // => 2
145+
findset.count(2) // => 2
146146
findset.getSetOf(1) // => Set {1, 2}
147147
findset.getSetOf(2) // => Set {1, 2}
148148
```
149149

150-
### Example
150+
151+
## Example
151152

152153
* A solution for leetcode "Find All People With Secret"
153154
(https://leetcode.com/problems/find-all-people-with-secret/):
154155

155156
```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+
}
158166

159167
const MAX_N = 1e5 + 10
160168
const answer: Set<number> = new Set()
161169
const nodes: Set<number> = new Set()
162170
const visited: Uint8Array = new Uint8Array(MAX_N)
163-
const findset: IEnhancedFindset = createEnhancedFindset(MAX_N)
171+
const findset = new MyFindset()
164172

165173
export function findAllPeople(N: number, meetings: number[][], firstPerson: number): number[] {
166-
const M: number = meetings.length
167-
168174
answer.clear()
169175
answer.add(1)
170176
answer.add(firstPerson + 1)
171177

178+
const M: number = meetings.length
172179
meetings
173180
.sort((x, y) => x[2] - y[2])
174181
.forEach(item => {
175182
item[0] += 1
176183
item[1] += 1
177184
})
178185

186+
findset.init(N)
179187
for (let i = 0, j: number; i < M; i = j) {
180188
const t: number = meetings[i][2]
181189
for (j = i + 1; j < M; ++j) {
@@ -190,7 +198,7 @@ amortized constant time complexity:
190198
}
191199

192200
for (const x of nodes) {
193-
findset.initNode(x)
201+
findset.resetNode(x)
194202
visited[x] = 0
195203
}
196204

@@ -206,7 +214,7 @@ amortized constant time complexity:
206214
if (visited[xx]) continue
207215
visited[xx] = 1
208216

209-
const xxSet: Set<number> = findset.getSetOf(xx)!
217+
const xxSet = findset.getSetOf(xx)!
210218
for (const t of xxSet) answer.add(t)
211219
}
212220
}
@@ -222,5 +230,5 @@ amortized constant time complexity:
222230
* [Disjoint-set data structure | Wikipedia][wiki-find-set]
223231

224232

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
226234
[wiki-find-set]: https://en.wikipedia.org/wiki/Disjoint-set_data_structure

0 commit comments

Comments
 (0)