€•CŒsphinx.addnodes”Œdocument”“”)”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)”}”(hhh]”(hŒ pending_xref”“”)”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…””}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ'/translations/zh_CN/core-api/union_find”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)”}”(hhh]”hŒChinese (Traditional)”…””}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/zh_TW/core-api/union_find”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒItalian”…””}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/it_IT/core-api/union_find”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒJapanese”…””}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/ja_JP/core-api/union_find”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒKorean”…””}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/ko_KR/core-api/union_find”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒPortuguese (Brazilian)”…””}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/pt_BR/core-api/union_find”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒSpanish”…””}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ'/translations/sp_SP/core-api/union_find”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒcomment”“”)”}”(hŒ SPDX-License-Identifier: GPL-2.0”h]”hŒ SPDX-License-Identifier: GPL-2.0”…””}”hh·sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1hµhhh²hh³ŒA/var/lib/git/docbuild/linux/Documentation/core-api/union_find.rst”h´KubhŒsection”“”)”}”(hhh]”(hŒtitle”“”)”}”(hŒUnion-Find in Linux”h]”hŒUnion-Find in Linux”…””}”(hhÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÊh²hh³hÇh´KubhŒ field_list”“”)”}”(hhh]”(hŒfield”“”)”}”(hhh]”(hŒ field_name”“”)”}”(hŒDate”h]”hŒDate”…””}”(hhéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hçhhäh³hÇh´KubhŒ field_body”“”)”}”(hŒ June 21, 2024”h]”hŒ paragraph”“”)”}”(hhûh]”hŒ June 21, 2024”…””}”(hhÿh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´Khhùubah}”(h]”h ]”h"]”h$]”h&]”uh1h÷hhäubeh}”(h]”h ]”h"]”h$]”h&]”uh1hâh³hÇh´Khhßh²hubhã)”}”(hhh]”(hè)”}”(hŒAuthor”h]”hŒAuthor”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hçhjh³hÇh´Kubhø)”}”(hŒXavier ”h]”hþ)”}”(hŒXavier ”h]”(hŒXavier <”…””}”(hj-h²hh³Nh´NubhŒ reference”“”)”}”(hŒxavier_qy@163.com”h]”hŒxavier_qy@163.com”…””}”(hj7h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”Œmailto:xavier_qy@163.com”uh1j5hj-ubhŒ>”…””}”(hj-h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K hj)ubah}”(h]”h ]”h"]”h$]”h&]”uh1h÷hjubeh}”(h]”h ]”h"]”h$]”h&]”uh1hâh³hÇh´K hhßh²hubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝhhÊh²hh³hÇh´KubhÉ)”}”(hhh]”(hÎ)”}”(hŒ,What is union-find, and what is it used for?”h]”hŒ,What is union-find, and what is it used for?”…””}”(hjfh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjch²hh³hÇh´K ubhþ)”}”(hŒŒUnion-find is a data structure used to handle the merging and querying of disjoint sets. The primary operations supported by union-find are:”h]”hŒŒUnion-find is a data structure used to handle the merging and querying of disjoint sets. The primary operations supported by union-find are:”…””}”(hjth²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´Khjch²hubhŒ block_quote”“”)”}”(hXpInitialization: Resetting each element as an individual set, with each set's initial parent node pointing to itself. Find: Determine which set a particular element belongs to, usually by returning a “representative element†of that set. This operation is used to check if two elements are in the same set. Union: Merge two sets into one. ”h]”(hŒdefinition_list”“”)”}”(hhh]”(hŒdefinition_list_item”“”)”}”(hŒuInitialization: Resetting each element as an individual set, with each set's initial parent node pointing to itself. ”h]”(hŒterm”“”)”}”(hŒAInitialization: Resetting each element as an individual set, with”h]”hŒAInitialization: Resetting each element as an individual set, with”…””}”(hj•h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j“h³hÇh´KhjubhŒ definition”“”)”}”(hhh]”hþ)”}”(hŒ2each set's initial parent node pointing to itself.”h]”hŒ4each set’s initial parent node pointing to itself.”…””}”(hj¨h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´Khj¥ubah}”(h]”h ]”h"]”h$]”h&]”uh1j£hjubeh}”(h]”h ]”h"]”h$]”h&]”uh1jh³hÇh´KhjŠubjŽ)”}”(hŒÁFind: Determine which set a particular element belongs to, usually by returning a “representative element†of that set. This operation is used to check if two elements are in the same set. ”h]”(j”)”}”(hŒEFind: Determine which set a particular element belongs to, usually by”h]”hŒEFind: Determine which set a particular element belongs to, usually by”…””}”(hjÆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j“h³hÇh´KhjÂubj¤)”}”(hhh]”hþ)”}”(hŒzreturning a “representative element†of that set. This operation is used to check if two elements are in the same set.”h]”hŒzreturning a “representative element†of that set. This operation is used to check if two elements are in the same set.”…””}”(hj×h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´KhjÔubah}”(h]”h ]”h"]”h$]”h&]”uh1j£hjÂubeh}”(h]”h ]”h"]”h$]”h&]”uh1jh³hÇh´KhjŠubeh}”(h]”h ]”h"]”h$]”h&]”uh1jˆhj„ubhþ)”}”(hŒUnion: Merge two sets into one.”h]”hŒUnion: Merge two sets into one.”…””}”(hj÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´Khj„ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j‚h³hÇh´Khjch²hubhþ)”}”(hXÉAs a data structure used to maintain sets (groups), union-find is commonly utilized to solve problems related to offline queries, dynamic connectivity, and graph theory. It is also a key component in Kruskal's algorithm for computing the minimum spanning tree, which is crucial in scenarios like network routing. Consequently, union-find is widely referenced. Additionally, union-find has applications in symbolic computation, register allocation, and more.”h]”hXËAs a data structure used to maintain sets (groups), union-find is commonly utilized to solve problems related to offline queries, dynamic connectivity, and graph theory. It is also a key component in Kruskal’s algorithm for computing the minimum spanning tree, which is crucial in scenarios like network routing. Consequently, union-find is widely referenced. Additionally, union-find has applications in symbolic computation, register allocation, and more.”…””}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´Khjch²hubhþ)”}”(hŒ7Space Complexity: O(n), where n is the number of nodes.”h]”hŒ7Space Complexity: O(n), where n is the number of nodes.”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K"hjch²hubhþ)”}”(hX’Time Complexity: Using path compression can reduce the time complexity of the find operation, and using union by rank can reduce the time complexity of the union operation. These optimizations reduce the average time complexity of each find and union operation to O(α(n)), where α(n) is the inverse Ackermann function. This can be roughly considered a constant time complexity for practical purposes.”h]”hX’Time Complexity: Using path compression can reduce the time complexity of the find operation, and using union by rank can reduce the time complexity of the union operation. These optimizations reduce the average time complexity of each find and union operation to O(α(n)), where α(n) is the inverse Ackermann function. This can be roughly considered a constant time complexity for practical purposes.”…””}”(hj'h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K$hjch²hubhþ)”}”(hŒŒThis document covers use of the Linux union-find implementation. For more information on the nature and implementation of union-find, see:”h]”hŒŒThis document covers use of the Linux union-find implementation. For more information on the nature and implementation of union-find, see:”…””}”(hj5h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K+hjch²hubjƒ)”}”(hŒZWikipedia entry on union-find https://en.wikipedia.org/wiki/Disjoint-set_data_structure ”h]”j‰)”}”(hhh]”jŽ)”}”(hŒXWikipedia entry on union-find https://en.wikipedia.org/wiki/Disjoint-set_data_structure ”h]”(j”)”}”(hŒWikipedia entry on union-find”h]”hŒWikipedia entry on union-find”…””}”(hjNh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j“h³hÇh´K/hjJubj¤)”}”(hhh]”hþ)”}”(hŒ9https://en.wikipedia.org/wiki/Disjoint-set_data_structure”h]”j6)”}”(hjah]”hŒ9https://en.wikipedia.org/wiki/Disjoint-set_data_structure”…””}”(hjch²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”jauh1j5hj_ubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K/hj\ubah}”(h]”h ]”h"]”h$]”h&]”uh1j£hjJubeh}”(h]”h ]”h"]”h$]”h&]”uh1jh³hÇh´K/hjGubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhjCubah}”(h]”h ]”h"]”h$]”h&]”uh1j‚h³hÇh´K.hjch²hubeh}”(h]”Œ*what-is-union-find-and-what-is-it-used-for”ah ]”h"]”Œ,what is union-find, and what is it used for?”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´K ubhÉ)”}”(hhh]”(hÎ)”}”(hŒ"Linux implementation of union-find”h]”hŒ"Linux implementation of union-find”…””}”(hjšh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj—h²hh³hÇh´K2ubhþ)”}”(hŒuLinux's union-find implementation resides in the file "lib/union_find.c". To use it, "#include ".”h]”hŒLinux’s union-find implementation resides in the file “lib/union_find.câ€. To use it, “#include â€.”…””}”(hj¨h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K4hj—h²hubhþ)”}”(hŒ5The union-find data structure is defined as follows::”h]”hŒ4The union-find data structure is defined as follows:”…””}”(hj¶h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K7hj—h²hubhŒ literal_block”“”)”}”(hŒNstruct uf_node { struct uf_node *parent; unsigned int rank; };”h]”hŒNstruct uf_node { struct uf_node *parent; unsigned int rank; };”…””}”hjÆsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÄh³hÇh´K9hj—h²hubhþ)”}”(hXIn this structure, parent points to the parent node of the current node. The rank field represents the height of the current tree. During a union operation, the tree with the smaller rank is attached under the tree with the larger rank to maintain balance.”h]”hXIn this structure, parent points to the parent node of the current node. The rank field represents the height of the current tree. During a union operation, the tree with the smaller rank is attached under the tree with the larger rank to maintain balance.”…””}”(hjÔh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´K>hj—h²hubeh}”(h]”Œ"linux-implementation-of-union-find”ah ]”h"]”Œ"linux implementation of union-find”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´K2ubhÉ)”}”(hhh]”(hÎ)”}”(hŒInitializing union-find”h]”hŒInitializing union-find”…””}”(hjíh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjêh²hh³hÇh´KDubhþ)”}”(hŒ¦You can complete the initialization using either static or initialization interface. Initialize the parent pointer to point to itself and set the rank to 0. Example::”h]”hŒ¥You can complete the initialization using either static or initialization interface. Initialize the parent pointer to point to itself and set the rank to 0. Example:”…””}”(hjûh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´KFhjêh²hubjÅ)”}”(hŒ/struct uf_node my_node = UF_INIT_NODE(my_node);”h]”hŒ/struct uf_node my_node = UF_INIT_NODE(my_node);”…””}”hj sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÄh³hÇh´KKhjêh²hubhþ)”}”(hŒor”h]”hŒor”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´KMhjêh²hubjƒ)”}”(hŒuf_node_init(&my_node); ”h]”hþ)”}”(hŒuf_node_init(&my_node);”h]”hŒuf_node_init(&my_node);”…””}”(hj)h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´KOhj%ubah}”(h]”h ]”h"]”h$]”h&]”uh1j‚h³hÇh´KOhjêh²hubeh}”(h]”Œinitializing-union-find”ah ]”h"]”Œinitializing union-find”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KDubhÉ)”}”(hhh]”(hÎ)”}”(hŒ Find the Root Node of union-find”h]”hŒ Find the Root Node of union-find”…””}”(hjHh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjEh²hh³hÇh´KRubhþ)”}”(hXThis operation is mainly used to determine whether two nodes belong to the same set in the union-find. If they have the same root, they are in the same set. During the find operation, path compression is performed to improve the efficiency of subsequent find operations. Example::”h]”hXThis operation is mainly used to determine whether two nodes belong to the same set in the union-find. If they have the same root, they are in the same set. During the find operation, path compression is performed to improve the efficiency of subsequent find operations. Example:”…””}”(hjVh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´KThjEh²hubjÅ)”}”(hŒ©int connected; struct uf_node *root1 = uf_find(&node_1); struct uf_node *root2 = uf_find(&node_2); if (root1 == root2) connected = 1; else connected = 0;”h]”hŒ©int connected; struct uf_node *root1 = uf_find(&node_1); struct uf_node *root2 = uf_find(&node_2); if (root1 == root2) connected = 1; else connected = 0;”…””}”hjdsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÄh³hÇh´KZhjEh²hubeh}”(h]”Œ find-the-root-node-of-union-find”ah ]”h"]”Œ find the root node of union-find”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KRubhÉ)”}”(hhh]”(hÎ)”}”(hŒUnion Two Sets in union-find”h]”hŒUnion Two Sets in union-find”…””}”(hj}h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjzh²hh³hÇh´Kcubhþ)”}”(hŒ°To union two sets in the union-find, you first find their respective root nodes and then link the smaller node to the larger node based on the rank of the root nodes. Example::”h]”hŒ¯To union two sets in the union-find, you first find their respective root nodes and then link the smaller node to the larger node based on the rank of the root nodes. Example:”…””}”(hj‹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hýh³hÇh´Kehjzh²hubjÅ)”}”(hŒuf_union(&node_1, &node_2);”h]”hŒuf_union(&node_1, &node_2);”…””}”hj™sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÄh³hÇh´Kjhjzh²hubeh}”(h]”Œunion-two-sets-in-union-find”ah ]”h"]”Œunion two sets in union-find”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´Kcubeh}”(h]”Œunion-find-in-linux”ah ]”h"]”Œunion-find in linux”ah$]”h&]”uh1hÈhhh²hh³hÇh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÇuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)”}”(hÍNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”jÚŒerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÇŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”Œnameids”}”(j´j±j”j‘jçjäjBj?jwjtj¬j©uŒ nametypes”}”(j´‰j”‰jç‰jB‰jw‰j¬‰uh}”(j±hÊj‘jcjäj—j?jêjtjEj©jzuŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.