1. Definition: Equivalence class refers to the largest set of mutually equivalent elements.
2. Online equivalence class
⑴ initially, only n elements, each element belongs to a separate equivalence class.
⑵ needs to do the following:
Combine the equivalence class containing a, b into a class Combina (A, b); Determines which class contains element E, that is, determines whether two elements are in the same find (e), and does not return different results. Combina (a) equivalent I=find (a), j=find (b); if (i!=j) Union (I,J);
⑶ Solution: Describe each collection (class) as a tree, using a linked list method to describe the tree. Each node must have a parent domain, but it does not have to have a children domain. The number within the node is the value of its parent field, and the number outside the node is its index. The index is also the element represented by the node. The parent domain of the root node is set to 0;
⑷ Code implementation:
int Find (int e)
{
while (!root[e])
E= Parent[e];
Returne;
}
Height rule: If the height of tree I is less than the height of the tree J, then J is the parent node of I.
Weight rule: If the number of nodes of tree I is greater than the number of nodes of Tree J, then J is the parent node of I.
In the merge algorithm, a Boolean domain root is added to each node, and if the current node is a node, the root domain is true.
The parent domain of each root node is used to hold the total number of nodes in the tree.
voidunion (int i, int j)//merge two trees with root i,j;
{
if (parent[i]< parent[j])
{
parent[j]+= Parent[i];
Root[i]= false;
Parent[i]= J;
}
Else
{
parent[i]+= Parent[j];
Root[j]= false;
parent[j]= i;
}
}
Path compression:
int Find (int e)
{
Int j = e;
while (!root[j])//Search root node
J =parent[j];
int f =e;
while (F!=J)
{
Int pf= Parent[f];
Parent[f]= J;
F =PF;
}
Return J;
}
3. Offline equivalence class
The input of the offline equivalence class is the number of elements n, the number of relationships R, and the r pair relationship, the object of the problem is that n elements are assigned to the corresponding equivalence class.