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
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
110
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
420
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
「ブロックテーマでは再現できない」は本当か?
inc2734
0
770
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
150
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
6
1.8k
CSC307 Lecture 09
javiergs
PRO
1
830
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
200
OSSとなったswift-buildで Xcodeのビルドを差し替えられるため 自分でXcodeを直せる時代になっている ダイアモンド問題編
yimajo
3
610
CSC307 Lecture 04
javiergs
PRO
0
660
CSC307 Lecture 02
javiergs
PRO
1
770
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
180
Featured
See All Featured
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
0
1.1k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
110
Practical Orchestrator
shlominoach
191
11k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
210
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Optimizing for Happiness
mojombo
379
71k
How to build a perfect <img>
jonoalderson
1
4.9k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
350
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
64
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で一致するので同一グループだと分かる 根 根