diff --git a/Documentation/core-api/index.rst b/Documentation/core-api/index.rst index f147854700e4429568897317fea2f3bffb597a79..e18a2ffe07877d7a609a17531aa19466ea03f26f 100644 --- a/Documentation/core-api/index.rst +++ b/Documentation/core-api/index.rst @@ -49,6 +49,7 @@ Library functionality that is used throughout the kernel. wrappers/atomic_t wrappers/atomic_bitops floating-point + union_find Low level entry and exit ======================== diff --git a/Documentation/core-api/union_find.rst b/Documentation/core-api/union_find.rst index 2bf0290c91840ee4974b6168f98491e859446c41..6df8b94fdb5a0bc23ff5a367e1b53e8ab0eabf77 100644 --- a/Documentation/core-api/union_find.rst +++ b/Documentation/core-api/union_find.rst @@ -16,9 +16,11 @@ of disjoint sets. The primary operations supported by union-find are: Initialization: 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. As a data structure used to maintain sets (groups), union-find is commonly @@ -63,7 +65,7 @@ operation, the tree with the smaller rank is attached under the tree with the larger rank to maintain balance. Initializing union-find --------------------- +----------------------- You can complete the initialization using either static or initialization interface. Initialize the parent pointer to point to itself and set the rank @@ -71,7 +73,9 @@ to 0. Example:: struct uf_node my_node = UF_INIT_NODE(my_node); + or + uf_node_init(&my_node); Find the Root Node of union-find diff --git a/Documentation/translations/zh_CN/core-api/index.rst b/Documentation/translations/zh_CN/core-api/index.rst index 922cabf7b5dd5a31915941622a94d89dcada37be..453a02cd1f4486364d928068bdff64310811736d 100644 --- a/Documentation/translations/zh_CN/core-api/index.rst +++ b/Documentation/translations/zh_CN/core-api/index.rst @@ -49,6 +49,7 @@ generic-radix-tree packing this_cpu_ops + union_find ======= diff --git a/Documentation/translations/zh_CN/core-api/union_find.rst b/Documentation/translations/zh_CN/core-api/union_find.rst index a56de57147e932d7204edf96643932c0aff56147..bb93fa8c6533251d0ec0f797a2770aa7ed98c848 100644 --- a/Documentation/translations/zh_CN/core-api/union_find.rst +++ b/Documentation/translations/zh_CN/core-api/union_find.rst @@ -3,21 +3,23 @@ :Original: Documentation/core-api/union_find.rst -=========================== +============================= Linuxä¸çš„并查集(Union-Find) -=========================== +============================= :日期: 2024å¹´6月21æ—¥ :作者: Xavier <xavier_qy@163.com> 何为并查集,它有什么用? ---------------------- +------------------------ å¹¶æŸ¥é›†æ˜¯ä¸€ç§æ•°æ®ç»“构,用于处ç†ä¸€äº›ä¸äº¤é›†çš„åˆå¹¶åŠæŸ¥è¯¢é—®é¢˜ã€‚并查集支æŒçš„ä¸»è¦æ“作: - åˆå§‹åŒ–:将æ¯ä¸ªå…ƒç´ åˆå§‹åŒ–为å•独的集åˆï¼Œæ¯ä¸ªé›†åˆçš„åˆå§‹çˆ¶èŠ‚ç‚¹æŒ‡å‘自身 + åˆå§‹åŒ–:将æ¯ä¸ªå…ƒç´ åˆå§‹åŒ–为å•独的集åˆï¼Œæ¯ä¸ªé›†åˆçš„åˆå§‹çˆ¶èŠ‚ç‚¹æŒ‡å‘自身。 + 查询:查询æŸä¸ªå…ƒç´ 属于哪个集åˆï¼Œé€šå¸¸æ˜¯è¿”回集åˆä¸çš„ä¸€ä¸ªâ€œä»£è¡¨å…ƒç´ â€ã€‚这个æ“作是为 了判æ–ä¸¤ä¸ªå…ƒç´ æ˜¯å¦åœ¨åŒä¸€ä¸ªé›†åˆä¹‹ä¸ã€‚ + åˆå¹¶ï¼šå°†ä¸¤ä¸ªé›†åˆåˆå¹¶ä¸ºä¸€ä¸ªã€‚ 并查集作为一ç§ç”¨äºŽç»´æŠ¤é›†åˆï¼ˆç»„)的数æ®ç»“构,它通常用于解决一些离线查询ã€åЍæ€è¿žé€šæ€§å’Œ @@ -37,7 +39,7 @@ Linuxä¸çš„并查集(Union-Find) https://en.wikipedia.org/wiki/Disjoint-set_data_structure 并查集的Linux实现 ----------------- +------------------ Linux的并查集实现在文件“lib/union_find.câ€ä¸ã€‚è¦ä½¿ç”¨å®ƒï¼Œéœ€è¦ “#include <linux/union_find.h>â€ã€‚ @@ -48,22 +50,25 @@ Linux的并查集实现在文件“lib/union_find.câ€ä¸ã€‚è¦ä½¿ç”¨å®ƒï¼Œéœ€ struct uf_node *parent; unsigned int rank; }; + å…¶ä¸parent为当å‰èŠ‚ç‚¹çš„çˆ¶èŠ‚ç‚¹ï¼Œrankä¸ºå½“å‰æ ‘的高度,在åˆå¹¶æ—¶å°†rankå°çš„节点接到rank大 的节点下é¢ä»¥å¢žåŠ å¹³è¡¡æ€§ã€‚ åˆå§‹åŒ–并查集 ---------- +------------- å¯ä»¥é‡‡ç”¨é™æ€æˆ–åˆå§‹åŒ–接å£å®Œæˆåˆå§‹åŒ–æ“作。åˆå§‹åŒ–时,parent 指针指å‘自身,rank 设置 为 0。 示例:: struct uf_node my_node = UF_INIT_NODE(my_node); + 或 + uf_node_init(&my_node); æŸ¥æ‰¾å¹¶æŸ¥é›†çš„æ ¹èŠ‚ç‚¹ ----------------- +------------------ 主è¦ç”¨äºŽåˆ¤æ–两个并查集是å¦å±žäºŽä¸€ä¸ªé›†åˆï¼Œå¦‚æžœæ ¹ç›¸åŒï¼Œé‚£ä¹ˆä»–们就是一个集åˆã€‚åœ¨æŸ¥æ‰¾è¿‡ç¨‹ä¸ ä¼šå¯¹è·¯å¾„è¿›è¡ŒåŽ‹ç¼©ï¼Œæé«˜åŽç»æŸ¥æ‰¾æ•ˆçŽ‡ã€‚ @@ -78,7 +83,7 @@ Linux的并查集实现在文件“lib/union_find.câ€ä¸ã€‚è¦ä½¿ç”¨å®ƒï¼Œéœ€ connected = 0; åˆå¹¶ä¸¤ä¸ªå¹¶æŸ¥é›† -------------- +-------------- 对于两个相交的并查集进行åˆå¹¶ï¼Œä¼šé¦–先查找它们å„è‡ªçš„æ ¹èŠ‚ç‚¹ï¼Œç„¶åŽæ ¹æ®æ ¹èŠ‚ç‚¹ç§©å¤§å°ï¼Œå°†å°çš„ 节点连接到大的节点下é¢ã€‚