static void Compute() { var size = 200; var quickFind = new QuickFindUF(size); var quickUnion = new QuickUnionUF(size); var weightedQuickUnion = new WeightedQuickUnionUF(size); var connections = ErdosRenyi.Generate(size); var quickFindResult = new int[size]; var quickUnionResult = new int[size]; var weightedQuickUnionResult = new int[size]; int p, q; for (var i = 0; i < size; i++) { p = connections[i].P; q = connections[i].Q; quickFind.Union(p, q); quickUnion.Union(p, q); weightedQuickUnion.Union(p, q); quickFindResult[i] = quickFind.ArrayVisitCount; quickUnionResult[i] = quickUnion.ArrayVisitCount; weightedQuickUnionResult[i] = weightedQuickUnion.ArrayParentVisitCount + weightedQuickUnion.ArraySizeVisitCount; quickFind.ResetArrayCount(); quickUnion.ResetArrayCount(); weightedQuickUnion.ResetArrayCount(); } Draw(quickFindResult, "Quick-Find"); Draw(quickUnionResult, "Quick-Union"); Draw(weightedQuickUnionResult, "Weighted Quick-Union"); }
static void Main(string[] args) { var n = 10000; for (var t = 0; t < 5; t++) { var input = ErdosRenyi.Generate(n); var weightedQuickUnionUF = new WeightedQuickUnionUF(n); var weightedQuickUnionPathCompressionUF = new WeightedQuickUnionPathCompressionUF(n); Console.WriteLine("N:" + n); var weightedQuickUnionTime = RunTest(weightedQuickUnionUF, input); var weightedQuickUnionPathCompressionTime = RunTest(weightedQuickUnionPathCompressionUF, input); Console.WriteLine("加权 quick-find 耗时(毫秒):" + weightedQuickUnionTime); Console.WriteLine("带路径压缩的加权 quick-union 耗时(毫秒):" + weightedQuickUnionPathCompressionTime); Console.WriteLine("比值:" + (double)weightedQuickUnionTime / weightedQuickUnionPathCompressionTime); Console.WriteLine(); n *= 2; } }
static void Main(string[] args) { var n = 2000; for (var t = 0; t < 5; t++) { var input = ErdosRenyi.Generate(n); var quickFind = new QuickFindUF(n); var quickUnion = new QuickUnionUF(n); Console.WriteLine("N:" + n); var quickFindTime = RunTest(quickFind, input); var quickUnionTime = RunTest(quickUnion, input); Console.WriteLine("quick-find 耗时(毫秒):" + quickFindTime); Console.WriteLine("quick-union 耗时(毫秒):" + quickUnionTime); Console.WriteLine("比值:" + (double)quickFindTime / quickUnionTime); Console.WriteLine(); n *= 2; } }