function Find(x)
root := x
while root.parent != root
root := root.parent
while x.parent != root
parent := x.parent
x.parent := root
x := parent
return root
経路分割と経路半減
Tarjan と van Leeuwen は、最悪計算量を保ったまま、根に向かう1回の走査だけで済み実用上より効率的な Find のアルゴリズムも考案している[2]。これは経路分割(path splitting)と経路半減(path halving)と呼ばれ、どちらも経路上のノードの親を根に近いノードに付け替える。
経路分割は、経路上のすべてのノードの親を、そのノードの祖父(親の親)に付け替える。
function Find(x)
while x.parent != x
(x, x.parent) := (x.parent, x.parent.parent)
return x
経路半減は、経路上の1つおきのノードだけの親を祖父に付け替える。
function Find(x)
while x.parent != x
x.parent := x.parent.parent
x := x.parent
return x
経路分割・経路半減は、再帰や経路の記憶を必要とせず、ループ1回で書けるため実装が簡潔である。
Union
MakeSet により8個のシングルトンを作った状態。
いくつかの Union を行った後の状態。いくつかの集合が1つにまとまっている。
Union(x, y) は、まず Find で x と y それぞれの根を求める。根が同じなら何もしない。異なれば、一方の根の親をもう一方の根にして2つの木を連結する。どちらを親にするかは以降の操作の計算量に影響し、無頓着に選ぶと木が高くなりすぎる。木の高さを抑えるため、効率的な実装では union by size か union by rank を用いる。どちらも、根に親ポインタ以外の情報を持たせて、どちらの根を新しい親にするかを決める。
サイズによる併合
union by size(サイズによる併合)では、根が部分木の要素数(自分自身を含む子孫の数)を持ち、Union で要素数が大きい方の木の根を全体の根にする。要素数が等しければどちらでもよい。併合後の根のサイズは、2つのサイズの和に更新する。
function Union(x, y)
xRoot := Find(x)
yRoot := Find(y)
1991年には、Galil と Italiano が素集合データ構造に関する調査論文を発表した[13]。同じ1991年には、Richard J. Anderson と Heather Woll が、ブロックすることなく並列化した Union-Find(wait-free な並列アルゴリズム)を発表している[18]。
2007年、ACM SIGPLAN の Workshop on ML において、Sylvain Conchon と Jean-Christophe Filliâtre が素集合森データ構造の永続版を発表した。これは構造の以前のバージョンを効率的に保持するもので、計算機支援証明システムCoqを使って形式的に正しさが証明されている[15]。最も高速な実装は、非永続のアルゴリズムとほぼ同等の性能を達成しているが、計算量の解析は行われていない。
123456789Tarjan,Robert Endre(1975).“Efficiency of a Good But Not Linear Set Union Algorithm”.Journal of the ACM22(2): 215–225.doi:10.1145/321879.321884.
1234Tarjan,Robert E.;van Leeuwen,Jan(1984).“Worst-case analysis of set union algorithms”.Journal of the ACM31(2): 245–281.doi:10.1145/62.2160.
123M. Fredman and M. Saks. The cell probe complexity of dynamic data structures. Proceedings of the Twenty-First Annual ACM Symposium on Theory of Computing, pages 345–354. May 1989. "Theorem 5: Any CPROBE(log n) implementation of the set union problem requires Ω(mα(m, n)) time to execute m Find's and n−1 Union's, beginning with n singleton sets."
123456Cormen,Thomas H.;Leiserson,Charles E.;Rivest,Ronald L.;Stein,Clifford(2009).“Chapter 21: Data structures for Disjoint Sets”.Introduction to Algorithms(Third ed.).MIT Press.ISBN978-0-262-03384-8
↑Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Chapter 21: Data structures for Disjoint Sets, pp.498–524.
12Galler,Bernard A.;Fischer,Michael J.(1964-05).“An improved equivalence algorithm”.Communications of the ACM7(5): 301–303.doi:10.1145/364099.364331. 素集合森に関する最初の論文。
123Tarjan,Robert Endre(1979).“A class of algorithms which require non-linear time to maintain disjoint sets”.Journal of Computer and System Sciences18(2): 110–127.doi:10.1016/0022-0000(79)90042-4.
12Hopcroft,J. E.;Ullman,J. D.(1973).“Set Merging Algorithms”.SIAM Journal on Computing2(4): 294–303.doi:10.1137/0202024.
↑Blum,Norbert(1985).“On the Single-Operation Worst-Case Time Complexity of the Disjoint Set Union Problem”.2nd Symp. On Theoretical Aspects of Computer Science: 32–38.
↑Alstrup,Stephen;Ben-Amram,Amir M.;Rauhe,Theis(1999).“Worst-case and amortised optimality in union-find (Extended abstract)”.Proceedings of the thirty-first annual ACM symposium on Theory of Computing.pp.499–506.doi:10.1145/301250.301383
↑Alstrup,Stephen;Thorup,Mikkel;Gørtz,Inge Li;Rauhe,Theis;Zwick,Uri(2014).“Union-Find with Constant Time Deletions”.ACM Transactions on Algorithms11(1): 6:1–6:28.doi:10.1145/2636922.
↑Ben-Amram,Amir M.;Yoffe,Simon(2011).“A simple and efficient Union-Find-Delete algorithm”.Theoretical Computer Science412(4–5): 487–492.doi:10.1016/j.tcs.2010.11.005.
123Zvi Galil and Giuseppe F. Italiano. Data structures and algorithms for disjoint set union problems, ACM Computing Surveys, Volume 23, Issue 3 (September 1991), pages 319-344. ACM Digital Library
↑Westbrook,Jeffery R.;Tarjan,Robert E.(1989).“Amortized Analysis of Algorithms for Set Union with Backtracking”.SIAM Journal on Computing18(1): 1–11.doi:10.1137/0218001.
12Sylvain Conchon and Jean-Christophe Filliâtre. A Persistent Union-Find Data Structure. In ACM SIGPLAN Workshop on ML, Freiburg, Germany, October 2007. 論文ページ
↑Gabow,Harold N.;Tarjan,Robert Endre(1985).“A linear-time algorithm for a special case of disjoint set union”.Journal of Computer and System Sciences30(2): 209–221.doi:10.1016/0022-0000(85)90014-5.
↑Knight,Kevin(1989).“Unification: A multidisciplinary survey”.ACM Computing Surveys21(1): 93–124.doi:10.1145/62029.62030.
↑
Anderson, Richard J.; Woll, Heather (1991). Wait-free Parallel Algorithms for the Union-Find Problem. 23rd ACM Symposium on Theory of Computing. pp.370–380. doi:10.1145/103418.103458.
All text is available under the terms of the GNU Free Documentation License. この記事は、ウィキペディアの素集合データ構造 (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。
Weblio辞書に掲載されているウィキペディアの記事も、全てGNU Free Documentation Licenseの元に提供されております。