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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
machio
September 09, 2025
Programming
0
1.5k
UnionFindとは
machio
September 09, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
組織で育むオブザーバビリティ
ryota_hnk
0
170
今から始めるClaude Code超入門
448jp
8
8.5k
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
0
930
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
370
AI巻き込み型コードレビューのススメ
nealle
0
130
AI時代の認知負荷との向き合い方
optfit
0
150
ThorVG Viewer In VS Code
nors
0
770
Architectural Extensions
denyspoltorak
0
280
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
250
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
510
CSC307 Lecture 02
javiergs
PRO
1
770
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
380
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
240
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
72
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2k
BBQ
matthewcrist
89
10k
Unsuck your backbone
ammeep
671
58k
Speed Design
sergeychernyshev
33
1.5k
How GitHub (no longer) Works
holman
316
140k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
98
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
92
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で一致するので同一グループだと分かる 根 根