public static ForestDisjointSet<int> Create(int[] elements, int[] unions) { var ds = new ForestDisjointSet<int>(); for (int i = 0; i < elements.Length; ++i) ds.MakeSet(i); for (int i = 0; i+1 < unions.Length; i+=2) { PexAssume.IsTrue(ds.Contains(unions[i])); PexAssume.IsTrue(ds.Contains(unions[i+1])); ds.Union(unions[i], unions[i+1]); } return ds; }
public void Contains() { ContainsTest(1, 2); ContainsTest( new TestVertex("1"), new TestVertex("2")); ContainsTest( new EquatableTestVertex("1"), new EquatableTestVertex("2")); #region Local function void ContainsTest <TValue>(TValue value1, TValue value2) { var set = new ForestDisjointSet <TValue>(); Assert.IsFalse(set.Contains(value1)); Assert.IsFalse(set.Contains(value2)); set.MakeSet(value1); Assert.IsTrue(set.Contains(value1)); Assert.IsFalse(set.Contains(value2)); set.MakeSet(value2); Assert.IsTrue(set.Contains(value1)); Assert.IsTrue(set.Contains(value2)); set.Union(value1, value2); Assert.IsTrue(set.Contains(value1)); Assert.IsTrue(set.Contains(value2)); } #endregion }
public void Unions(int elementCount, [PexAssumeNotNull]KeyValuePair<int,int>[] unions) { PexAssume.IsTrue(0 < elementCount); PexSymbolicValue.Minimize(elementCount); PexAssume.TrueForAll( unions, u => 0 <= u.Key && u.Key < elementCount && 0 <= u.Value && u.Value < elementCount ); var target = new ForestDisjointSet<int>(); // fill up with 0..elementCount - 1 for (int i = 0; i < elementCount; i++) { target.MakeSet(i); Assert.IsTrue(target.Contains(i)); Assert.AreEqual(i + 1, target.ElementCount); Assert.AreEqual(i + 1, target.SetCount); } // apply Union for pairs unions[i], unions[i+1] for (int i = 0; i < unions.Length; i++) { var left = unions[i].Key; var right= unions[i].Value; var setCount = target.SetCount; bool unioned = target.Union(left, right); // should be in the same set now Assert.IsTrue(target.AreInSameSet(left, right)); // if unioned, the count decreased by 1 PexAssert.ImpliesIsTrue(unioned, () => setCount - 1 == target.SetCount); } }
public void Contains_Throws() { var set = new ForestDisjointSet <TestVertex>(); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => set.Contains(null)); }
public static ForestDisjointSet <int> Create([NotNull] int[] elements, [NotNull] int[] unions) { var sets = new ForestDisjointSet <int>(); for (int i = 0; i < elements.Length; ++i) { sets.MakeSet(i); } for (int i = 0; i + 1 < unions.Length; i += 2) { Assert.IsTrue(sets.Contains(unions[i])); Assert.IsTrue(sets.Contains(unions[i + 1])); sets.Union(unions[i], unions[i + 1]); } return(sets); }
private void Unions(int elementCount, [NotNull] KeyValuePair <int, int>[] unions) { Assert.IsTrue(0 < elementCount); QuikGraphAssert.TrueForAll( unions, u => 0 <= u.Key && u.Key < elementCount && 0 <= u.Value && u.Value < elementCount); var target = new ForestDisjointSet <int>(); // Fill up with 0..elementCount - 1 for (int i = 0; i < elementCount; ++i) { target.MakeSet(i); Assert.IsTrue(target.Contains(i)); Assert.AreEqual(i + 1, target.ElementCount); Assert.AreEqual(i + 1, target.SetCount); } // Apply Union for pairs unions[i], unions[i+1] foreach (KeyValuePair <int, int> pair in unions) { int left = pair.Key; int right = pair.Value; int setCount = target.SetCount; bool unioned = target.Union(left, right); // Should be in the same set now Assert.IsTrue(target.AreInSameSet(left, right)); // If unioned, the count decreased by 1 QuikGraphAssert.ImpliesIsTrue(unioned, () => setCount - 1 == target.SetCount); } }