~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/core-api/union_find.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php