struct::disjointset(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– struct::disjointset β€” the manual, but make it make sense πŸ’–

okay bestie, sit down, we're learning struct::disjointset, from the Tcl Data Structures corner of the tcllib universe. it's a whole vibe and honestly? kind of iconic. let's get into it ✨


✨ NAME ✨

struct::disjointset β€” a disjoint set data structure 🫢


🌸 SYNOPSIS 🌸

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 structure is merge-find, and honestly both names are doing their own thing.

normally, when you're dealing with 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. slay.

here though? the question flips. it's "which of several sets contains the element E?" like, the element is known, the set is not, and we want to find it fast. it's not quite the exact inverse of the original question, but it's close, bestie. and the other thing everyone wants is to quickly merge two sets into one, and still have the result be fast for finding elements. hence: merge-find. iykyk πŸ’…

and why is it called a disjoint-set? okay so, 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 term "partition" 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 little pictorial representation of all these concepts, because a picture is worth a thousand words and this one is lowkey a work of art:

         +-----------------+ 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 (yes, the classics), see http://en.wikipedia.org/wiki/Disjoint_set_data_structure.


πŸ‘‘ API πŸ‘‘

the package exports a single command, ::struct::disjointset. every single thing this package can do is reachable through a subcommand of that command. one command to rule them all, main character energy fr fr.

  • ::struct::disjointset disjointsetName β€” creates a brand new disjoint set object with an associated global Tcl command whose name is disjointsetName. that command is what you use to invoke various operations on the disjointset. its general form is:
disjointsetName option ?arg arg ...?

the option and the args determine the exact behavior of the command. the following commands are possible for disjointset objects:

  • disjointsetName add-element item β€” creates 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, i.e. it verifies that none of the elements are already part of the disjoint set, and throws an error if they are. πŸ™ˆ

the result of this method is the empty string.

this method runs in constant time. we love a fast queen.

  • disjointsetName add-partition elements β€” creates a new partition in the specified disjoint set and fills it with the values found in the set of elements. the command keeps the integrity of the disjoint set intact, 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. math girlies rise up πŸŽ€

  • disjointsetName num-partitions β€” returns the number of 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 is a boolean value: True if the two elements are 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 β€” determines the partitions the elements a and b are contained in and merges them into a single partition. if the two elements were already in the same partition, nothing changes. no drama, no notes.

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. it's giving "we picked one at random, don't ask questions".

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. rip to a real one πŸ•ŠοΈ

🌈 BUGS, IDEAS, FEEDBACK 🌈

this document, and the package it describes, will undoubtedly contain bugs and other problems. it's giving "we're all doing our best". 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 want the glow-up too 🫢

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. yes bestie, that specific button. you got this.


πŸ’œ KEYWORDS πŸ’œ

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


⭐ CATEGORY ⭐

Data structures


and that's the whole thing, bestie! this page came to you from tcllib, version 1.2, dated 1.2 β€” go forth and merge-find your way to greatness, no cap πŸŽ€βœ¨πŸ’…

πŸ“¦ where this page comes from