当前位置:网站首页>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
边栏推荐
- 异常com.alibaba.fastjson.JSONException: not match : - =
- Five common negotiation strategies of consulting companies and how to safeguard their own interests
- 开发中Boolean类型使用遇到的坑
- 《MongoDB入门教程》第04篇 MongoDB客户端
- list去重并统计个数
- Cheer yourself up
- Is it safe for Guotai Junan to open an account online
- ES6深入—ES6 Class 类
- 事务回滚异常
- Cs231n notes (medium) -- applicable to 0 Foundation
猜你喜欢

今日睡眠质量记录79分

ES6深入—ES6 Class 类

Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)

【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……

Data Lake (XIV): spark and iceberg integrated query operation

Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022

Seaborn draws 11 histograms

Subclasses and superclasses of abstract classes

效果编辑器新版上线!3D渲染、加标注、设置动画,这次一个编辑器就够了

服务器的数据库连不上了2003,10060“Unknown error“【服务已起、防火墙已关、端口已开、netlent 端口不通】
随机推荐
This article takes you through the addition, deletion, modification and query of JS processing tree structure data
scratch五彩糖葫芦 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Cheer yourself up
APICloud云调试解决方案
OneForAll安装使用
Solve the Hanoi Tower problem [modified version]
Obj resolves to a set
Domestic API management artifact used by the company
面对新的挑战,成为更好的自己--进击的技术er
程序员如何提升自己的格局?
[es6] 模板字符串内添加if判断或添加三元运算符判断
Quelques réflexions cognitives
【毕业季】作为一名大二计科在校生,我有话想说
10分钟帮你搞定Zabbix监控平台告警推送到钉钉群
ES6 drill down - Async functions and symbol types
list使用Stream流进行根据元素某属性数量相加
Five common negotiation strategies of consulting companies and how to safeguard their own interests
CISP-PTE之PHP伪协议总结
怎样在电脑上设置路由器的WiFi密码
"21 days proficient in typescript-3" - install and build a typescript development environment md