今週の日曜日に試験があるんだけど、自分がやっていることは正しいかどうかを確認したいだけなんだ (試験は私を懐疑的にするんだよね)
アルゴリズムの仕組みは次のとおりです。
int Find(int x)
{ // Return the set containing x and compress the path from x to root
// First, find the root
int z = x; while (P[z] > 0) { z = P[z]; } int root = z;
// Start at x again and reset the parent // of every node on the path from x to root z = x;
while (P[z] > 0)
{ int t = P[z];
P[z] = root; // Reset Parent of z to root
z = t; }
return root; }
これは質問です:
n 個の要素の集合から素集合に対する Union-Find 問題のために開発されたアルゴリズムを思い出してください。Find はパス圧縮を使用し、Union はランキングを使用します。また、同じランクの 2 つのツリーの和集合は、2 番目の引数に関連付けられたルートを新しいルートとして選択します。セット S = {1, 2, ..., 10} と 10 個の互いに素な部分集合 (それぞれが 1 つの要素を含む) から始めます。を。
Union(1,2)、Union(3,4)、Union(5,6)、Union(1,5)、Union(1,3)を実行した後、ツリーの最終セットを描画します。
そして、これは質問に対する私の解決策です:
このアルゴリズムに関するヒントや提案があれば幸いです。
ありがとう!