Skip to main content
ProductsDesktop toolsTASTECADDrawing reviewTASTEFILESFile managementTASTEFTPFile transferTASTESSHRemote workspaceTASTEZIPCompression and extractionTASTEWINMFile managementDeveloper toolsTASTECODERust integrated development environmentTASTESTUDIOIntegrated development and test environmentTASTEETLData integration (ETL)ServersTASTEREMOTERemote desktop serverTASTEGEOGeospatial serverTASTEMAILMail workspaceInvestingTASTESTOCKStock auto-trading desktop appGet startedDownloadsDocumentationBuyAll products
Marketplace
News
TechNewsToday's dev newsDeveloper docsProgramming languagesRustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngularServer frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · ExpressApp & desktop frameworksTauriFlutterReact NativeElectronAlgorithmsAlgorithm basicsCore algorithmsAdvanced algorithmsPractical algorithms
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcaseCommunity guide
Work with usTech transferWebsite buildDesktop app buildRelease and payment pipelineSales partnerContact
ServicesRemote desktopCustom installer packages
TASTEDEV
Products

Desktop tools

TASTECADComplex drawings, opened with easeTASTEFILESJust the files you need, as fast as possibleTASTEFTPThe tidiest path for your filesTASTESSHA stronger link between you and your serversTASTEZIPPack faster, unpack saferTASTEWINMA keyboard-first file manager with two panes

Developer tools

TASTECODEEdit, build, debug and test Rust apps in one windowTASTESTUDIOFrom editing to remote tests and approved AI fixesTASTEETLMove data while you watch the values

Servers

TASTEREMOTEA remote desktop you run on your own serverTASTEGEORebuilding a GeoServer-compatible geospatial server in RustTASTEMAILTake back control of your email

Investing

TASTESTOCKStock auto-trading that follows the rules you set: when, and what

Get started

DownloadsLatest builds of every productDocsHow to use each productBuySubscriptions and perpetual licencesAll productsEvery product at a glance
All products
MarketplaceNews
Tech

News

Today's dev newsOfficial announcements on languages, frameworks, GitHub and AIDeveloper docsGuides to languages, frameworks and algorithms

Programming languages

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

Frameworks

Web front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngular
Server frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
App & desktop frameworksTauriFlutterReact NativeElectron

Algorithms

Algorithm basicsComplexity analysis · Sorting · Searching · Basic data structures · Trees and heapsCore algorithmsGraph traversal · Shortest paths & minimum spanning trees · Dynamic programming · Greedy algorithms · Divide and conquer · BacktrackingAdvanced algorithmsString algorithms · Math and number theory · Bit manipulation · Segment trees · Union-findPractical algorithmsCoding interview strategy · LRU cache · Rate limiting · Consistent hashing
All development docs
Community
ProjectsDiscover builders and their workDevelopment logsDecisions and lessons learnedPromptsReusable instructionsAI tool issuesProblems and workarounds for Claude, Codex, GeminiShowcaseFinished resultsCommunity guideHow we build in the open
Work with us
Tech transferNative apps in Rust and eguiWebsite build10 languages, accounts, docs and downloadsDesktop app buildRust and egui, no WebViewRelease and payment pipelineSigning, MSI, deb, rpm, AppImage, hashesSales partnerRecommend us and earn 30%ContactBuilds, partnerships and investment
Services
Remote desktopReach your computer from a browserCustom installer packagesOnly the features you need, settings preloaded
Buy
EN
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
Log in
Log in
TASTEDEV

From small tools to better development.

support@tastedev.netSupport TASTEDEV
ProductsProduct lineupBuyDocsDownloadsMarketplaceSupport
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcase
PoliciesAbout usPrivacy policyTerms of serviceSubscription termsRefund policyCommunity guidelines
AccountLog inSign up
Visitors
Today
Yesterday
Total views
Company
GXSOFT
Representative
CHOI YOUNG GU
Business registration number
853-44-00368
Address
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
Phone
+82-10-4404-1523
Email
whitecalvin@tastedev.net
Privacy officer
CHOI YOUNG GU

© 2026 TASTEDEVProduct information is based on verified development sources.Released · improving

Union-find docs
Union-find
Concepts: Disjoint Sets and Union-FindHow It Works: Tracing find and unionImplementation: Building a DSU ClassComplexity: Inverse Ackermann in Plain WordsPractice ProblemsReal-World Use and Pitfalls
Discussions

Union-find guide · 6/6

Real-World Use and Pitfalls

Union-find is not just an interview data structure. It lives inside graph libraries, image processing, compilers and data-cleaning pipelines. This chapter shows how real libraries expose it, lists common mistakes, and points to further reading.

Union-find in libraries

Before writing your own, check whether a library you already use provides one.

  • SciPy: scipy.cluster.hierarchy.DisjointSet (since SciPy 1.6) accepts any hashable object as an element, not just integers.
  • NetworkX: networkx.utils.UnionFind is used internally, for example by its Kruskal minimum spanning tree implementation.
  • Boost: the C++ Boost Graph Library provides boost::disjoint_sets, used for incremental connected components.
  • Image processing: connected-component labeling such as scikit-image's label or OpenCV's connectedComponents relies on the same idea to merge provisional labels in the classic two-pass algorithm.
from scipy.cluster.hierarchy import DisjointSet

ds = DisjointSet(["a", "b", "c", "d"])
ds.merge("a", "b")
ds.merge("c", "d")
print(ds.connected("a", "c"))   # False
ds.add("e")
ds.merge("b", "e")
print(sorted(map(sorted, ds.subsets())))  # [['a', 'b', 'e'], ['c', 'd']]

Where it shows up in real systems

  • Networks and infrastructure: tracking which devices can reach each other as links are added.
  • Data cleaning and entity resolution: grouping records that share a phone number, email or device ID into one customer.
  • Compilers and type checkers: unification in type inference and Steensgaard-style points-to analysis merge things proven to be "the same" with union-find.
  • Physics simulations: percolation experiments open random grid cells and check whether the top and bottom have become connected.
  • Clustering: merging every pair of points closer than a threshold gives the same result as single-linkage clustering.

Handling string keys

Real data rarely comes with integer keys. Number each new key with a dictionary and the array-based DSU works unchanged.

class KeyedDSU:
    def __init__(self):
        self.index, self.parent, self.size = {}, [], []

    def _id(self, key):
        if key not in self.index:
            self.index[key] = len(self.parent)
            self.parent.append(len(self.parent))
            self.size.append(1)
        return self.index[key]

    def find(self, key):
        x = self._id(key)
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x

    def union(self, a, b):
        ra, rb = self.find(a), self.find(b)
        if ra == rb:
            return False
        if self.size[ra] < self.size[rb]:
            ra, rb = rb, ra
        self.parent[rb] = ra
        self.size[ra] += self.size[rb]
        return True

d = KeyedDSU()
d.union("phone:555-0100", "mail:kim@x.io")
d.union("mail:kim@x.io", "device:A7")
print(d.find("phone:555-0100") == d.find("device:A7"))  # True

Common pitfalls

  • Linking non-roots: writing parent[a] = b corrupts the sets. Always link the roots returned by find.
  • Comparing parents to test membership: parent[a] == parent[b] can be wrong before paths are compressed. Always use find(a) == find(b).
  • One-based numbering: if vertices are numbered from 1, allocate n+1 slots or subtract 1.
  • Deep recursion: a recursive find in Python raises RecursionError on large inputs. Use a loop.
  • Deletion and undo: the basic structure cannot split sets. If you need undo, use a rollback DSU, which drops path compression and records each change on a stack.
class RollbackDSU:
    def __init__(self, n):
        self.parent, self.size, self.history = list(range(n)), [1] * n, []

    def find(self, x):            # no path compression, so changes stay undoable
        while self.parent[x] != x:
            x = self.parent[x]
        return x

    def union(self, a, b):
        ra, rb = self.find(a), self.find(b)
        if self.size[ra] < self.size[rb]:
            ra, rb = rb, ra
        self.history.append((rb, ra) if ra != rb else None)
        if ra != rb:
            self.parent[rb] = ra
            self.size[ra] += self.size[rb]

    def rollback(self):
        last = self.history.pop()
        if last:
            rb, ra = last
            self.parent[rb] = rb
            self.size[ra] -= self.size[rb]

Summary

Union-find already powers SciPy, NetworkX and Boost, as well as image labeling, type inference and data cleaning. Three habits prevent most bugs: link roots, compare with find, and implement find with a loop. When you need undo, reach for a rollback DSU.

Further reading

  • Disjoint-set data structure (Wikipedia)
  • Disjoint Set Union (cp-algorithms)
  • Case Study: Union-Find (Algorithms, 4th Edition)
  • Union-Find Disjoint Sets (VisuAlgo)
  • scipy.cluster.hierarchy.DisjointSet
  • Kruskal's algorithm (Wikipedia)
Questions about this chapter?
Previous chapterPractice Problems

0 comments

Top commentsNewest first

Sign in · Sign in to leave a comment.