Union-find: Difference between revisions

From Algowiki
Jump to navigation Jump to search
Line 11: Line 11:
== Methods ==
== Methods ==


Name: '''find'''.
=== find ===
 
Input: An element <math>i\in\{1,\ldots,N\}</math>.
Input: An element <math>i\in\{1,\ldots,N\}</math>.
Output: The element <math>j\in\{1,\ldots,N\}</math> that represents the subset to which <math>i</math> currently belongs (<math>i=j</math> is possible).
Output: The element <math>j\in\{1,\ldots,N\}</math> that represents the subset to which <math>i</math> currently belongs (<math>i=j</math> is possible).
Precondition:
Precondition:
Postcondition:
Postcondition:
Method


Name:  '''unite'''.
 
=== unite ===
 
Input: Two elements, <math>i,j\in\{1,\ldots,N\}</math>.
Input: Two elements, <math>i,j\in\{1,\ldots,N\}</math>.
Output:
 
Output: none.
 
Precondition: The elements <math>i</math> and <math>j</math> do not belong to the same subset. In other words, it is find<math>(i)\neq</math>find<math>(j)</math>.
Precondition: The elements <math>i</math> and <math>j</math> do not belong to the same subset. In other words, it is find<math>(i)\neq</math>find<math>(j)</math>.
Postcondition: The subsets to which <math>i</math> and <math>j</math> belong are united (the representative of the united set is not specified).
Postcondition: The subsets to which <math>i</math> and <math>j</math> belong are united (the representative of the united set is not specified).


== Known implementations ==
== Known implementations ==


# Union-find with lists
# Union-find with lists

Revision as of 12:34, 20 June 2015

General information

Representation invariant:

  1. There is a natural number [math]\displaystyle{ N\in\mathbb{N} }[/math], which is constant throughout the lifetime of the object.
  2. At any moment, there is a (dynamically changing) partition of [math]\displaystyle{ S:=\{1,\ldots,N\} }[/math] into non-empty disjoint subsets. Each element of [math]\displaystyle{ S }[/math] belongs to exactly one of these partition sets.
  3. Each of these subsets [math]\displaystyle{ S'\subseteq S }[/math] is represented by an arbitrary particular member of [math]\displaystyle{ S' }[/math], which does not change as long as [math]\displaystyle{ S' }[/math] is one of the partition sets.

Constructor: Receives [math]\displaystyle{ N }[/math] as its input and initializes the union-find object such that each [math]\displaystyle{ i\in\{1,\ldots,N\} }[/math] is a singleton [math]\displaystyle{ \{i\} }[/math], which is represented by its unique member [math]\displaystyle{ i }[/math].

Methods

find

Input: An element [math]\displaystyle{ i\in\{1,\ldots,N\} }[/math].

Output: The element [math]\displaystyle{ j\in\{1,\ldots,N\} }[/math] that represents the subset to which [math]\displaystyle{ i }[/math] currently belongs ([math]\displaystyle{ i=j }[/math] is possible).

Precondition:

Postcondition:


unite

Input: Two elements, [math]\displaystyle{ i,j\in\{1,\ldots,N\} }[/math].

Output: none.

Precondition: The elements [math]\displaystyle{ i }[/math] and [math]\displaystyle{ j }[/math] do not belong to the same subset. In other words, it is find[math]\displaystyle{ (i)\neq }[/math]find[math]\displaystyle{ (j) }[/math].

Postcondition: The subsets to which [math]\displaystyle{ i }[/math] and [math]\displaystyle{ j }[/math] belong are united (the representative of the united set is not specified).

Known implementations

  1. Union-find with lists