union_find#

pybear.utilities.union_find(pairs)#

Use the union-find algorithm to find groups of connected values from disjoint pairs of connected values.

Requires an sequence list-like container holding sequence list-like pairs of values. The contents of the pairs are not validated, but must be hashable by a Python dictionary and must be compatible with Python ‘==’ and ‘!=’ operators. Python lists and tuples are tested and recommended, though other list-like containers such as sets and numpy arrays are likely to work. The output is not sorted in any way; any sorting needs to be done external to union_find.

Parameters:
pairsSequence[Sequence[Any]]

Disjoint pairs of connected values.

Returns:
_connectedtuple[tuple[Any, …], …]

The unions of the disjoint pairs of connected values into groups of mutually connected values.

Examples

>>> from pybear.utilities import union_find
>>> pairs = [(0, 4), (7, 3), (5, 9), (5, 4), (3, 8)]
>>> out = union_find(pairs)
>>> print(out)
((0, 4, 5, 9), (7, 3, 8))