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
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
2026年 エンジニアリング自己学習法
yumechi
0
130
Patterns of Patterns
denyspoltorak
0
1.4k
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
200
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
260
Data-Centric Kaggle
isax1015
2
770
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
190
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
400
Fluid Templating in TYPO3 14
s2b
0
130
AtCoder Conference 2025
shindannin
0
1.1k
AI & Enginnering
codelynx
0
110
Featured
See All Featured
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
0
3.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
[SF Ruby Conf 2025] Rails X
palkan
1
740
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
150
Raft: Consensus for Rubyists
vanstee
141
7.3k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Skip the Path - Find Your Career Trail
mkilby
0
53
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Between Models and Reality
mayunak
1
180
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
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で一致するので同一グループだと分かる 根 根