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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@easy-data-structure-js/union-find

v1.0.0

Published

A TypeScript implementation of union-find (disjoint set) data structure

Readme

Union-Find

A TypeScript implementation of union-find (disjoint set) data structure with path compression and union by rank optimizations.

Installation

npm install @easy-data-structure-js/union-find

# or
pnpm add @easy-data-structure-js/union-find

Usage

import { UnionFind } from '@easy-data-structure-js/union-find';

const uf = new UnionFind(5); // Create union-find with 5 elements (0-4)

// Union operations
uf.union(0, 1);
uf.union(2, 3);

// Check if elements are connected
console.log(uf.connected(0, 1)); // true
console.log(uf.connected(0, 2)); // false

// Get number of disjoint sets
console.log(uf.getCount()); // 3

// Find root of element
console.log(uf.find(0)); // root of element 0

Features

  • Path compression for efficient find operations
  • Union by rank for balanced trees
  • TypeScript support with full type safety
  • Zero dependencies
  • Optimized performance: nearly O(1) for both union and find operations

API

constructor(n: number)

Creates a union-find data structure with n elements (0 to n-1).

find(x: number): number

Finds the root of element x with path compression. Time complexity: O(α(n)) where α is the inverse Ackermann function.

union(x: number, y: number): boolean

Unions the sets containing x and y. Returns true if union was performed, false if they were already connected. Time complexity: O(α(n)).

connected(x: number, y: number): boolean

Returns true if x and y are in the same set. Time complexity: O(α(n)).

getCount(): number

Returns the number of disjoint sets.

Use Cases

  • Network connectivity problems
  • Kruskal's minimum spanning tree algorithm
  • Dynamic connectivity queries
  • Percolation problems
  • Image processing (connected components)