public void testUnionAndConnected_MultiConnection() { QuickUnionUF quickUnionUF = new QuickUnionUF(10); quickUnionUF.Union(0, 1); quickUnionUF.Union(2, 1); Assert.IsTrue(quickUnionUF.Connected(0, 1)); Assert.IsTrue(quickUnionUF.Connected(2, 1)); Assert.IsTrue(quickUnionUF.Connected(2, 0)); Assert.AreEqual(8, quickUnionUF.Count()); Assert.AreEqual(1, quickUnionUF.Find(1)); Assert.AreEqual(1, quickUnionUF.Find(0)); Assert.AreEqual(1, quickUnionUF.Find(2)); quickUnionUF.Union(2, 5); Assert.IsTrue(quickUnionUF.Connected(0, 1)); Assert.IsTrue(quickUnionUF.Connected(2, 1)); Assert.IsTrue(quickUnionUF.Connected(2, 0)); Assert.IsTrue(quickUnionUF.Connected(5, 0)); Assert.IsTrue(quickUnionUF.Connected(5, 1)); Assert.IsTrue(quickUnionUF.Connected(5, 2)); Assert.AreEqual(7, quickUnionUF.Count()); Assert.AreEqual(5, quickUnionUF.Find(1)); Assert.AreEqual(5, quickUnionUF.Find(0)); Assert.AreEqual(5, quickUnionUF.Find(2)); Assert.AreEqual(5, quickUnionUF.Find(5)); }
public void Connect_4_with_3_and_3_with_8_and_9_with_4() { var uf = new QuickUnionUF(10); uf.Union(4, 3); uf.Union(3, 8); uf.Union(9, 4); for (int i = 0; i < uf.Length; i++) { if (i == 3) { Assert.Equal(8, uf.Nodes[i]); } else if (i == 4) { Assert.Equal(3, uf.Nodes[i]); } else if (i == 9) { Assert.Equal(8, uf.Nodes[i]); } else { Assert.Equal(i, uf.Nodes[i]); } } }
public void A_big_test() { var uf = new QuickUnionUF(10); uf.Union(4, 3); uf.Union(3, 8); uf.Union(6, 5); uf.Union(9, 4); uf.Union(2, 1); Assert.True(uf.IsConnected(8, 9)); Assert.False(uf.IsConnected(5, 4)); uf.Union(5, 0); uf.Union(7, 2); uf.Union(6, 1); uf.Union(7, 3); Assert.Equal(1, uf.Nodes[0]); Assert.Equal(8, uf.Nodes[1]); Assert.Equal(1, uf.Nodes[2]); Assert.Equal(8, uf.Nodes[3]); Assert.Equal(3, uf.Nodes[4]); Assert.Equal(0, uf.Nodes[5]); Assert.Equal(5, uf.Nodes[6]); Assert.Equal(1, uf.Nodes[7]); Assert.Equal(8, uf.Nodes[8]); Assert.Equal(8, uf.Nodes[9]); }
public void testUnionAndConnected_AlreadyConnected() { QuickUnionUF quickUnionUF = new QuickUnionUF(10); quickUnionUF.Union(0, 1); Assert.IsTrue(quickUnionUF.Connected(0, 1)); Assert.AreEqual(9, quickUnionUF.Count()); quickUnionUF.Union(0, 1); Assert.IsTrue(quickUnionUF.Connected(0, 1)); Assert.AreEqual(9, quickUnionUF.Count()); Assert.AreEqual(1, quickUnionUF.Find(1)); Assert.AreEqual(1, quickUnionUF.Find(0)); }
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() { 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 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"); }
public static void Main() { QuickUnionUF uf = new QuickUnionUF(10); Console.WriteLine("Connected (1,2): " + ((uf.Connected(1, 2) == false) ? "Passed":"Failed")); uf.Union(2, 3); Console.WriteLine("Connected (2,3): " + ((uf.Connected(2, 3) == true) ? "Passed":"Failed")); uf.Union(4, 7); uf.Union(1, 8); uf.Union(1, 7); uf.Union(2, 4); Console.WriteLine("Connected (1,4): " + ((uf.Connected(1, 4) == true) ? "Passed":"Failed")); Console.WriteLine("Connected (8,9): " + ((uf.Connected(8, 9) == false) ? "Passed":"Failed")); Console.WriteLine("Connected (3,8): " + ((uf.Connected(3, 8) == true) ? "Passed":"Failed")); Console.WriteLine("Connected (5,9): " + ((uf.Connected(5, 9) == false) ? "Passed":"Failed")); Console.WriteLine("Connected (10,9): " + ((uf.Connected(10, 9) == false) ? "Passed":"Failed")); }
static void Main(string[] args) { //Quick-find QuickFindUF grid = new QuickFindUF(10); grid.Union(2, 3); grid.Union(3, 6); grid.Union(6, 5); Console.WriteLine($"Does 2 and 5 connect? {grid.Connected(2, 5)}"); Console.WriteLine($"Does 6 and 1 connect? {grid.Connected(1, 6)}"); //Quick-union QuickUnionUF quuf = new QuickUnionUF(10); quuf.Union(2, 3); quuf.Union(3, 6); quuf.Union(6, 5); Console.WriteLine($"Does 2 and 5 connect? {quuf.Connected(2, 5)}"); Console.WriteLine($"Does 6 and 1 connect? {quuf.Connected(1, 6)}"); Console.WriteLine(GreatestCommonDenominator.GetGCD(9, 0)); }
public void Connect_4_with_3() { int from = 4; int to = 3; var uf = new QuickUnionUF(10); uf.Union(from, to); for (int i = 0; i < uf.Length; i++) { if (i == from) { Assert.Equal(to, uf.Nodes[i]); } else { Assert.Equal(i, uf.Nodes[i]); } } }
public void testUnion_ParameterValidation_QLargerThanN() { QuickUnionUF quickUnionUF = new QuickUnionUF(10); quickUnionUF.Union(4, 11); }
public void testUnion_ParameterValidation_QEqualN() { QuickUnionUF quickUnionUF = new QuickUnionUF(10); quickUnionUF.Union(4, 10); }
public void testUnion_ParameterValidation_QLessThan0() { QuickUnionUF quickUnionUF = new QuickUnionUF(10); quickUnionUF.Union(4, -1); }