struct::disjointset(3tcl)

πŸ“¦
version 2.0 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

struct::disjointset β€” the disjoint set data structure, straight out of the Tcl Data Structures manual bestie πŸ’…


✨ SYNOPSIS ✨

okay so this is literally how you summon it:

package require Tcl 8.6 9

package require struct::disjointset ?1.2?

::struct::disjointset disjointsetName

disjointsetName option ?arg arg ...?

disjointsetName add-element item

disjointsetName add-partition elements

disjointsetName partitions

disjointsetName num-partitions

disjointsetName equal a b

disjointsetName merge a b

disjointsetName find e

disjointsetName exemplars

disjointsetName find-exemplar e

disjointsetName destroy

πŸ¦‹ DESCRIPTION πŸ¦‹

So this package hands you disjoint sets. Another name for this whole vibe is merge-find, iykyk.

Normally when you're doing sets and their elements, the question is "Is this element E contained in this set S?" β€” and you already know both E and S, easy.

Here tho? The question is "Which of several sets contains the element E?" Like, the element is known but the set is NOT, and you wanna find it fast. It's not exactly the inverse of the original question, but it's close, fr fr. Another operation people always want is quickly merging two sets into one, with the result still being fast for finding elements. That's why the alternative term is merge-find.

So why is it called a disjoint-set? Because another way of describing the whole situation is that we have:

  • a finite set S, containing
  • a number of elements E, split into
  • a set of partitions P. The latter term applies, because the intersection of each pair P, P' of partitions is empty, with the union of all partitions covering the whole set.
  • An alternative name for the partitions would be equivalence classes, and all elements in the same class are considered as equal.

Here's a pictorial representation of all the concepts above, no cap:

          +-----------------+ The outer lines are the boundaries of the set S.
          |           /     | The inner regions delineated by the skewed lines
          |  *       /   *  | are the partitions P. The *'s denote the elements
          |      *  / \     | E in the set, each in a single partition, their
          |*       /   \    | equivalence class.
          |       /  *  \   |
          |      / *   /    |
          | *   /\  * /     |
          |    /  \  /      |
          |   /    \/  *    |
          |  / *    \       |
          | /     *  \      |
          +-----------------+

For more info, go look at http://en.wikipedia.org/wiki/Disjoint_set_data_structure 🫢


πŸŽ€ API πŸŽ€

The package exports exactly one command, ::struct::disjointset. Every single thing it can do is reachable through a subcommand of that command. Obsessed honestly.

::struct::disjointset disjointsetName

Creates a brand new disjoint set object with an associated global Tcl command whose name is disjointsetName. That command is your girl β€” you use it to invoke all the various operations on the disjointset. It has this general form:

disjointsetName option ?arg arg ...?

The option and the args decide the exact behavior of the command. These are the commands you can throw at a disjointset object:

disjointsetName add-element item

Makes a new partition in the specified disjoint set and fills it with the single item item. The command keeps the integrity of the disjoint set intact, meaning it verifies that none of the elements are already part of the disjoint set, and it throws an error otherwise 🚨

The result of this method is the empty string.

This method runs in constant time. periodt.

disjointsetName add-partition elements

Makes a new partition in the specified disjoint set and fills it with the values found in the set of elements. Same energy as above: the command maintains the integrity of the disjoint set, i.e. it verifies that none of the elements are already part of the disjoint set, and throws an error otherwise.

The result of the command is the empty string.

This method runs in time proportional to the size of elements].

disjointsetName partitions

Returns the set of partitions the named disjoint set currently consists of. The form of the result is a list of lists; the inner lists contain the elements of the partitions.

This method runs in time O(N*alpha(N)), where N is the number of elements in the disjoint set and alpha is the inverse Ackermann function.

disjointsetName num-partitions

Returns how many partitions the named disjoint set currently consists of.

This method runs in constant time.

disjointsetName equal a b

Figures out whether the two elements a and b of the disjoint set belong to the same partition. The result of the method is a boolean value: True if the two elements are contained in the same partition, False otherwise.

An error will be thrown if either a or b are not elements of the disjoint set. πŸ’€

This method runs in amortized time O(alpha(N)), where N is the number of elements in the larger partition and alpha is the inverse Ackermann function.

disjointsetName merge a b

Finds the partitions that the elements a and b live in and merges them into a single partition. If the two elements were already chillin' in the same partition, nothing will change.

The result of the method is the empty string.

This method runs in amortized time O(alpha(N)), where N is the number of items in the larger of the partitions being merged. The worst case time is O(N).

disjointsetName find e

Returns a list of the members of the partition of the disjoint set which contains the element e.

This method runs in O(N*alpha(N)) time, where N is the total number of items in the disjoint set and alpha is the inverse Ackermann function. See find-exemplar for a faster method, if all that's needed is a unique identifier for the partition rather than an enumeration of all its elements.

disjointsetName exemplars

Returns a list containing an exemplar of each partition in the disjoint set. The exemplar is a member of the partition, chosen arbitrarily.

This method runs in O(N*alpha(N)) time, where N is the total number of items in the disjoint set and alpha is the inverse Ackermann function.

disjointsetName find-exemplar e

Returns the exemplar of the partition of the disjoint set containing the element e. Throws an error if e is not found in the disjoint set. The exemplar is an arbitrarily chosen member of the partition. The only operation that will change the exemplar of any partition is merge.

This method runs in O(alpha(N)) time, where N is the number of items in the partition containing E, and alpha is the inverse Ackermann function.

disjointsetName destroy

Destroys the disjoint set object and all associated memory. bye bestie πŸ‘‹


🌸 BUGS, IDEAS, FEEDBACK 🌸

This document, and the package it describes, will undoubtedly contain bugs and other problems (it's giving humbling, honestly). Please report such in the category struct :: disjointset of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation. We wanna hear it 🫢

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.


πŸ‘‘ KEYWORDS πŸ‘‘

disjoint set, equivalence class, find, merge find, partition, partitioned set, union


πŸŽ€ CATEGORY πŸŽ€

Data structures


and that's the whole page, bestie πŸ’– this one ships with tcllib, version 1.2, and it's the struct::disjointset(3tcl) man page β€” go be the main character of your own union-find βœ¨πŸŒˆπŸ¦‹

πŸ“¦ where this page comes from