npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@algorithm.ts/findset

v4.0.4

Published

Find set written in Typescript.

Downloads

284

Readme

A typescript implementation of the Findset data structure, usually also known as Disjoint-set data structure.

The find-set is a data structure used to maintain the node relationship in a forest. Find set support to perform the following operations under the amortized constant time complexity:

  1. Determine whether two nodes are in a synonymous tree.
  2. Merge two trees.

Install

  • npm

    npm install --save @algorithm.ts/findset
  • pnpm

    pnpm add @algorithm.ts/findset

Usage

  • Create a ordinary findset:

    import { Findset } from '@algorithm.ts/findset'
    
    const findset = new Findset()
    
    // Initialize the findset with 1000 node.
    findset.init(1000)
    
    // Find the root node of the tree where the given node located.
    // !!!Attention. The node number must be a positive integer in range of [1, 1000]
    findset.root(4)   // => 4
    
    // Merge two trees.
    findset.merge(2, 3)
    
    assert(findset.root(2) === findset.root(3))
  • Create a heuristic findset:

    The heuristic find-set maintains the number of nodes of each tree on the basis of the ordinary version. When merging trees, always use the root node of the tree with more nodes as the root node of the new tree, which can reduce the number of executions of subsequent queries.

    import { HeuristicFindset } from '@algorithm.ts/findset'
    
    const findset = new HeuristicFindset()
    
    // Initialize the findset with 1000 node.
    findset.init(1000)
    
    // Find the root node of the tree where the given node located.
    // !!!Attention. The node number must be a positive integer in range of [1, 1000]
    findset.root(4)   // => 4
    
    // Merge two trees.
    findset.merge(2, 3)
    
    assert(findset.root(2) === findset.root(3))
    
    // Count the nodes of a tree.
    findset.count(1)   // => 1
    findset.count(2)   // => 2
    findset.count(3)   // => 2
    findset.count(4)   // => 1
  • Create an enhanced findset:

    On the basis of ordinary findset, this enhanced version also supports to get all the nodes on a given tree (access through the root node).

    import { EnhancedFindset } from '@algorithm.ts/findset'
    
    const findset = new EnhancedFindset()
    
    findset.init(100)
    findset.count(1)      // => 1
    findset.merge(1, 2)
    findset.count(1)      // => 2
    findset.count(2)      // => 2
    findset.getSetOf(1)   // => Set {1, 2}
    findset.getSetOf(2)   // => Set {1, 2}

Example

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

    import { EnhancedFindset } from '@algorithm.ts/findset'
    
    class MyFindset extends EnhancedFindset {
      public resetNode(x: number): void {
        this._parent[x] = 0
        this._sets[x].clear()
        this._sets[x].add(x)
      }
    }
    
    const MAX_N = 1e5 + 10
    const answer: Set<number> = new Set()
    const nodes: Set<number> = new Set()
    const visited: Uint8Array = new Uint8Array(MAX_N)
    const findset = new MyFindset()
    
    export function findAllPeople(N: number, meetings: number[][], firstPerson: number): number[] {
      answer.clear()
      answer.add(1)
      answer.add(firstPerson + 1)
    
      const M: number = meetings.length
      meetings
        .sort((x, y) => x[2] - y[2])
        .forEach(item => {
          item[0] += 1
          item[1] += 1
        })
    
      findset.init(N)
      for (let i = 0, j: number; i < M; i = j) {
        const t: number = meetings[i][2]
        for (j = i + 1; j < M; ++j) {
          if (meetings[j][2] !== t) break
        }
    
        nodes.clear()
        for (let k = i; k < j; ++k) {
          const [x, y] = meetings[k]
          nodes.add(x)
          nodes.add(y)
        }
    
        for (const x of nodes) {
          findset.resetNode(x)
          visited[x] = 0
        }
    
        for (let k = i; k < j; ++k) {
          const [x, y] = meetings[k]
          findset.merge(x, y)
        }
    
        for (const x of nodes) {
          if (!answer.has(x)) continue
    
          const xx: number = findset.root(x)
          if (visited[xx]) continue
          visited[xx] = 1
    
          const xxSet = findset.getSetOf(xx)!
          for (const t of xxSet) answer.add(t)
        }
      }
    
      return Array.from(answer)
        .map(x => x - 1)
        .sort((x, y) => x - y)
    }

Related