当前位置:网站首页>Explanation of parallel search set theory and code implementation
Explanation of parallel search set theory and code implementation
2022-07-05 07:24:00 【BugMaker-shen】
List of articles
One 、 Concept
And lookups are tree like data structures , It is mainly used to solve some problems The problem of element grouping . Used to deal with some Disjoint Set Of Merge as well as Inquire about problem
The idea of joint search set is to represent the whole forest with an array , The root node of the tree uniquely identifies a collection , As long as we find the root of an element , You can determine which set it is in
Two 、 And look up the construction process
Let's first explain the construction process of the search set
Father Son
1 3
1 2
5 4
2 4
6 8
8 7
Let's assume that the above nodes are in a set , On the left is the parent node , On the right is the child node
For the first four groups of data , We construct the following structure

There is a problem , According to the first 4 When constructing a set with group data , Should put the 4 The root node of the tree is the same as 2 The root node of the tree is connected , The correct set structure is as follows :

In the process of constructing and searching sets , It's not simply connecting two nodes , Instead, connect the root node of the tree where the node is located
Determine whether two nodes are in a set , Judge whether the root node of the tree where these two nodes are located is the same
We then construct a set of the last two sets of data 
The above kind of direct handle 7 Put it in 8 The following is wrong , We just said , Merging two sets is to merge the root nodes of two trees .7 Become the root node of a tree alone , and 6 A merger , The correct combination is as follows :

Through the above construction process , Here is a summary :
- In the process of constructing and searching sets , It's not simply connecting two nodes , Instead, connect the root node of the tree where the node is located ( Don't care about the parent-child relationship of the root node )
- Determine whether two nodes are in a set , Judge whether the root node of the tree where these two nodes are located is the same
3、 ... and 、 Code implementation
When implemented on code , Whether merging or querying , We need to find the root node of the tree where the node is located , Therefore, you need to record the parent node
The main idea : The position of array elements corresponding to each node , Store the number of its parent node
How to initialize ?
Because we say that the root node of the tree where each independent node is located is itself , So we initialize with its own node number
When did you find the roots ?
When x = arr[x] When equal , Indicates that its parent node is itself , It means that the root of the tree is itself
The initialization of parallel search set is as follows :

The forest we constructed is represented by union search set as follows

Traversal array , By comparing whether the element value and subscript are the same, we can get and check how many tree roots there are in the set . You can also see from the picture , node 1 and 6 It's the root
#include <iostream>
using namespace std;
const int SIZE = 9;
int parent[SIZE];
// return x The number of the root node of the tree
int find_root(int x) {
if (x <= 0 || x >= SIZE) {
return -1;
}
while (x != parent[x]) {
x = parent[x];
}
return x;
}
// Recursive version of the query
int cur_find_root(int x) {
if (x <= 0 || x >= SIZE) {
return -1;
}
if (x == parent[x]) {
return x;
}
else {
return cur_find_root(parent[x]);
}
}
// Merge x and y Where the collection is
void merge(int x, int y) {
x = find_root(x);
y = find_root(y);
if (x != y) {
// x and y In a set, there is no need to merge , If you are not in a set, you need to merge
parent[y] = x;
// parent[x] = y;
}
}
int main() {
for (int i = 0; i < SIZE; i++) {
parent[i] = i;
}
int x, y;
for (int i = 0; i < 6; i++) {
cin >> x >> y;
merge(x, y);
}
cout<<(find_root(2) == find_root(8) ? "YES" : "NO")<<endl;
return 0;
}
边栏推荐
- I can't stand the common annotations of idea anymore
- Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换
- 借助 Navicat for MySQL 软件 把 不同或者相同数据库链接中的某数据库表数据 复制到 另一个数据库表中
- Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
- Altimeter data knowledge point 2
- Mipi interface, DVP interface and CSI interface of camera
- [vscode] prohibit the pylance plug-in from automatically adding import
- PowerManagerService(一)— 初始化
- [software testing] 02 -- software defect management
- Database SQL practice 4. Find the last of employees in all assigned departments_ Name and first_ name
猜你喜欢
![When jupyter notebook is encountered, erroe appears in the name and is not output after running, but an empty line of code is added downward, and [] is empty](/img/fe/fb6df31c78551d8908ba7964c16180.jpg)
When jupyter notebook is encountered, erroe appears in the name and is not output after running, but an empty line of code is added downward, and [] is empty

行测--资料分析--fb--高照老师

The mutual realization of C L stack and queue in I

Word import literature -mendeley
![[software testing] 03 -- overview of software testing](/img/1e/0b6458160e34e43f021ea4797de70a.jpg)
[software testing] 03 -- overview of software testing

Daily Practice:Codeforces Round #794 (Div. 2)(A~D)

SD_ CMD_ SEND_ SHIFT_ REGISTER

CADD课程学习(5)-- 构建靶点已知的化合结构(ChemDraw)

Light up the running light, rough notes for beginners (1)

Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
随机推荐
GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
2022.06.27_每日一题
golang定时器使用踩的坑:定时器每天执行一次
玩转gRPC—深入概念与原理
SD_ CMD_ SEND_ SHIFT_ REGISTER
Using GEE plug-in in QGIS
[OBS] x264 Code: "buffer_size“
纯碱是做什么的?
Word import literature -mendeley
Raspberry pie 4B arm platform aarch64 PIP installation pytorch
Simple operation of running water lamp (keil5)
Target detection series - detailed explanation of the principle of fast r-cnn
C learning notes
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
公安专业知识--哔哩桐老师
Simple operation with independent keys (hey, a little fancy) (keil5)
[vscode] search using regular expressions
Eclipse project recompile, clear cache
Hdu1231 maximum continuous subsequence (divide and conquer or dynamic gauge or double pointer)
R language learning notes 1