€•>BŒ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Œ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 <”…””}”(hjhžhhŸNh NubhŒ reference”“”)”}”(hŒxavier_qy@163.com”h]”hŒxavier_qy@163.com”…””}”(hj#hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”Œmailto:xavier_qy@163.com”uh1j!hjubhŒ>”…””}”(hjhžhhŸNh Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h K hjubah}”(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?”…””}”(hjRhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¹hjOhž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:”…””}”(hj`hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h KhjOhž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”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhŸh³h Khj{ubhŒ 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&]”uh1jhj{ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jyhŸh³h Khjvubjz)”}”(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&]”uh1jhŸ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&]”uh1jhj®ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jyhŸh³h Khjvubeh}”(h]”h ]”h"]”h$]”h&]”uh1jthjpubhê)”}”(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 Khjpubeh}”(h]”h ]”h"]”h$]”h&]”uh1jnhŸh³h KhjOhž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 KhjOhž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"hjOhž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.”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h K$hjOhž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:”…””}”(hj!hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h K+hjOhžhubjo)”}”(hŒZWikipedia entry on union-find https://en.wikipedia.org/wiki/Disjoint-set_data_structure ”h]”ju)”}”(hhh]”jz)”}”(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”…””}”(hj:hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhŸh³h K/hj6ubj)”}”(hhh]”hê)”}”(hŒ9https://en.wikipedia.org/wiki/Disjoint-set_data_structure”h]”j")”}”(hjMh]”hŒ9https://en.wikipedia.org/wiki/Disjoint-set_data_structure”…””}”(hjOhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”jMuh1j!hjKubah}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h K/hjHubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj6ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jyhŸh³h K/hj3ubah}”(h]”h ]”h"]”h$]”h&]”uh1jthj/ubah}”(h]”h ]”h"]”h$]”h&]”uh1jnhŸh³h K.hjOhž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žhubjo)”}”(hŒuf_node_init(&my_node); ”h]”hê)”}”(hŒuf_node_init(&my_node);”h]”hŒuf_node_init(&my_node);”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h KOhjubah}”(h]”h ]”h"]”h$]”h&]”uh1jnhŸ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”…””}”(hj4hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¹hj1hž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:”…””}”(hjBhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h KThj1hž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;”…””}”hjPsbah}”(h]”h ]”h"]”h$]”h&]”h±h²uh1j°hŸh³h KZhj1hž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”…””}”(hjihžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¹hjfhž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:”…””}”(hjwhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1héhŸh³h Kehjfhž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 Kjhjfhž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 jj€j}jÓjÐj.j+jcj`j˜j•uŒ nametypes”}”(j ‰j€‰jÓ‰j.‰jc‰j˜‰uh}”(jh¶j}jOjÐjƒj+jÖj`j1j•jfuŒ 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.