当前位置:网站首页>Joint search set structure
Joint search set structure
2022-07-24 22:13:00 【Harrison who likes to knock code】
Union checking set
The time complexity of adding, deleting, modifying and checking is O(1) At present, we have learned the structure of hash table and parallel query set
What is juxtaposition ?
1) There are several samples a、b、c、d… The type assumption is V
2) In the parallel search set, it was initially thought that each sample was in a separate set
3) The user can call the following two methods at any time :
- boolean isSameSet(V x, V y): Query sample x And the sample y Whether it belongs to a collection
- void union(V x, V y) : hold x and y All samples in their respective sets are combined into one set
4)isSameSet and union The lower the cost of the method, the better , It is best to O(1)
Add
1) Each node has a pointer pointing up
2) node a Find the header node up , be called a The representative node of the set
3) Inquire about x and y Whether they belong to the same set , Is to see if the representative node found is a
4) hold x and y All points of the respective set are merged into a set , Just need a small set of representative points to hang
Just below the representative points of the large set
And search set optimization
1) The process of nodes looking for representative points up , Turn the chain along the way into a flat
2) The small collection hangs under the large collection
3) If method calls are frequent , Then the cost of a single call is O(1), Both ways
And look up the application of the set
- Solve the problem of merging two large areas
- It is often used in fields such as graphs
Core code && Detailed notes
package com.harrison.class10;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
public class Code01_UnionFind {
// Customize V type , Each sample value Wrap a layer outside
public static class Node<V>{
V value;
public Node(V v) {
value=v;
}
}
public static class UnionSet<V>{
// stay nodes Inside the watch , Any sample value Each one corresponds to a node
// And after initialization , There will never be any change , Just write down the correspondence
public HashMap<V, Node<V>> nodes;
// Each node corresponds to its parent node one by one , Recorded in the parents Inside the watch
public HashMap<Node<V>, Node<V>> parents;
// There is only one point , And this point is the representative point of the set
public HashMap<Node<V>, Integer> sizeMap;
public UnionSet(List<V> values) {
// The user gives all samples at one time
for(V cur:values) {
// Take each sample value All wrapped in a layer to form node
Node<V> node=new Node<>(cur);
// Write down the correspondence , Never change after
nodes.put(cur, node);
// Because in the beginning, each sample only had its own , So the parent node is still itself
parents.put(node, node);
// Because in the beginning, every point is a representative point , So the set size is 1
sizeMap.put(node, 1);
}
}
// From the point of cur Start , Keep looking up , Find a representative point that can't go up , return
// All nodes along the way are put into a container ( Containers can not be stacks ), The purpose is to write down the path
public Node<V> findFather(Node<V> cur){
Stack<Node<V>> path=new Stack<>();
while(cur!=parents.get(cur)) {
path.push(cur);
cur=parents.get(cur);
}
// After jumping out of the above loop ,cur It must point to the representative point
// Before returning to the representative point , Change the parent nodes of all nodes along the way to representative points
// Important optimization is reflected in this , Flatten the chain
while(!path.isEmpty()) {
parents.put(path.pop(), cur);
}
return cur;
}
public boolean isSameSet(V a,V b) {
// If either of the two samples is not in nodes Table initialization ,
// It must not be in the same set
if(!nodes.containsKey(a) || !nodes.containsKey(b)) {
return false;
}
// if Missed , It means that both samples are nodes It's registered in the form
// Find the representative point of the set through the sample , If the memory addresses of two representative points are the same
// It means that it is in the same set
return findFather(nodes.get(a))==findFather(nodes.get(b));
}
// Be careful : yes a The entire set of samples and b The entire set of samples A merger
public void union(V a,V b) {
// Not registered , Can't merge
if(!nodes.containsKey(a) || !nodes.containsKey(b)) {
return ;
}
// If registered , Find the representative point of your set
Node<V> aHead=findFather(nodes.get(a));
Node<V> bHead=findFather(nodes.get(b));
// Only two representative points with different memory addresses need to be merged
// otherwise , It shows that the two sets represented by these two representative points are already merged
if(aHead!=bHead) {
// Find the size of two sets respectively
int aSetSize=sizeMap.get(aHead);
int bSetSize=sizeMap.get(bHead);
Node<V> big=aSetSize>=bSetSize?aHead:bHead;
Node<V> small=big==aHead?bHead:aHead;
// The small collection hangs under the large collection
// Directly change the parent node of the head node of a small set into the head node of a large set
parents.put(small, big);
// So the size of a large set becomes the sum of the sizes of two sets
sizeMap.put(big, aSetSize+bSetSize);
// The small set head node is no longer used as a representative point , So delete in sizeMap The records in
sizeMap.remove(small);
}
}
}
}
边栏推荐
- 窗口内最大值或最小值的更新结构——窗口内最大值
- 【考研词汇训练营】Day 12 —— native,separate,figure,contribute,species,assumption,suppose
- Uniform sampling and thinning of PCL point cloud processing (61)
- PCL点云处理之创建二维格网组织点云数据(六十四)
- day10:声明式事务控制
- [icml2022] climate change and machine learning: opportunities, challenges and considerations, 121 ppt
- VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题
- In the eyes of professionals in the robot industry, what is the current situation of the robot industry?
- What are the methods of knowledge map relation extraction
- 【数据库学习】Redis 解析器&&单线程&&模型
猜你喜欢

运动控制卡应用开发教程之调用激光振镜控制

Feeding Program Source Code to ZK VMs

工程项目管理软件排名

Gradle learning set integration

Sensor experiment - 485 air temperature and humidity

Gradle learning - gradle advanced instructions

C# 使用SQLite

模板的使用

Local data enhancement method of icml2022 | graph neural network

LED digital display driver IC and anti-interference LED digital tube display driver ic-vk1s68c ssop24 are applicable to finger clip pulse oximeter, arm electronic sphygmomanometer, thermometer, fetal
随机推荐
Uniform sampling and thinning of PCL point cloud processing (61)
General syntax and classification of SQL language (II)
集成Swagger 学习
Tencent +360+ Sogou school recruitment test + summary of knowledge points
Push information to wechat through enterprise wechat self built application
Database - Metadata databasemetadata beginner
在机器人行业的专业人士眼里,机器人行业目前的情况如何?
PCL point cloud processing: creating a two-dimensional grid to organize point cloud data (64)
ESP32C3 LED PWM使用和ESP32差异说明
Redefine analysis - release of eventbridge real-time event analysis platform
Win10 解base64
【考研词汇训练营】Day 12 —— native,separate,figure,contribute,species,assumption,suppose
My love lesson 2 - remove webpage pop-up
[postgraduate entrance examination English vocabulary training camp] day 11 - offer, form, maintain, critical
一种兼容、更小、易用的WEB字体API
PCL点云处理之CSF布料模拟滤波(五十九)
由斐波那契数列引述到矩阵快速幂技巧
Use of templates
Ue5 reports an error using the plug-in quixel Bridge
图结构的实现,从点到边再到图