private static void RunHoffmanPavleyRankedShortestPathAndCheck <TVertex, TEdge>(
            [NotNull] IBidirectionalGraph <TVertex, TEdge> graph,
            [NotNull] Dictionary <TEdge, double> edgeWeights,
            [NotNull] TVertex rootVertex,
            [NotNull] TVertex targetVertex,
            int pathCount)
            where TEdge : IEdge <TVertex>
        {
            FastGraphAssert.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;
            }
        }
Example #2
0
        public void RemoveMinimum([NotNull] int[] keys, double errorRate)
        {
            FastGraphAssert.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);
        }
Example #3
0
        private static void ComputeTrails <TVertex, TEdge>(
            [NotNull] IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph,
            [NotNull] TVertex root,
            [NotNull, InstantHandle] Func <TVertex, TVertex, TEdge> edgeFactory,
            [NotNull, ItemNotNull] out ICollection <TEdge>[] trails,
            [NotNull, ItemNotNull] 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);
                FastGraphAssert.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)));
            }
        }
        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)
            {
                FastGraphAssert.TrueForAll(algorithm.EdgesColors, pair => pair.Value == GraphColor.Black);
            }
            else
            {
                FastGraphAssert.TrueForAll(
                    new[] { edge12, edge13, edge24, edge25 },
                    edge => algorithm.EdgesColors[edge] == GraphColor.Black);
                FastGraphAssert.TrueForAll(
                    new[] { edge67, edge68, edge86 },
                    edge => algorithm.EdgesColors[edge] == GraphColor.White);
            }
        }
Example #5
0
        private static void ComputeTrails <TVertex, TEdge>(
            [NotNull] IMutableVertexAndEdgeListGraph <TVertex, TEdge> graph,
            [NotNull, InstantHandle] Func <TVertex, TVertex, TEdge> edgeFactory,
            [NotNull, ItemNotNull] out ICollection <TEdge>[] trails,
            [NotNull, ItemNotNull] 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);
                FastGraphAssert.TrueForAll(
                    trail,
                    edge => edges.Contains(edge));
            }
        }
Example #6
0
        public void ProcessAllComponents(bool processAll)
        {
            var graph = new BidirectionalGraph <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 BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph)
            {
                ProcessAllComponents = processAll
            };

            algorithm.Compute(1);

            if (processAll)
            {
                FastGraphAssert.TrueForAll(algorithm.VerticesColors, pair => pair.Value == GraphColor.Black);
            }
            else
            {
                FastGraphAssert.TrueForAll(
                    new[] { 1, 2, 3, 4, 5 },
                    vertex => algorithm.VerticesColors[vertex] == GraphColor.Black);
                FastGraphAssert.TrueForAll(
                    new[] { 6, 7, 8 },
                    vertex => algorithm.VerticesColors[vertex] == GraphColor.White);
            }
        }