private static void testBigMerge() { int maxRank = 20; int trials = 10000; int numElems = 1 << maxRank; // Grows exponentially DisjointSet ds = new DisjointSet(numElems); for (int level = 0; level < maxRank; level++) { int mergeStep = 1 << level; int incrStep = mergeStep * 2; for (int i = 0; i < numElems; i += incrStep) { assert(!ds.AreInSameSet(i, i + mergeStep)); assert(ds.MergeSets(i, i + mergeStep)); } // Now we have a bunch of sets of size 2^(level+1) // Do random tests int mask = -incrStep; // 0b11...100...00 for (int i = 0; i < trials; i++) { int j = rand.Next(numElems); int k = rand.Next(numElems); bool expect = (j & mask) == (k & mask); assert(ds.AreInSameSet(j, k) == expect); } } }
public void AlreadyMergedItems() { var data = new[] { "A", "B", "C", }; var disjointSet = new DisjointSet <string>(data); disjointSet.Union("A", "B"); Assert.True(disjointSet.AreInSameSet("A", "B")); Assert.False(disjointSet.AreInSameSet("A", "C")); }
/*---- Test suite ----*/ private static void testNew() { DisjointSet ds = new DisjointSet(10); assert(ds.GetNumberOfSets() == 10); assert(ds.GetSizeOfSet(0) == 1); assert(ds.GetSizeOfSet(2) == 1); assert(ds.GetSizeOfSet(9) == 1); assert(ds.AreInSameSet(0, 0)); assert(!ds.AreInSameSet(0, 1)); assert(!ds.AreInSameSet(9, 3)); ds.CheckStructure(); }
/*---- Test suite ----*/ private static void TestNew() { DisjointSet ds = new DisjointSet(10); AssertEquals(10, ds.NumberOfSets); AssertEquals(1, ds.GetSizeOfSet(0)); AssertEquals(1, ds.GetSizeOfSet(2)); AssertEquals(1, ds.GetSizeOfSet(9)); AssertTrue(ds.AreInSameSet(0, 0)); AssertFalse(ds.AreInSameSet(0, 1)); AssertFalse(ds.AreInSameSet(9, 3)); ds.CheckStructure(); }
private static void testAgainstNaiveRandomly() { int trials = 1000; int iterations = 3000; int numElems = 300; for (int i = 0; i < trials; i++) { NaiveDisjointSet nds = new NaiveDisjointSet(numElems); DisjointSet ds = new DisjointSet(numElems); for (int j = 0; j < iterations; j++) { int k = rand.Next(numElems); int l = rand.Next(numElems); assert(ds.GetSizeOfSet(k) == nds.GetSizeOfSet(k)); assert(ds.AreInSameSet(k, l) == nds.AreInSameSet(k, l)); if (rand.NextDouble() < 0.1) { assert(ds.MergeSets(k, l) == nds.MergeSets(k, l)); } assert(nds.GetNumberOfSets() == ds.GetNumberOfSets()); if (rand.NextDouble() < 0.001) { ds.CheckStructure(); } } ds.CheckStructure(); } }
private static void testMerge() { DisjointSet ds = new DisjointSet(10); assert(ds.MergeSets(0, 1)); ds.CheckStructure(); assert(ds.GetNumberOfSets() == 9); assert(ds.AreInSameSet(0, 1)); assert(ds.MergeSets(2, 3)); ds.CheckStructure(); assert(ds.GetNumberOfSets() == 8); assert(ds.AreInSameSet(2, 3)); assert(!ds.MergeSets(2, 3)); ds.CheckStructure(); assert(ds.GetNumberOfSets() == 8); assert(!ds.AreInSameSet(0, 2)); assert(ds.MergeSets(0, 3)); ds.CheckStructure(); assert(ds.GetNumberOfSets() == 7); assert(ds.AreInSameSet(0, 2)); assert(ds.AreInSameSet(3, 0)); assert(ds.AreInSameSet(1, 3)); }
private static void TestMerge() { DisjointSet ds = new DisjointSet(10); AssertTrue(ds.MergeSets(0, 1)); ds.CheckStructure(); AssertEquals(9, ds.NumberOfSets); AssertTrue(ds.AreInSameSet(0, 1)); AssertTrue(ds.MergeSets(2, 3)); ds.CheckStructure(); AssertEquals(8, ds.NumberOfSets); AssertTrue(ds.AreInSameSet(2, 3)); AssertFalse(ds.MergeSets(2, 3)); ds.CheckStructure(); AssertEquals(8, ds.NumberOfSets); AssertFalse(ds.AreInSameSet(0, 2)); AssertTrue(ds.MergeSets(0, 3)); ds.CheckStructure(); AssertEquals(7, ds.NumberOfSets); AssertTrue(ds.AreInSameSet(0, 2)); AssertTrue(ds.AreInSameSet(3, 0)); AssertTrue(ds.AreInSameSet(1, 3)); }