private 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"); }
private static void Compute() { char[] split = { '\n', '\r' }; var input = File.ReadAllText(DataFiles.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); }
public static void Client() { int[] items = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var uf = new QuickUnionUf(items); uf.Union(1, 2); uf.Union(1, 3); uf.Union(1, 6); //uf.Union(2, 5); //uf.Union(2, 7); //uf.Union(3, 4); //uf.Union(6, 8); //uf.Union(8, 9); var connected = uf.Connected(3, 6) ? "Connected" : "Not Connected"; Console.WriteLine(connected); connected = uf.Connected(3, 5) ? "Connected" : "Not Connected"; Console.WriteLine(connected); }