private void Add([NotNull] int[] keys) { QuikGraphAssert.TrueForAll(keys, k => k < int.MaxValue); Assert.IsTrue(keys.Length > 0); var target = new SoftHeap <int, int>(1 / 4.0, int.MaxValue); foreach (int key in keys) { int count = target.Count; target.Add(key, key + 1); Assert.AreEqual(count + 1, target.Count); } int lastMin = int.MaxValue; int error = 0; while (target.Count > 0) { var kv = target.DeleteMin(); if (lastMin < kv.Key) { ++error; } lastMin = kv.Key; Assert.AreEqual(kv.Key + 1, kv.Value); } Assert.IsTrue(error / (double)keys.Length <= target.ErrorRate); }
public void RemoveMinimum(int[] keys, double errorRate) { QuikGraphAssert.TrueForAll(keys, k => k < int.MaxValue); Assert.IsTrue(keys.Length > 0); var heap = new SoftHeap <int, string>(errorRate, int.MaxValue); foreach (int key in keys) { heap.Add(key, key.ToString()); } Assert.AreEqual(keys.Length, heap.Count); int lastMinimum = int.MaxValue; int nbError = 0; while (heap.Count > 0) { KeyValuePair <int, string> pair = heap.RemoveMinimum(); // Removed pair can be not the minimal // Since it's a "soft" heap that not guarantee 100% result // But a faster management if (lastMinimum < pair.Key) { ++nbError; } lastMinimum = pair.Key; Assert.AreEqual(pair.Key.ToString(), pair.Value); } Assert.IsTrue(nbError / (double)keys.Length <= errorRate); }
private static void RunHoffmanPavleyRankedShortestPathAndCheck <TVertex, TEdge>( IBidirectionalGraph <TVertex, TEdge> graph, Dictionary <TEdge, double> edgeWeights, TVertex rootVertex, TVertex targetVertex, int pathCount) where TEdge : IEdge <TVertex> { QuikGraphAssert.TrueForAll(graph.Edges, edgeWeights.ContainsKey); var target = new HoffmanPavleyRankedShortestPathAlgorithm <TVertex, TEdge>(graph, e => edgeWeights[e]) { ShortestPathCount = pathCount }; target.Compute(rootVertex, targetVertex); double lastWeight = double.MinValue; foreach (TEdge[] path in target.ComputedShortestPaths.Select(p => p.ToArray())) { double weight = path.Sum(e => edgeWeights[e]); Assert.IsTrue(lastWeight <= weight, $"{lastWeight} <= {weight}"); Assert.AreEqual(rootVertex, path.First().Source); Assert.AreEqual(targetVertex, path.Last().Target); Assert.IsTrue(path.IsPathWithoutCycles <TVertex, TEdge>()); lastWeight = weight; } }
private static void ComputeTrails <TVertex, TEdge>( IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph, TVertex root, Func <TVertex, TVertex, TEdge> edgeFactory, out ICollection <TEdge>[] trails, out TEdge[] circuit) where TEdge : IEdge <TVertex> { trails = new ICollection <TEdge> [0]; circuit = new TEdge[0]; int circuitCount = EulerianTrailAlgorithm <TVertex, TEdge> .ComputeEulerianPathCount(graph); if (circuitCount == 0) { return; } TEdge[] graphEdges = graph.Edges.ToArray(); var algorithm = new EulerianTrailAlgorithm <TVertex, TEdge>(graph); algorithm.AddTemporaryEdges((s, t) => edgeFactory(s, t)); TEdge[] augmentedGraphEdges = graph.Edges.ToArray(); Assert.GreaterOrEqual(augmentedGraphEdges.Length, graphEdges.Length); TEdge[] temporaryEdges = augmentedGraphEdges.Except(graphEdges).ToArray(); Assert.AreEqual(augmentedGraphEdges.Length - graphEdges.Length, temporaryEdges.Length); algorithm.Compute(); trails = algorithm.Trails(root).ToArray(); algorithm.RemoveTemporaryEdges(); Assert.IsNotNull(algorithm.Circuit); circuit = algorithm.Circuit; // Lets make sure all the edges are in the trail var edges = new HashSet <TEdge>(); foreach (TEdge edge in graph.Edges) { Assert.IsTrue(edges.Add(edge)); } foreach (ICollection <TEdge> trail in trails) { Assert.AreEqual(graph.EdgeCount, edges.Count); QuikGraphAssert.TrueForAll( trail, // Edge in graph or part of temporary ones but is a root edge => edges.Contains(edge) || (temporaryEdges.Contains(edge) && Equals(edge.Source, root))); } }
private static void InsertAndEnumerate <TPriority, TValue>( [NotNull] BinaryHeap <TPriority, TValue> heap, [NotNull] KeyValuePair <TPriority, TValue>[] pairs) { var dictionary = new Dictionary <TPriority, TValue>(); foreach (KeyValuePair <TPriority, TValue> pair in pairs) { heap.Add(pair.Key, pair.Value); dictionary[pair.Key] = pair.Value; } QuikGraphAssert.TrueForAll(heap, pair => dictionary.ContainsKey(pair.Key)); }
private static void ComputeTrails <TVertex, TEdge>( IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph, Func <TVertex, TVertex, TEdge> edgeFactory, out ICollection <TEdge>[] trails, out TEdge[] circuit) where TEdge : IEdge <TVertex> { trails = new ICollection <TEdge> [0]; circuit = new TEdge[0]; int circuitCount = EulerianTrailAlgorithm <TVertex, TEdge> .ComputeEulerianPathCount(graph); if (circuitCount == 0) { return; } var algorithm = new EulerianTrailAlgorithm <TVertex, TEdge>(graph); algorithm.AddTemporaryEdges((s, t) => edgeFactory(s, t)); algorithm.Compute(); trails = algorithm.Trails().ToArray(); algorithm.RemoveTemporaryEdges(); Assert.IsNotNull(algorithm.Circuit); circuit = algorithm.Circuit; // Lets make sure all the edges are in the trail var edges = new HashSet <TEdge>(); foreach (TEdge edge in graph.Edges) { Assert.IsTrue(edges.Add(edge)); } foreach (ICollection <TEdge> trail in trails) { Assert.AreEqual(graph.EdgeCount, edges.Count); QuikGraphAssert.TrueForAll( trail, edge => edges.Contains(edge)); } }
public void ProcessAllComponents(bool processAll) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge21 = new Edge <int>(2, 1); var edge24 = new Edge <int>(2, 4); var edge25 = new Edge <int>(2, 5); var edge67 = new Edge <int>(6, 7); var edge68 = new Edge <int>(6, 8); var edge86 = new Edge <int>(8, 6); var graph = new AdjacencyGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge21, edge24, edge25, edge67, edge68, edge86 }); var algorithm = new EdgeDepthFirstSearchAlgorithm <int, Edge <int> >(graph) { ProcessAllComponents = processAll }; algorithm.Compute(1); if (processAll) { QuikGraphAssert.TrueForAll(algorithm.EdgesColors, pair => pair.Value == GraphColor.Black); } else { QuikGraphAssert.TrueForAll( new[] { edge12, edge13, edge24, edge25 }, edge => algorithm.EdgesColors[edge] == GraphColor.Black); QuikGraphAssert.TrueForAll( new[] { edge67, edge68, edge86 }, edge => algorithm.EdgesColors[edge] == GraphColor.White); } }
public void ProcessAllComponents(bool processAll) { var graph = new UndirectedGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(2, 1), new Edge <int>(2, 4), new Edge <int>(2, 5), new Edge <int>(6, 7), new Edge <int>(6, 8), new Edge <int>(8, 6) }); var algorithm = new UndirectedDepthFirstSearchAlgorithm <int, Edge <int> >(graph) { ProcessAllComponents = processAll }; algorithm.Compute(1); if (processAll) { QuikGraphAssert.TrueForAll(algorithm.VerticesColors, pair => pair.Value == GraphColor.Black); } else { QuikGraphAssert.TrueForAll( new[] { 1, 2, 3, 4, 5 }, vertex => algorithm.VerticesColors[vertex] == GraphColor.Black); QuikGraphAssert.TrueForAll( new[] { 6, 7, 8 }, vertex => algorithm.VerticesColors[vertex] == GraphColor.White); } }
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); } }