{"id":21129,"library":"disjoint-set","title":"Disjoint Set","description":"A Python implementation of the disjoint set (union-find) data structure, featuring path compression and union by rank/size. Current version 0.9.0, requires Python >=3.10. The library is stable with occasional updates.","status":"active","version":"0.9.0","language":"python","source_language":"en","source_url":"https://github.com/mrapacz/disjoint-set","tags":["disjoint-set","union-find","data-structures"],"install":[{"cmd":"pip install disjoint-set","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"","wrong":"","symbol":"DisjointSet","correct":"from disjoint_set import DisjointSet"}],"quickstart":{"code":"from disjoint_set import DisjointSet\n\nds = DisjointSet()\nds.find(1)  # returns 1\nds.union(1, 2)\nds.find(2)  # returns 1 (or 2, depending on implementation)\nprint(ds.connected(1, 2))  # True\nprint(len(ds))  # 1 (number of disjoint sets)","lang":"python","description":"Quickstart: create a DisjointSet, use union and find."},"warnings":[{"fix":"Explicitly call 'ds.find(x)' for each element before unions, or use 'ds.add(x)' if available (not in this library).","message":"The library does not support initializing with a set of elements; elements are added implicitly on find/union. If you need to pre-populate, call find on each element first.","severity":"gotcha","affected_versions":"all"},{"fix":"Always call find after union to compress paths immediately for subsequent operations.","message":"The library uses path compression and union by size, but the implementation may not guarantee O(alpha(n)) amortized if used incorrectly. Ensure you always call union with two elements (creates if missing).","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Install using 'pip install disjoint-set' and import as 'from disjoint_set import DisjointSet'.","cause":"Incorrect import path or library not installed.","error":"ImportError: cannot import name 'DisjointSet' from 'disjoint_set'"},{"fix":"Instantiate without arguments: ds = DisjointSet(); then add elements via find/union.","cause":"Trying to pass initial elements to the constructor, which is not supported.","error":"TypeError: DisjointSet() takes no arguments"},{"fix":"Use 'find' to implicitly add an element: ds.find(x).","cause":"Attempting to use a non-existent method 'add'. This library does not have an explicit add method.","error":"AttributeError: 'DisjointSet' object has no attribute 'add'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}