static void Compute() { char[] split = { '\n', '\r' }; var input = TestCase.Properties.Resources.mediumUF.Split(split, StringSplitOptions.RemoveEmptyEntries); var size = int.Parse(input[0]); var quickFind = new QuickFindUF(size); var quickUnion = new QuickUnionUF(size); string[] pair; int p, q; var quickFindResult = new int[size]; var quickUnionResult = new int[size]; for (var i = 1; i < size; i++) { pair = input[i].Split(' '); p = int.Parse(pair[0]); q = int.Parse(pair[1]); quickFind.Union(p, q); quickUnion.Union(p, q); quickFindResult[i - 1] = quickFind.ArrayVisitCount; quickUnionResult[i - 1] = quickUnion.ArrayVisitCount; quickFind.ResetArrayCount(); quickUnion.ResetArrayCount(); } Draw(quickFindResult); Draw(quickUnionResult); }
static void Main(string[] args) { string[] input = "9-0 3-4 5-8 7-2 2-1 5-7 0-3 4-2".Split(' '); var quickUnion = new QuickUnionUF(10); foreach (string s in input) { quickUnion.ResetArrayCount(); string[] numbers = s.Split('-'); int p = int.Parse(numbers[0]); int q = int.Parse(numbers[1]); quickUnion.Union(p, q); int[] parent = quickUnion.GetParent(); for (int i = 0; i < parent.Length; i++) { if (parent[i] == i) { Console.WriteLine("|---- " + i); DFS(parent, i, 1); } } Console.WriteLine("数组访问:" + quickUnion.ArrayVisitCount); } }
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"); }