当前位置:网站首页>Summary of methods for finding intersection of ordered linked list sets
Summary of methods for finding intersection of ordered linked list sets
2022-07-05 16:30:00 【Software engineering Xiao Shi】
- double for Circulation , Time complexity O(n*n)
- Zipper method , Time complexity O(n)
- Divide the barrels horizontally , Multithreading parallel
- bitmap, Greatly improve the parallelism of operation , Time complexity O(n)
- Jump watch , The time complexity is O(log(n))
Scheme 1 :for * for, Earth way , Time complexity O(n*n)
Voice over : Stupid way .
Option two : Orderly list Find the intersection , Zipper method

Ordered set 1{1,3,5,7,8,9}
Ordered set 2{2,3,4,5,6,7}
Two pointers point to the first element , Compare the size of the elements :
(1) If the same , Put the result set , Move a pointer at will ;
(2) otherwise , Move a pointer with a smaller value , Until the end of the team ;
The advantage of this method is :
utilize “ Orderly ” This feature , The elements in the collection are compared at most once , The time complexity is O(n);
This method is like the gears on both sides of a zipper , One by one comparison is like a zipper , So it's called zipper method
Option three : Parallel optimization by buckets
give an example :
Ordered set 1{1,3,5,7,8,9, 10,30,50,70,80,90}
Ordered set 2{2,3,4,5,6,7, 20,30,40,50,60,70}
Find the intersection , Split the buckets first :
bucket 1 For the range of [1, 9]
bucket 2 For the range of [10, 100]
bucket 3 For the range of [101, max_int]
therefore :
aggregate 1 Split it into
aggregate a{1,3,5,7,8,9}
aggregate b{10,30,50,70,80,90}
aggregate c{}
aggregate 2 Split it into
aggregate d{2,3,4,5,6,7}
aggregate e{20,30,40,50,60,70}
aggregate e{}
The amount of data in each bucket is greatly reduced , And there is no repeating element in each bucket , You can use multithreaded parallel computing :
bucket 1 A collection within a And collection d The intersection of x{3,5,7}
bucket 2 A collection within b And collection e The intersection of y{30, 50, 70}
bucket 3 A collection within c And collection d The intersection of z{}
Final , aggregate 1 And collection 2 Intersection , yes x And y And z Union , I.e. set {3,5,7,30,50,70}.
Voice over : Multithreading 、 Horizontal segmentation is a common optimization method .
Option four :bitmap Optimize again
After the data is split horizontally , The data in each bucket must be in a range , If the set matches this characteristic , You can use bitmap To represent a set :

Pictured above , hypothesis set1{1,3,5,7,8,9} and set2{2,3,4,5,6,7} All of the elements in the bucket value [1, 16] Within the scope of , It can be used 16 individual bit To describe these two sets , The elements of the original collection x, In this 16bitmap No x individual bit by 1, Now two bitmap Find the intersection , Just put two bitmap Conduct “ And ” operation , Result set bitmap Of 3,5,7 Is it 1, Indicates that the intersection of the original set is {3,5,7}.
Divide the barrels horizontally ,bitmap After the optimization , Can greatly improve the efficiency of intersection , But time complexity is still O(n).bitmap It takes a lot of continuous space , Large memory footprint .
Voice over :bitmap Can represent a set , It's very fast to find the intersection of sets .
Option five : Jump watch skiplist
The intersection of the ordered list set , Jump table is the most commonly used data structure , It can get the complexity of intersection from O(n) It's close to O(log(n)).

aggregate 1{1,2,3,4,20,21,22,23,50,60,70}
aggregate 2{50,70}
Ask for intersection , If you use zipper method , Will find 1,2,3,4,20,21,22,23 All have to be traversed invalid once , Every element has to be compared , The time complexity is O(n), Can you compare each time “ Skip some elements ” Well ?
The jump watch appears :

aggregate 1{1,2,3,4,20,21,22,23,50,60,70} When creating a jump table , There is only one level {1,20,50} Three elements , The second level is the same as the common list .
aggregate 2{50,70} Because there are fewer elements , Only one level of common linked list has been established .
such , In implementation “ zipper ” In the process of finding intersection ,set1 The pointer of can be controlled by 1 Jump to the 20 Jump to 50, Can skip many elements in the middle , There is no need to compare one by one , The time complexity of finding intersection is approximate O(log(n)).
come from :https://mp.weixin.qq.com/s/6qU7yWKhMZUiyu7TlcuiSA
边栏推荐
- Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
- PSPNet | 语义分割及场景分析
- 10 minutes to help you get ZABBIX monitoring platform alarm pushed to nail group
- 移动办公时如何使用frp内网穿透+teamviewer方式快速连入家中内网主机
- Query the latest record in SQL
- 2020-2022 two-year anniversary of creation
- 【深度学习】深度学习如何影响运筹学?
- 对象和类的关系
- Exception com alibaba. fastjson. JSONException: not match : - =
- The memory of a Zhang
猜你喜欢

Domestic API management artifact used by the company

降本40%!Redis多租户集群的容器化实践

Seaborn绘制11个柱状图

PSPNet | 语义分割及场景分析
![[Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video](/img/69/3aedcdafb2b4e83087dc1ce593dc38.png)
[Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video

《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md

Win11提示无法安全下载软件怎么办?Win11无法安全下载软件

服务器的数据库连不上了2003,10060“Unknown error“【服务已起、防火墙已关、端口已开、netlent 端口不通】

项目中批量update

极坐标扇图使用场景与功能详解
随机推荐
Use of RLOCK lock
Use of set tag in SQL
vulnhub-FirstBlood
Apiccloud cloud debugging solution
The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough
Query the latest record in SQL
Starkware: to build ZK "universe"
Relationship between objects and classes
obj集合转为实体集合
程序员如何提升自己的格局?
普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
[graduation season] as a sophomore majoring in planning, I have something to say
"21 days proficient in typescript-3" - install and build a typescript development environment md
新春限定丨“牛年忘烦”礼包等你来领~
Some cognitive thinking
2020-2022两周年创作纪念日
今日睡眠质量记录79分
中间表是如何被消灭的?
《MongoDB入门教程》第04篇 MongoDB客户端
Subclasses and superclasses of abstract classes