Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
UnionFindとは
Search
machio
September 09, 2025
Programming
0
1.5k
UnionFindとは
machio
September 09, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
RubyとGoでゼロから作る証券システム: 高信頼性が求められるシステムのコードの外側にある設計と運用のリアル
free_world21
0
290
Unity6.3 AudioUpdate
cova8bitdots
0
130
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
DevinとClaude Code、SREの現場で使い倒してみた件
karia
1
1.1k
Understanding Apache Lucene - More than just full-text search
spinscale
0
120
クライアントワークでSREをするということ。あるいは事業会社におけるSREと同じこと・違うこと
nnaka2992
1
340
野球解説AI Agentを開発してみた - 2026/02/27 LayerX社内LT会資料
shinyorke
PRO
0
300
S3ストレージクラスの「見える」「ある」「使える」は全部違う ─ 体験から見た、仕様の深淵を覗く
ya_ma23
0
530
AWS×クラウドネイティブソフトウェア設計 / AWS x Cloud-Native Software Design
nrslib
16
3.2k
TipKitTips
ktcryomm
0
170
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
590
AI時代のシステム設計:ドメインモデルで変更しやすさを守る設計戦略
masuda220
PRO
5
1k
Featured
See All Featured
Site-Speed That Sticks
csswizardry
13
1.1k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
290
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
990
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
220
New Earth Scene 8
popppiees
1
1.7k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
86
Skip the Path - Find Your Career Trail
mkilby
1
79
Designing Experiences People Love
moore
143
24k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
150
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
640
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Writing Fast Ruby
sferik
630
63k
Transcript
P1 ©Techouse All Rights Reserved UnionFind(素集合データ構造 )とは
P2 ©Techouse All Rights Reserved UnionFindとは グループ分けを効率的に管理するデータ構造
P3 ©Techouse All Rights Reserved UnionFindを用いると 例えば、フットサルのチーム分けなどを効率的に行える
P4 ©Techouse All Rights Reserved UnionFindが提供する操作は次の 2種類だけ 1. 「まとめる」
2. 「判定」
P5 ©Techouse All Rights Reserved • n個の要素があるとする • 最初、n個の要素は全て別々のグループ 1
2 3 4 5
P6 ©Techouse All Rights Reserved 操作1. 「まとめる」
P7 ©Techouse All Rights Reserved 2つのグループを 1つにまとめる 1 2
3 4 5
P8 ©Techouse All Rights Reserved 1 2 3 4 5
2つのグループを 1つにまとめる
P9 ©Techouse All Rights Reserved 1 2 3 4 5
1 2 2つのグループを 1つにまとめる
P10 ©Techouse All Rights Reserved 1 2 3 4 5
1 2 2つのグループを 1つにまとめる
P11 ©Techouse All Rights Reserved 1 2 3 4 5
1 2 3 4 5 2つのグループを 1つにまとめる
P12 ©Techouse All Rights Reserved 操作2. 「判定」
P13 ©Techouse All Rights Reserved 1 3 2 4 6
5 1と3 → true 1と2 → false 2つの要素が同じグループに属しているかを判定する
P14 ©Techouse All Rights Reserved おさらいすると
P15 ©Techouse All Rights Reserved UnionFindは 「まとめる」「判定」 2種類の操作のみで グループ分けを行う
P16 ©Techouse All Rights Reserved 皆さんは「まとめる」「判定」の 2種類の操作で グループ分けを効率的に行えることを 直感的に理解できるはず
P17 ©Techouse All Rights Reserved というのも これまで何度も UnionFindと 同じことをやってきている
P18 ©Techouse All Rights Reserved 例えば、フットサルのチーム分け
P19 ©Techouse All Rights Reserved チーム分けを抽選やグーチョキパー で決めたら
P20 ©Techouse All Rights Reserved 手当たり次第、 「あなたはどのチームですか?」 と周りの人に質問しますよね
P21 ©Techouse All Rights Reserved これが、UnionFindの「判定」です
P22 ©Techouse All Rights Reserved 「判定」の結果、 同じチームであることが分かったら その人と一緒に固まりますよね?
P23 ©Techouse All Rights Reserved これが、「まとめる」です
P24 ©Techouse All Rights Reserved これで小グループができていきます
P25 ©Techouse All Rights Reserved 小グループの誰かが、別グループの誰かと話して (=判定) 同じグループであることが分かった場合、 二つのグループでまとまって より大きな
1つのグループとして固まりますよね
P26 ©Techouse All Rights Reserved UnionFindの仕組みは、これと同じです
P27 ©Techouse All Rights Reserved では、具体的にどう実現するか
P28 ©Techouse All Rights Reserved グループを木構造で扱います
P29 ©Techouse All Rights Reserved 木構造とは
P30 ©Techouse All Rights Reserved 根 root 節 node 節
node 葉 reaf 葉 reaf 葉 reaf 葉 reaf • 階層を持ったデータ構造 • 1つの親から複数の子がぶら下 がるような構造
P31 ©Techouse All Rights Reserved 1 3 2 4 6
5 7
P32 ©Techouse All Rights Reserved 2 4 6 5 1
3 7
P33 ©Techouse All Rights Reserved 2 4 6 5 5
1 3 2 4 6 1 3 7 7
P34 ©Techouse All Rights Reserved 2 4 6 5 5
1 3 2 4 6 各ノード(要素)は親を1つ持つ 1 3 7 7
P35 ©Techouse All Rights Reserved 1 3 2 4 6
5 5 1 3 2 4 6 各ノード(要素)は親を1つ持つ 3の親 4と6の親 5自身の親 7 7の親
P36 ©Techouse All Rights Reserved 「まとめる」操作をどう実現するか
P37 ©Techouse All Rights Reserved 「まとめる」操作をどう実現するか 片方の根からもう片方の根に 辺(リンク)を張れば良い
P38 ©Techouse All Rights Reserved 1 2 1 3 6
2 4
P39 ©Techouse All Rights Reserved 1 2 1 3 6
2 4
P40 ©Techouse All Rights Reserved 1 2 1 1 3
6 2 4
P41 ©Techouse All Rights Reserved 1 2 1 2 1
3 6 2 4
P42 ©Techouse All Rights Reserved 1 2 1 3 6
2 4 1 2
P43 ©Techouse All Rights Reserved 1 2 1 3 6
2 4 1 2 6 2 4
P44 ©Techouse All Rights Reserved 1 2 1 3 6
2 4 1 2 6 2 4 1 3
P45 ©Techouse All Rights Reserved 「判定」操作をどう実現するか
P46 ©Techouse All Rights Reserved 「判定」操作をどう実現するか 2つの要素の親をたどり、 根が同じかどうかを見れば良い
P47 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する
P48 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の親を辿っていく
P49 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の親を辿っていく 親
P50 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の親を辿っていく 親 親
P51 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の親を辿っていく 根 親 親
P52 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の根 => 1 根 親 親
P53 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の根 => 1 7の親を辿っていく 根
P54 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の根 => 1 7の親を辿っていく 根 親
P55 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の根 => 1 7の親を辿っていく 根 親 親
P56 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の根 => 1 7の親を辿っていく 根 親 親 根
P57 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7が同じグループか「判定」する 5の根 => 1 7の根 => 2 根 親 親 根
P58 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7は所属する木 (グループ)の 根(代表元)が一致しない 根 根
P59 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 5と7は所属する木 (グループ)の 根(代表元)が一致しない つまり、別グループである 根 根
P60 ©Techouse All Rights Reserved 1 3 6 2 4
5 7 逆に、例えば 4と7は根が2で一致するので同一グループだと分かる 根 根