|
| 1 | +.. SPDX-License-Identifier: GPL-2.0 |
| 2 | +
|
| 3 | +==================== |
| 4 | +Union-Find in Linux |
| 5 | +==================== |
| 6 | + |
| 7 | + |
| 8 | +:Date: June 21, 2024 |
| 9 | +:Author: Xavier <xavier_qy@163.com> |
| 10 | + |
| 11 | +What is union-find, and what is it used for? |
| 12 | +------------------------------------------------ |
| 13 | + |
| 14 | +Union-find is a data structure used to handle the merging and querying |
| 15 | +of disjoint sets. The primary operations supported by union-find are: |
| 16 | + |
| 17 | + Initialization: Resetting each element as an individual set, with |
| 18 | + each set's initial parent node pointing to itself. |
| 19 | + Find: Determine which set a particular element belongs to, usually by |
| 20 | + returning a “representative element” of that set. This operation |
| 21 | + is used to check if two elements are in the same set. |
| 22 | + Union: Merge two sets into one. |
| 23 | + |
| 24 | +As a data structure used to maintain sets (groups), union-find is commonly |
| 25 | +utilized to solve problems related to offline queries, dynamic connectivity, |
| 26 | +and graph theory. It is also a key component in Kruskal's algorithm for |
| 27 | +computing the minimum spanning tree, which is crucial in scenarios like |
| 28 | +network routing. Consequently, union-find is widely referenced. Additionally, |
| 29 | +union-find has applications in symbolic computation, register allocation, |
| 30 | +and more. |
| 31 | + |
| 32 | +Space Complexity: O(n), where n is the number of nodes. |
| 33 | + |
| 34 | +Time Complexity: Using path compression can reduce the time complexity of |
| 35 | +the find operation, and using union by rank can reduce the time complexity |
| 36 | +of the union operation. These optimizations reduce the average time |
| 37 | +complexity of each find and union operation to O(α(n)), where α(n) is the |
| 38 | +inverse Ackermann function. This can be roughly considered a constant time |
| 39 | +complexity for practical purposes. |
| 40 | + |
| 41 | +This document covers use of the Linux union-find implementation. For more |
| 42 | +information on the nature and implementation of union-find, see: |
| 43 | + |
| 44 | + Wikipedia entry on union-find |
| 45 | + https://en.wikipedia.org/wiki/Disjoint-set_data_structure |
| 46 | + |
| 47 | +Linux implementation of union-find |
| 48 | +----------------------------------- |
| 49 | + |
| 50 | +Linux's union-find implementation resides in the file "lib/union_find.c". |
| 51 | +To use it, "#include <linux/union_find.h>". |
| 52 | + |
| 53 | +The union-find data structure is defined as follows:: |
| 54 | + |
| 55 | + struct uf_node { |
| 56 | + struct uf_node *parent; |
| 57 | + unsigned int rank; |
| 58 | + }; |
| 59 | + |
| 60 | +In this structure, parent points to the parent node of the current node. |
| 61 | +The rank field represents the height of the current tree. During a union |
| 62 | +operation, the tree with the smaller rank is attached under the tree with the |
| 63 | +larger rank to maintain balance. |
| 64 | + |
| 65 | +Initializing union-find |
| 66 | +-------------------- |
| 67 | + |
| 68 | +You can complete the initialization using either static or initialization |
| 69 | +interface. Initialize the parent pointer to point to itself and set the rank |
| 70 | +to 0. |
| 71 | +Example:: |
| 72 | + |
| 73 | + struct uf_node my_node = UF_INIT_NODE(my_node); |
| 74 | +or |
| 75 | + uf_node_init(&my_node); |
| 76 | + |
| 77 | +Find the Root Node of union-find |
| 78 | +-------------------------------- |
| 79 | + |
| 80 | +This operation is mainly used to determine whether two nodes belong to the same |
| 81 | +set in the union-find. If they have the same root, they are in the same set. |
| 82 | +During the find operation, path compression is performed to improve the |
| 83 | +efficiency of subsequent find operations. |
| 84 | +Example:: |
| 85 | + |
| 86 | + int connected; |
| 87 | + struct uf_node *root1 = uf_find(&node_1); |
| 88 | + struct uf_node *root2 = uf_find(&node_2); |
| 89 | + if (root1 == root2) |
| 90 | + connected = 1; |
| 91 | + else |
| 92 | + connected = 0; |
| 93 | + |
| 94 | +Union Two Sets in union-find |
| 95 | +---------------------------- |
| 96 | + |
| 97 | +To union two sets in the union-find, you first find their respective root nodes |
| 98 | +and then link the smaller node to the larger node based on the rank of the root |
| 99 | +nodes. |
| 100 | +Example:: |
| 101 | + |
| 102 | + uf_union(&node_1, &node_2); |
0 commit comments