union_find.rst 3.5 KB

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