$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
UnionFindとは
Search
machio
September 09, 2025
Programming
0
1.4k
UnionFindとは
machio
September 09, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
150
ViewファーストなRailsアプリ開発のたのしさ
sugiwe
0
400
開発に寄りそう自動テストの実現
goyoki
1
450
ZOZOにおけるAI活用の現在 ~モバイルアプリ開発でのAI活用状況と事例~
zozotech
PRO
8
4.1k
AIコーディングエージェント(Gemini)
kondai24
0
160
Reactive Thinking with Signals and the new Resource API
manfredsteyer
PRO
0
160
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
120
AIエージェントを活かすPM術 AI駆動開発の現場から
gyuta
0
230
Developing static sites with Ruby
okuramasafumi
0
160
WebRTC、 綺麗に見るか滑らかに見るか
sublimer
1
150
認証・認可の基本を学ぼう前編
kouyuume
0
160
無秩序からの脱却 / Emergence from chaos
nrslib
2
12k
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Side Projects
sachag
455
43k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
Building Applications with DynamoDB
mza
96
6.8k
The Pragmatic Product Professional
lauravandoore
37
7.1k
Typedesign – Prime Four
hannesfritz
42
2.9k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.8k
Facilitating Awesome Meetings
lara
57
6.7k
We Have a Design System, Now What?
morganepeng
54
7.9k
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で一致するので同一グループだと分かる 根 根