コード例 #1
0
        public void TestRemoveEdge()
        {
            var graph = new DirectedDynamicGraph();

            graph.AddEdge(1, 2, 0102, 010200);
            graph.AddEdge(1, 3, 0103);

            Assert.AreEqual(1, graph.RemoveEdge(1, 2));

            Assert.AreEqual(1, graph.EdgeCount);
            var enumerator = graph.GetEdgeEnumerator();

            Assert.IsTrue(enumerator.MoveTo(1));
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(3, enumerator.Neighbour);
            Assert.AreEqual(0103, enumerator.Data0);

            graph = new DirectedDynamicGraph();
            graph.AddEdge(1, 2, 0102, 010200);
            graph.AddEdge(1, 3, 0103);
            graph.AddEdge(1, 4, 0104, 010400, 01040000);

            Assert.AreEqual(1, graph.RemoveEdge(1, 3));
            enumerator = graph.GetEdgeEnumerator();
            Assert.IsTrue(enumerator.MoveTo(1));
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(2, enumerator.Neighbour);
            Assert.AreEqual(0102, enumerator.Data0);
            Assert.AreEqual(010200, enumerator.DynamicData[0]);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(4, enumerator.Neighbour);
            Assert.AreEqual(0104, enumerator.Data0);
            Assert.AreEqual(010400, enumerator.DynamicData[0]);
            Assert.AreEqual(01040000, enumerator.DynamicData[1]);
        }
コード例 #2
0
        public void Test3Vertices()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 0, 100, null);
            graph.AddEdge(1, 2, 100, null);
            graph.AddEdge(2, 1, 100, null);
            graph.Compress();

            // contract graph.
            var hierarchyBuilder = new HierarchyBuilder(graph, new EdgeDifferencePriorityCalculator(graph, new DykstraWitnessCalculator(int.MaxValue)),
                                                        new DykstraWitnessCalculator(int.MaxValue), (i) => Enumerable.Empty <uint[]>());

            hierarchyBuilder.Run();

            // check edges.
            var edges01 = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 1);

            Assert.IsNotNull(edges01);
            var edge10 = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 0);

            Assert.IsNull(edge10);
            var edge12 = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 0);

            Assert.IsNull(edge12);
            var edges21 = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 1);

            Assert.IsNotNull(edges21);
        }
コード例 #3
0
        public void TestVertexCountAndTrim()
        {
            var graph = new DirectedDynamicGraph(10, 1);

            // add edge.
            graph.AddEdge(0, 1, 1);

            // trim.
            graph.Trim();
            Assert.AreEqual(2, graph.VertexCount);

            graph = new DirectedDynamicGraph(10, 1);

            // add edge.
            graph.AddEdge(0, 1, 1);
            graph.AddEdge(0, 11001, 1);

            // trim.
            graph.Trim();
            Assert.AreEqual(11002, graph.VertexCount);

            graph = new DirectedDynamicGraph(10, 1);

            // trim.
            graph.Trim(); // keep minimum one vertex.
            Assert.AreEqual(1, graph.VertexCount);
        }
コード例 #4
0
        public void TestTwoEdgeInfiniteHops()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 2, 100, null);

            var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue);

            // calculate witness for weight of 200.
            var forwardWitnesses  = new EdgePath <float> [1];
            var backwardWitnesses = new EdgePath <float> [1];

            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 1000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(2, forwardWitnesses[0].Vertex);
            Assert.AreEqual(2, backwardWitnesses[0].Vertex);

            // calculate witness for weight of 50.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 50 }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);
        }
コード例 #5
0
        public void TestTwoNeighboursOneWayOpposite()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, true);
            graph.AddEdge(1, 0, 100, false);
            graph.AddEdge(0, 2, 100, true);
            graph.AddEdge(2, 0, 100, false);

            // create a witness calculator and the priority calculator.
            var priorityCalculator = new EdgeDifferencePriorityCalculator(graph,
                                                                          new WitnessCalculatorMock((source, target) =>
            {
                if (source == 0 && target == 1)
                {
                    return(new Tuple <EdgePath <float>, EdgePath <float> >(
                               new EdgePath <float>(0, 100, new EdgePath <float>(1)),
                               new EdgePath <float>()));
                }
                if (source == 0 && target == 2)
                {
                    return(new Tuple <EdgePath <float>, EdgePath <float> >(
                               new EdgePath <float>(0, 100, new EdgePath <float>(2)),
                               new EdgePath <float>()));
                }
                if (source == 1 && target == 0)
                {
                    return(new Tuple <EdgePath <float>, EdgePath <float> >(
                               new EdgePath <float>(),
                               new EdgePath <float>(0, 100, new EdgePath <float>(1))));
                }
                if (source == 2 && target == 0)
                {
                    return(new Tuple <EdgePath <float>, EdgePath <float> >(
                               new EdgePath <float>(),
                               new EdgePath <float>(0, 100, new EdgePath <float>(2))));
                }
                if (source == 1 && target == 2)
                {
                    return(new Tuple <EdgePath <float>, EdgePath <float> >(
                               new EdgePath <float>(),
                               new EdgePath <float>()));
                }
                if (source == 2 && target == 1)
                {
                    return(new Tuple <EdgePath <float>, EdgePath <float> >(
                               new EdgePath <float>(),
                               new EdgePath <float>()));
                }
                return(null);
            }));
            var priority = priorityCalculator.Calculate(new BitArray32(graph.VertexCount), (i) => null, 0);

            Assert.AreEqual(-2, priority);
        }
コード例 #6
0
ファイル: DykstraTests.cs プロジェクト: zyhong/routing
        public void TestTwoEdgesMiddleHighest()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(2, 1, 100, null);

            // create algorithm and run.
            var algorithm = new Itinero.Algorithms.Contracted.EdgeBased.Dykstra(graph,
                                                                                new EdgePath <float>[] { new EdgePath <float>(0) },
                                                                                (v) => null, true);

            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            EdgePath <float> visit;

            Assert.IsTrue(algorithm.TryGetVisit(0, out visit));
            Assert.AreEqual(0, visit.Weight);
            Assert.AreEqual(0, visit.Vertex);
            Assert.AreEqual(null, visit.From);
            Assert.IsTrue(algorithm.TryGetVisit(1, out visit));
            Assert.AreEqual(100, visit.Weight);
            Assert.AreEqual(1, visit.Vertex);
            Assert.IsNotNull(visit.From);
            Assert.AreEqual(0, visit.From.Vertex);
            Assert.IsFalse(algorithm.TryGetVisit(2, out visit));

            // create algorithm and run.
            algorithm = new Itinero.Algorithms.Contracted.EdgeBased.Dykstra(graph,
                                                                            new EdgePath <float>[] { new EdgePath <float>(0) },
                                                                            (v) => null, false);
            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            Assert.IsTrue(algorithm.TryGetVisit(0, out visit));
            Assert.AreEqual(0, visit.Weight);
            Assert.AreEqual(0, visit.Vertex);
            Assert.AreEqual(null, visit.From);
            Assert.IsTrue(algorithm.TryGetVisit(1, out visit));
            Assert.AreEqual(100, visit.Weight);
            Assert.AreEqual(1, visit.Vertex);
            Assert.IsNotNull(visit.From);
            Assert.AreEqual(0, visit.From.Vertex);
            Assert.IsFalse(algorithm.TryGetVisit(2, out visit));
        }
コード例 #7
0
        public void TestQuadrilateralOneWay()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 2, 100, true);
            graph.AddEdge(2, 0, 100, false);
            graph.AddEdge(0, 3, 10, false);
            graph.AddEdge(3, 0, 10, true);
            graph.AddEdge(1, 2, 1000, false);
            graph.AddEdge(2, 1, 1000, true);
            graph.AddEdge(1, 3, 10000, true);
            graph.AddEdge(3, 1, 10000, false);
            graph.Compress();

            // create a witness calculator and the priority calculator.
            var contractedFlags    = new BitArray32(graph.VertexCount);
            var priorityCalculator = new EdgeDifferencePriorityCalculator(graph,
                                                                          new Itinero.Algorithms.Contracted.EdgeBased.Witness.DykstraWitnessCalculator(10));

            //var priorityCalculator = new EdgeDifferencePriorityCalculator(graph,
            //    new WitnessCalculatorMock(new uint[][]
            //        {
            //            new uint[] { 1, 3, 2, 1 },
            //            new uint[] { 3, 0, 1, 1 }
            //        }));

            Assert.AreEqual(0, priorityCalculator.Calculate(contractedFlags, (i) => null, 0));
            Assert.AreEqual(0, priorityCalculator.Calculate(contractedFlags, (i) => null, 1));
            Assert.AreEqual(0, priorityCalculator.Calculate(contractedFlags, (i) => null, 2));
            Assert.AreEqual(0, priorityCalculator.Calculate(contractedFlags, (i) => null, 3));
        }
コード例 #8
0
        public void TestDoubleContractionOneway()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 0, 100, null);
            graph.AddEdge(1, 2, 100, null);
            graph.AddEdge(2, 1, 100, null);
            graph.AddEdge(1, 3, 10, null);
            graph.AddEdge(3, 1, 10, null);
            graph.AddEdge(3, 2, 10, null);
            graph.AddEdge(2, 3, 10, null);
            graph.Compress();

            // contract graph.
            var priorities = new Dictionary <uint, float>();

            priorities.Add(1, 0);
            priorities.Add(0, 1);
            priorities.Add(2, 2);
            priorities.Add(3, 3);
            var hierarchyBuilder = new HierarchyBuilder(graph,
                                                        new MockPriorityCalculator(priorities),
                                                        new DykstraWitnessCalculator(int.MaxValue), (i) => Enumerable.Empty <uint[]>());

            hierarchyBuilder.Run();

            // edges 1->2 and 2->1 should have been removed.
            var edge = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 3);

            Assert.IsNotNull(edge);
            var edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);

            Assert.AreEqual(110, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(1, edge.GetContracted());

            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 0);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(100, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(Constants.NO_VERTEX, edgeData.ContractedId);

            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 3);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(10, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(Constants.NO_VERTEX, edgeData.ContractedId);

            edge = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 3);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(10, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(Constants.NO_VERTEX, edgeData.ContractedId);
        }
コード例 #9
0
        public void TestOneNeighbour()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 0, 100, null);

            // create a witness calculator and the priority calculator.
            var priorityCalculator = new EdgeDifferencePriorityCalculator(graph,
                                                                          new WitnessCalculatorMock());
            var priority = priorityCalculator.Calculate(new BitArray32(graph.VertexCount), (i) => null, 0);

            Assert.AreEqual(-1, priority);
        }
コード例 #10
0
ファイル: DefaultWeightHandler.cs プロジェクト: amseet/Orion
        /// <summary>
        /// Adds a new edge with the given direction and weight.
        /// </summary>
        public sealed override void AddEdge(DirectedDynamicGraph graph, uint vertex1, uint vertex2, bool?direction, float weight)
        {
            var data = Data.Contracted.Edges.ContractedEdgeDataSerializer.Serialize(
                weight, direction);

            graph.AddEdge(vertex1, vertex2, data);
        }
コード例 #11
0
        public void TestGetSequence2()
        {
            // build graph.
            var graph      = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);
            var e1         = graph.AddEdge(0, 1, 100, null);
            var e2         = graph.AddEdge(1, 2, 100, null);
            var e3         = graph.AddEdge(2, 6, 100, null, 4, new uint[] { 3 }, new uint[] { 5 });
            var e4         = graph.AddEdge(6, 16, 100, null, 11, new uint[] { 7, 8, 9, 10 }, new uint[] { 12, 13, 14, 15 });
            var enumerator = graph.GetEdgeEnumerator();

            // build and test getting sequences from paths.
            var path = new EdgePath <float>(0);
            var s    = path.GetSequence2(enumerator);

            Assert.IsNotNull(s);
            Assert.AreEqual(0, s.Length);

            path = new EdgePath <float>(1, 100, new EdgePath <float>(0));
            s    = path.GetSequence2(enumerator);
            Assert.IsNotNull(s);
            Assert.AreEqual(1, s.Length);
            Assert.AreEqual(0, s[0]);

            path = new EdgePath <float>(2, 200, e2 + 1, new EdgePath <float>(1, 100, new EdgePath <float>(0)));
            s    = path.GetSequence2(enumerator);
            Assert.IsNotNull(s);
            Assert.AreEqual(2, s.Length);
            Assert.AreEqual(0, s[0]);
            Assert.AreEqual(1, s[1]);

            path = new EdgePath <float>(6, 300, e3 + 1, new EdgePath <float>(2, 200, e2 + 1, new EdgePath <float>(1, 100, new EdgePath <float>(0))));
            s    = path.GetSequence2(enumerator);
            Assert.IsNotNull(s);
            Assert.AreEqual(1, s.Length);
            Assert.AreEqual(5, s[0]);

            path = new EdgePath <float>(16, 400, e4 + 1, new EdgePath <float>(6, 300, e3 + 1, new EdgePath <float>(2, 200, e2 + 1, new EdgePath <float>(1, 100, new EdgePath <float>(0)))));
            s    = path.GetSequence2(enumerator);
            Assert.IsNotNull(s);
            Assert.AreEqual(4, s.Length);
            Assert.AreEqual(12, s[0]);
            Assert.AreEqual(13, s[1]);
            Assert.AreEqual(14, s[2]);
            Assert.AreEqual(15, s[3]);
        }
コード例 #12
0
        public void TestDeserialize()
        {
            var graph = new DirectedDynamicGraph(10, 1);

            graph.AddEdge(0, 1, 1);

            // serialize.
            using (var stream = new System.IO.MemoryStream())
            {
                var size = graph.Serialize(stream);

                stream.Seek(0, System.IO.SeekOrigin.Begin);

                var deserializedGraph = DirectedDynamicGraph.Deserialize(stream, DirectedGraphProfile.Aggressive24);
                Assert.AreEqual(size, stream.Position);

                Assert.AreEqual(2, deserializedGraph.VertexCount);
                Assert.AreEqual(1, deserializedGraph.EdgeCount);

                // verify all edges.
                var edges = deserializedGraph.GetEdgeEnumerator(0);
                Assert.AreEqual(1, edges.Count());
                Assert.AreEqual(1, edges.First().Data[0]);
                Assert.AreEqual(1, edges.First().Neighbour);

                edges = deserializedGraph.GetEdgeEnumerator(1);
                Assert.IsFalse(edges.MoveNext());
            }

            graph = new DirectedDynamicGraph(10, 1);
            graph.AddEdge(0, 1, 1);
            graph.AddEdge(0, 2, 2);
            graph.AddEdge(0, 3, 3);
            graph.AddEdge(0, 4, 4);
            graph.AddEdge(5, 1, 5);
            graph.AddEdge(5, 2, 6);
            graph.AddEdge(5, 3, 7);
            graph.AddEdge(5, 4, 8);

            // serialize.
            using (var stream = new System.IO.MemoryStream())
            {
                var size = graph.Serialize(stream);

                stream.Seek(0, System.IO.SeekOrigin.Begin);

                var deserializedGraph = DirectedDynamicGraph.Deserialize(stream, DirectedGraphProfile.Aggressive24);
                Assert.AreEqual(size, stream.Position);

                Assert.AreEqual(6, deserializedGraph.VertexCount);
                Assert.AreEqual(8, deserializedGraph.EdgeCount);
            }
        }
コード例 #13
0
        public void TestTwoEdgesDirectedMiddleHighest()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, true);
            graph.AddEdge(2, 1, 100, false);

            // create algorithm and run.
            var algorithm = new Itinero.Algorithms.Contracted.EdgeBased.BidirectionalDykstra(graph,
                                                                                             new EdgePath <float>[] { new EdgePath <float>(0) }, new EdgePath <float>[] { new EdgePath <float>(2) }, x => null);

            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);
            Assert.AreEqual(1, algorithm.Best);
            EdgePath <float> visit;

            Assert.IsTrue(algorithm.TryGetForwardVisit(0, out visit));
            Assert.AreEqual(0, visit.Weight);
            Assert.AreEqual(0, visit.Vertex);
            Assert.AreEqual(null, visit.From);
            Assert.IsTrue(algorithm.TryGetForwardVisit(1, out visit));
            Assert.AreEqual(100, visit.Weight);
            Assert.AreEqual(1, visit.Vertex);
            Assert.IsNotNull(visit.From);
            Assert.AreEqual(0, visit.From.Vertex);

            Assert.IsTrue(algorithm.TryGetBackwardVisit(2, out visit));
            Assert.AreEqual(0, visit.Weight);
            Assert.AreEqual(2, visit.Vertex);
            Assert.AreEqual(null, visit.From);
            Assert.IsTrue(algorithm.TryGetBackwardVisit(1, out visit));
            Assert.AreEqual(100, visit.Weight);
            Assert.AreEqual(1, visit.Vertex);
            Assert.IsNotNull(visit.From);
            Assert.AreEqual(2, visit.From.Vertex);

            Assert.AreEqual(new List <uint>(new uint[] { 0, 1, 2 }), algorithm.GetPath());
        }
コード例 #14
0
        public void TestSerialize()
        {
            var graph = new DirectedDynamicGraph(10, 1);

            // add and compress.
            graph.AddEdge(0, 1, 1);
            graph.Compress();
            var expectedSize = 1 + 8 + 8 + 8 + 4 +         // the header: version byte two longs representing vertex, edge count and the size of the edge array and one int for minimum edge size.
                               graph.VertexCount * 1 * 4 + // the bytes for the vertex-index: 1 uint.
                               graph.EdgeCount * 2 * 4;    // the bytes for the edges: one edge 2 uint's.

            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
                Assert.AreEqual(expectedSize, stream.Position);
            }

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().Data[0]);
            Assert.AreEqual(1, edges.First().Neighbour);

            graph = new DirectedDynamicGraph(10, 1);

            // add and compress.
            graph.AddEdge(0, 1, 10);
            graph.AddEdge(1, 2, 20);
            graph.AddEdge(2, 3, 30);
            graph.AddEdge(3, 4, 40);
            graph.RemoveEdge(1, 2);
            graph.Compress();
            expectedSize = 1 + 8 + 8 + 8 + 4 +      // the header: version bytes, three longs representing vertex and edge count and the size of the edge array and one int for fixed edge size.
                           graph.VertexCount * 4 +  // the bytes for the vertex-index: 2 uint's.
                           graph.EdgeCount * 2 * 4; // the bytes for the edges: one edge 1 uint.
            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
                Assert.AreEqual(expectedSize, stream.Position);
            }
        }
コード例 #15
0
        public void TestUTurnHandling()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 3, 100, null, 1, null, null);
            graph.AddEdge(3, 0, 100, null, 1, null, null);
            graph.AddEdge(2, 3, 100, null, 1, null, null);
            graph.AddEdge(3, 2, 100, null, 1, null, null);

            // a path from 0->2 is impossible because it would involve a u-turn at vertex 3 resulting in an original path 0->1->3->1->2.
            // same for 2->0

            var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue);

            // calculate witness for weight of 200.
            var forwardWitnesses  = new EdgePath <float> [1];
            var backwardWitnesses = new EdgePath <float> [1];

            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { float.MaxValue }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // build graph.
            graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);
            graph.AddEdge(0, 3, 100, null, 1, null, new uint[] { 100 });
            graph.AddEdge(3, 0, 100, null, 1, new uint[] { 100 }, null);
            graph.AddEdge(2, 3, 100, null, 1, null, new uint[] { 100 });
            graph.AddEdge(3, 2, 100, null, 1, new uint[] { 100 }, null);

            // a path from 0->2 is impossible because it would involve a u-turn at vertex 3 resulting in an original path 0->...->100->3->100->...->2.
            // same for 2->0

            // calculate witness for weight of 200.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { float.MaxValue }),
                                        ref forwardWitnesses, ref backwardWitnesses, uint.MaxValue);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);
        }
コード例 #16
0
        public void TestTwoNeighboursNotifyContracted()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 0, 100, null);
            graph.AddEdge(0, 2, 100, null);
            graph.AddEdge(2, 0, 100, null);

            // create a witness calculator and the priority calculator.
            var contractedFlags = new BitArray32(graph.VertexCount);

            contractedFlags[1] = true;
            var priorityCalculator = new EdgeDifferencePriorityCalculator(graph,
                                                                          new WitnessCalculatorMock());

            priorityCalculator.NotifyContracted(1);
            var priority = priorityCalculator.Calculate(contractedFlags, (i) => null, 0);

            Assert.AreEqual(0, priority);
        }
コード例 #17
0
        public void TestDoubleContraction()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 2, 100, null);
            graph.AddEdge(2, 0, 100, null);
            graph.AddEdge(0, 3, 100, null);
            graph.AddEdge(3, 0, 100, null);
            graph.AddEdge(1, 2, 200, null);
            graph.AddEdge(2, 1, 200, null);
            graph.AddEdge(1, 3, 200, null);
            graph.AddEdge(3, 1, 200, null);
            graph.Compress();

            // contract graph.
            var priorityCalculator = new EdgeDifferencePriorityCalculator(graph, new DykstraWitnessCalculator(int.MaxValue));

            priorityCalculator.DepthFactor      = 0;
            priorityCalculator.ContractedFactor = 0;
            var hierarchyBuilder = new HierarchyBuilder(graph, priorityCalculator,
                                                        new DykstraWitnessCalculator(int.MaxValue), (i) => Enumerable.Empty <uint[]>());

            hierarchyBuilder.Run();

            // check edges.
            var edge = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 2);

            Assert.IsNull(edge);
            edge = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 0);
            Assert.IsNotNull(edge);

            edge = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 3);
            Assert.IsNull(edge);
            edge = graph.GetEdgeEnumerator(3).FirstOrDefault(x => x.Neighbour == 0);
            Assert.IsNotNull(edge);

            edge = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 1);
            Assert.IsNull(edge);
            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 2);
            Assert.IsNotNull(edge);

            edge = graph.GetEdgeEnumerator(3).FirstOrDefault(x => x.Neighbour == 1);
            Assert.IsNull(edge);
            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 3);
            Assert.IsNotNull(edge);
        }
コード例 #18
0
        public void TestAddEdge()
        {
            var graph = new DirectedDynamicGraph();

            graph.AddEdge(1, 2, 0102);

            Assert.AreEqual(1, graph.EdgeCount);
            var enumerator = graph.GetEdgeEnumerator();

            Assert.IsTrue(enumerator.MoveTo(1));
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(0102, enumerator.Data0);

            graph = new DirectedDynamicGraph();
            graph.AddEdge(1, 2, 0102, 010200);

            Assert.AreEqual(1, graph.EdgeCount);
            enumerator = graph.GetEdgeEnumerator();
            Assert.IsTrue(enumerator.MoveTo(1));
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(0102, enumerator.Data0);
            Assert.AreEqual(010200, enumerator.DynamicData[0]);
        }
コード例 #19
0
        public void TestThreeNeighbours()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 0, 100, null);
            graph.AddEdge(0, 2, 100, null);
            graph.AddEdge(2, 0, 100, null);
            graph.AddEdge(0, 3, 100, null);
            graph.AddEdge(3, 0, 100, null);

            // create a witness calculator and the priority calculator.
            var priorityCalculator = new EdgeDifferencePriorityCalculator(graph,
                                                                          new WitnessCalculatorMock((source, target) =>
            {
                return(new Tuple <EdgePath <float>, EdgePath <float> >(
                           new EdgePath <float>(0),
                           new EdgePath <float>(0)));
            }));
            var priority = priorityCalculator.Calculate(new BitArray32(graph.VertexCount), (i) => null, 0);

            Assert.AreEqual(3, priority);
        }
コード例 #20
0
        public void TestPentagonDirected()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, true);
            graph.AddEdge(1, 0, 100, false);
            graph.AddEdge(1, 2, 100, true);
            graph.AddEdge(2, 1, 100, false);
            graph.AddEdge(2, 3, 100, true);
            graph.AddEdge(3, 2, 100, false);
            graph.AddEdge(3, 4, 100, true);
            graph.AddEdge(4, 3, 100, false);
            graph.AddEdge(4, 0, 100, true);
            graph.AddEdge(0, 4, 100, false);
            graph.Compress();

            // contract graph.
            var priorityCalculator = new EdgeDifferencePriorityCalculator(graph, new DykstraWitnessCalculator(int.MaxValue));

            priorityCalculator.ContractedFactor = 0;
            priorityCalculator.DepthFactor      = 0;
            var hierarchyBuilder = new HierarchyBuilder(graph, priorityCalculator,
                                                        new DykstraWitnessCalculator(int.MaxValue), (i) => Enumerable.Empty <uint[]>());

            hierarchyBuilder.Run();

            // check edges.
            var edges01 = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 1);

            Assert.IsNotNull(edges01);
            var edges10 = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 0);

            Assert.IsNull(edges10);

            var edges12 = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 2);

            Assert.IsNull(edges12);
            var edges21 = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 1);

            Assert.IsNotNull(edges21);

            var edges23 = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 3);

            Assert.IsNotNull(edges23);
            var edges32 = graph.GetEdgeEnumerator(3).FirstOrDefault(x => x.Neighbour == 2);

            Assert.IsNull(edges32);

            var edges34 = graph.GetEdgeEnumerator(3).FirstOrDefault(x => x.Neighbour == 4);

            Assert.IsNull(edges34);
            var edges43 = graph.GetEdgeEnumerator(4).FirstOrDefault(x => x.Neighbour == 3);

            Assert.IsNotNull(edges43);

            var edges40 = graph.GetEdgeEnumerator(4).FirstOrDefault(x => x.Neighbour == 0);

            Assert.IsNull(edges40);
            var edges04 = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 4);

            Assert.IsNotNull(edges04);

            var edges41 = graph.GetEdgeEnumerator(4).FirstOrDefault(x => x.Neighbour == 1);

            Assert.IsNotNull(edges41);
            var s1 = edges41.GetSequence1();
            var s2 = edges41.GetSequence2();

            Assert.AreEqual(1, s1.Length);
            Assert.AreEqual(0, s1[0]);
            Assert.AreEqual(1, s2.Length);
            Assert.AreEqual(0, s2[0]);
            var edges14 = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 4);

            Assert.IsNull(edges14);

            var edges31 = graph.GetEdgeEnumerator(3).FirstOrDefault(x => x.Neighbour == 1);

            Assert.IsNotNull(edges31);
            s1 = edges31.GetSequence1();
            s2 = edges31.GetSequence2();
            Assert.AreEqual(1, s1.Length);
            Assert.AreEqual(4, s1[0]);
            Assert.AreEqual(1, s2.Length);
            Assert.AreEqual(0, s2[0]);
            var edges13 = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 3);

            Assert.IsNull(edges13);
        }
コード例 #21
0
        public void TestPentagon()
        {
            var routerDb = new RouterDb();

            routerDb.AddSupportedVehicle(VehicleMock.Car());
            routerDb.Network.AddVertex(0, 0, 0);
            routerDb.Network.AddVertex(1, 1, 1);
            routerDb.Network.AddVertex(2, 2, 2);
            routerDb.Network.AddVertex(3, 3, 3);
            routerDb.Network.AddVertex(4, 4, 4);
            routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });
            routerDb.Network.AddEdge(1, 2, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });
            routerDb.Network.AddEdge(2, 3, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });
            routerDb.Network.AddEdge(3, 4, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });
            routerDb.Network.AddEdge(4, 0, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });

            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null);
            graph.AddEdge(0, 4, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null);
            graph.AddEdge(2, 1, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null);
            graph.AddEdge(2, 3, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null);
            graph.AddEdge(3, 1, 200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null, 2, null, null);
            graph.AddEdge(4, 1, 200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null, 0, null, null);
            graph.AddEdge(4, 3, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null);
            routerDb.AddContracted(VehicleMock.Car().Fastest(), new ContractedDb(graph));

            // create algorithm and run.
            var algorithm = new ManyToManyWeightsBidirectionalDykstra(new Router(routerDb), VehicleMock.Car().Fastest(),
                                                                      new RouterPoint[] {
                routerDb.Network.CreateRouterPointForVertex(0),
                routerDb.Network.CreateRouterPointForVertex(1),
                routerDb.Network.CreateRouterPointForVertex(2),
                routerDb.Network.CreateRouterPointForVertex(3),
                routerDb.Network.CreateRouterPointForVertex(4)
            },
                                                                      new RouterPoint[] {
                routerDb.Network.CreateRouterPointForVertex(0),
                routerDb.Network.CreateRouterPointForVertex(1),
                routerDb.Network.CreateRouterPointForVertex(2),
                routerDb.Network.CreateRouterPointForVertex(3),
                routerDb.Network.CreateRouterPointForVertex(4)
            });

            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            Assert.IsNotNull(algorithm.Weights);
            Assert.AreEqual(5, algorithm.Weights.Length);

            Assert.AreEqual(5, algorithm.Weights[0].Length);
            Assert.AreEqual(5, algorithm.Weights[1].Length);
            Assert.AreEqual(5, algorithm.Weights[2].Length);
            Assert.AreEqual(5, algorithm.Weights[3].Length);
            Assert.AreEqual(5, algorithm.Weights[4].Length);

            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][0], SecondsTolerance);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][1], SecondsTolerance);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][2], SecondsTolerance);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][3], SecondsTolerance);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][4], SecondsTolerance);

            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][0], SecondsTolerance);
            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][1], SecondsTolerance);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][2], SecondsTolerance);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][3], SecondsTolerance);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][4], SecondsTolerance);

            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][0], SecondsTolerance);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][1], SecondsTolerance);
            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][2], SecondsTolerance);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][3], SecondsTolerance);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][4], SecondsTolerance);
        }
コード例 #22
0
        public void TestTwoEdgesLeftMiddleHighest()
        {
            // build graph.
            var oneway = VehicleMock.Car(t => new FactorAndSpeed()
            {
                Value       = VehicleMock.Car().Fastest().FactorAndSpeed(null).Value,
                SpeedFactor = VehicleMock.Car().Fastest().FactorAndSpeed(null).SpeedFactor,
                Direction   = 1
            }).Fastest();
            var routerDb = new RouterDb();

            routerDb.AddSupportedVehicle(oneway.Parent);
            routerDb.Network.AddVertex(0, 0, 0);
            routerDb.Network.AddVertex(1, 1, 1);
            routerDb.Network.AddVertex(2, 2, 2);
            routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });
            routerDb.Network.AddEdge(1, 2, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });

            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(1, 0, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, false);
            graph.AddEdge(2, 1, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, false);
            routerDb.AddContracted(VehicleMock.Car().Fastest(), new ContractedDb(graph));

            // create algorithm and run.
            var algorithm = new ManyToManyWeightsBidirectionalDykstra(new Router(routerDb), oneway,
                                                                      new RouterPoint[] {
                routerDb.Network.CreateRouterPointForVertex(0),
                routerDb.Network.CreateRouterPointForVertex(1),
                routerDb.Network.CreateRouterPointForVertex(2)
            },
                                                                      new RouterPoint[] {
                routerDb.Network.CreateRouterPointForVertex(0),
                routerDb.Network.CreateRouterPointForVertex(1),
                routerDb.Network.CreateRouterPointForVertex(2)
            });

            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            Assert.IsNotNull(algorithm.Weights);
            Assert.AreEqual(3, algorithm.Weights.Length);
            Assert.AreEqual(3, algorithm.Weights[0].Length);
            Assert.AreEqual(3, algorithm.Weights[1].Length);
            Assert.AreEqual(3, algorithm.Weights[2].Length);

            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][0], SecondsTolerance);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][1], SecondsTolerance);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][2], SecondsTolerance);
            Assert.AreEqual(float.MaxValue, algorithm.Weights[1][0]);
            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][1], SecondsTolerance);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][2], SecondsTolerance);
            Assert.AreEqual(float.MaxValue, algorithm.Weights[2][0]);
            Assert.AreEqual(float.MaxValue, algorithm.Weights[2][1]);
            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][2], SecondsTolerance);
        }
コード例 #23
0
        public void TestTwoEdgesMiddleHighest()
        {
            // build graph.
            var routerDb = new RouterDb();

            routerDb.AddSupportedVehicle(VehicleMock.Car());
            routerDb.Network.AddVertex(0, 0, 0);
            routerDb.Network.AddVertex(1, 1, 1);
            routerDb.Network.AddVertex(2, 2, 2);
            routerDb.Network.AddEdge(0, 1, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });
            routerDb.Network.AddEdge(1, 2, new Itinero.Data.Network.Edges.EdgeData()
            {
                Distance = 100,
                Profile  = 0,
                MetaId   = 0
            });

            // build graph.
            var graph = new DirectedDynamicGraph();

            graph.AddEdge(0, 1, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null);
            graph.AddEdge(2, 1, 100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, null);
            routerDb.AddContracted(VehicleMock.Car().Fastest(), new ContractedDb(graph));

            // create algorithm and run.
            var algorithm = new ManyToManyWeightsBidirectionalDykstra(new Router(routerDb), VehicleMock.Car().Fastest(),
                                                                      new RouterPoint[] {
                routerDb.Network.CreateRouterPointForVertex(0),
                routerDb.Network.CreateRouterPointForVertex(1),
                routerDb.Network.CreateRouterPointForVertex(2)
            },
                                                                      new RouterPoint[] {
                routerDb.Network.CreateRouterPointForVertex(0),
                routerDb.Network.CreateRouterPointForVertex(1),
                routerDb.Network.CreateRouterPointForVertex(2)
            });

            algorithm.Run();

            // check results.
            Assert.IsTrue(algorithm.HasRun);
            Assert.IsTrue(algorithm.HasSucceeded);

            Assert.IsNotNull(algorithm.Weights);
            Assert.AreEqual(3, algorithm.Weights.Length);
            Assert.AreEqual(3, algorithm.Weights[0].Length);
            Assert.AreEqual(3, algorithm.Weights[1].Length);
            Assert.AreEqual(3, algorithm.Weights[2].Length);

            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][0], 0.1);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][1], 0.1);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[0][2], 0.1);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][0], 0.1);
            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][1], 0.1);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[1][2], 0.1);
            Assert.AreEqual(200 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][0], 0.1);
            Assert.AreEqual(100 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][1], 0.1);
            Assert.AreEqual(000 * VehicleMock.Car().Fastest().FactorAndSpeed(null).Value, algorithm.Weights[2][2], 0.1);
        }
コード例 #24
0
        public void TestRestrictedNetwork2()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 0, 100, null);
            graph.AddEdge(1, 2, 100, null);
            graph.AddEdge(2, 1, 100, null);
            graph.AddEdge(1, 3, 100, null);
            graph.AddEdge(3, 1, 100, null);
            graph.AddEdge(1, 4, 100, null);
            graph.AddEdge(4, 1, 100, null);
            graph.AddEdge(2, 3, 100, null);
            graph.AddEdge(3, 2, 100, null);
            graph.Compress();

            // contract graph.
            var priorities = new Dictionary <uint, float>();

            priorities.Add(1, 0);
            priorities.Add(0, 1);
            priorities.Add(2, 2);
            priorities.Add(3, 3);
            priorities.Add(4, 4);
            var hierarchyBuilder = new HierarchyBuilder(graph,
                                                        new MockPriorityCalculator(priorities),
                                                        new DykstraWitnessCalculator(int.MaxValue), (i) =>
            {
                if (i == 0 || i == 1 || i == 4)
                {
                    return(new uint[][]
                    {
                        new uint[] { 0, 1, 4 }
                    });
                }
                return(null);
            });

            hierarchyBuilder.Run();

            // check all edges.
            ContractedEdgeData edgeData;
            var edge = graph.GetEdgeEnumerator(0).FirstOrDefault(x =>
            {
                if (x.Neighbour == 4)
                {
                    edgeData = ContractedEdgeDataSerializer.Deserialize(x.Data[0], Constants.NO_VERTEX);
                    return(edgeData.Direction == true);
                }
                return(false);
            });

            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(500, edgeData.Weight);
            Assert.AreEqual(true, edgeData.Direction);
            Assert.AreEqual(1, edge.GetContracted());
            edge = graph.GetEdgeEnumerator(0).FirstOrDefault(x =>
            {
                if (x.Neighbour == 4)
                {
                    edgeData = ContractedEdgeDataSerializer.Deserialize(x.Data[0], Constants.NO_VERTEX);
                    return(edgeData.Direction == false);
                }
                return(false);
            });
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(200, edgeData.Weight);
            Assert.AreEqual(false, edgeData.Direction);
            Assert.AreEqual(1, edge.GetContracted());
            edge = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 2);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(200, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(1, edge.GetContracted());
            edge = graph.GetEdgeEnumerator(0).FirstOrDefault(x => x.Neighbour == 3);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(200, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(1, edge.GetContracted());

            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 0);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(100, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(null, edge.GetContracted());
            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 2);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(100, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(null, edge.GetContracted());
            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 3);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(100, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(null, edge.GetContracted());
            edge = graph.GetEdgeEnumerator(1).FirstOrDefault(x => x.Neighbour == 4);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(100, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(null, edge.GetContracted());

            edge = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 3);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(100, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(null, edge.GetContracted());
            edge = graph.GetEdgeEnumerator(2).FirstOrDefault(x => x.Neighbour == 4);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(200, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(1, edge.GetContracted());

            edge = graph.GetEdgeEnumerator(3).FirstOrDefault(x => x.Neighbour == 4);
            Assert.IsNotNull(edge);
            edgeData = ContractedEdgeDataSerializer.Deserialize(edge.Data[0], Constants.NO_VERTEX);
            Assert.AreEqual(200, edgeData.Weight);
            Assert.AreEqual(null, edgeData.Direction);
            Assert.AreEqual(1, edge.GetContracted());
        }
コード例 #25
0
        public void TestQuadrilateralOneWay()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 2, 100, true);
            graph.AddEdge(2, 0, 100, false);
            graph.AddEdge(0, 3, 10, false);
            graph.AddEdge(3, 0, 10, true);
            graph.AddEdge(1, 2, 1000, false);
            graph.AddEdge(2, 1, 1000, true);
            graph.AddEdge(1, 3, 10000, true);
            graph.AddEdge(3, 1, 10000, false);
            graph.Compress(false);

            var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue);

            // calculate witnesses for 0.
            var forwardWitnesses  = new EdgePath <float> [1];
            var backwardWitnesses = new EdgePath <float> [1];

            witnessCalculator.Calculate(graph, (i) => null, 2, new List <uint>(new uint[] { 3 }), new List <float>(new float[] { 110 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 0);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 0.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 3, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 110 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 0);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 2.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 1100 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 2);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 2.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 1, new List <uint>(new uint[] { 0 }), new List <float>(new float[] { 1100 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 2);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 1.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 3, new List <uint>(new uint[] { 2 }), new List <float>(new float[] { 11000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 1);
            Assert.AreEqual(2, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 1.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 2, new List <uint>(new uint[] { 3 }), new List <float>(new float[] { 11000 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 1);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(3, backwardWitnesses[0].Vertex);

            // calculate witnesses for 3.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 0, new List <uint>(new uint[] { 1 }), new List <float>(new float[] { 10010 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 3);
            Assert.AreEqual(1, forwardWitnesses[0].Vertex);
            Assert.AreEqual(Constants.NO_VERTEX, backwardWitnesses[0].Vertex);

            // calculate witnesses for 3.
            forwardWitnesses  = new EdgePath <float> [1];
            backwardWitnesses = new EdgePath <float> [1];
            witnessCalculator.Calculate(graph, (i) => null, 1, new List <uint>(new uint[] { 0 }), new List <float>(new float[] { 10010 }),
                                        ref forwardWitnesses, ref backwardWitnesses, 3);
            Assert.AreEqual(Constants.NO_VERTEX, forwardWitnesses[0].Vertex);
            Assert.AreEqual(0, backwardWitnesses[0].Vertex);
        }
コード例 #26
0
        public void TestBuildPath()
        {
            // build graph.
            var graph  = new DirectedDynamicGraph(5, 1);
            var edge01 = graph.AddEdge(0, 1, 100, null);
            //graph.AddEdge(1, 0, 101, null);
            //graph.AddEdge(1, 2, 102, null);
            var edge21 = graph.AddEdge(2, 1, 103, null);
            //graph.AddEdge(1, 3, 104, null);
            var edge31 = graph.AddEdge(3, 1, 105, null);
            //graph.AddEdge(1, 4, 106, null);
            var edge41 = graph.AddEdge(4, 1, 107, null);
            var edge23 = graph.AddEdge(2, 3, 108, null);
            //graph.AddEdge(3, 2, 109, null);

            var path = graph.BuildPath(new uint[] { 1 });

            Assert.IsNotNull(path);
            Assert.AreEqual(1, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);

            path = graph.BuildPath(new uint[] { 1, 2 });
            Assert.IsNotNull(path);
            Assert.AreEqual(2, path.Vertex);
            Assert.IsNotNull(path.From);
            Assert.AreEqual(103, path.Weight);
            Assert.AreEqual(-graph.GetOriginal(2, 1).IdDirected(), path.Edge);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(1, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);

            path = graph.BuildPath(new uint[] { 2, 1 }, true);
            Assert.IsNotNull(path);
            Assert.AreEqual(2, path.Vertex);
            Assert.IsNotNull(path.From);
            Assert.AreEqual(103, path.Weight);
            Assert.AreEqual(-graph.GetOriginal(2, 1).IdDirected(), path.Edge);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(1, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);

            path = graph.BuildPath(new uint[] { 2, 1 });
            Assert.IsNotNull(path);
            Assert.AreEqual(1, path.Vertex);
            Assert.IsNotNull(path.From);
            Assert.AreEqual(103, path.Weight);
            Assert.AreEqual(graph.GetOriginal(2, 1).IdDirected(), path.Edge);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(2, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);

            path = graph.BuildPath(new uint[] { 1, 2 }, true);
            Assert.IsNotNull(path);
            Assert.AreEqual(1, path.Vertex);
            Assert.IsNotNull(path.From);
            Assert.AreEqual(103, path.Weight);
            Assert.AreEqual(graph.GetOriginal(2, 1).IdDirected(), path.Edge);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(2, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);

            path = graph.BuildPath(new uint[] { 1, 2 }, true, true);
            Assert.IsNotNull(path);
            Assert.AreEqual(1, path.Vertex);
            Assert.IsNotNull(path.From);
            Assert.AreEqual(103, path.Weight);
            Assert.AreEqual(graph.GetOriginal(2, 1).IdDirected(), path.Edge);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(2, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);
        }
コード例 #27
0
        public void TestAddMultipleEdges()
        {
            // a new graph.
            var graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);

            graph.AddEdge(0, 1, 10, 100);
            graph.AddEdge(1, 0, 10, 100);
            graph.AddEdge(0, 2, 20, 200);
            graph.AddEdge(2, 0, 20, 200);

            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(10, edges.First(x => x.Neighbour == 1).Data0);
            Assert.AreEqual(100, edges.First(x => x.Neighbour == 1).DynamicData[0]);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 2).Data[0]);
            Assert.AreEqual(200, edges.First(x => x.Neighbour == 2).DynamicData[0]);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(10, edges.First(x => x.Neighbour == 0).Data[0]);
            Assert.AreEqual(100, edges.First(x => x.Neighbour == 0).DynamicData[0]);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 0).Data[0]);
            Assert.AreEqual(200, edges.First(x => x.Neighbour == 0).DynamicData[0]);

            // a new graph.
            graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);
            graph.AddEdge(0, 1, 10, 100);
            graph.AddEdge(0, 2, 20, 200);
            graph.AddEdge(1, 0, 10, 100);
            graph.AddEdge(2, 0, 20, 200);

            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(10, edges.First(x => x.Neighbour == 1).Data0);
            Assert.AreEqual(100, edges.First(x => x.Neighbour == 1).DynamicData[0]);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 2).Data[0]);
            Assert.AreEqual(200, edges.First(x => x.Neighbour == 2).DynamicData[0]);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(10, edges.First(x => x.Neighbour == 0).Data[0]);
            Assert.AreEqual(100, edges.First(x => x.Neighbour == 0).DynamicData[0]);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 0).Data[0]);
            Assert.AreEqual(200, edges.First(x => x.Neighbour == 0).DynamicData[0]);

            // a new graph.
            graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);
            graph.AddEdge(0, 1, 10);
            graph.AddEdge(1, 0, 10);
            graph.AddEdge(0, 2, 20);
            graph.AddEdge(2, 0, 20);

            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(10, edges.First(x => x.Neighbour == 1).Data0);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 2).Data[0]);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(10, edges.First(x => x.Neighbour == 0).Data[0]);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 0).Data[0]);

            // a new graph.
            graph = new DirectedDynamicGraph(ContractedEdgeDataSerializer.DynamicFixedSize);
            graph.AddEdge(0, 1, 10);
            graph.AddEdge(0, 2, 20);
            graph.AddEdge(1, 0, 10);
            graph.AddEdge(2, 0, 20);

            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(10, edges.First(x => x.Neighbour == 1).Data0);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 2).Data[0]);

            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(10, edges.First(x => x.Neighbour == 0).Data[0]);

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(20, edges.First(x => x.Neighbour == 0).Data[0]);
        }
コード例 #28
0
        public void TestAddSmallNetwork1AndContract()
        {
            // build graph.
            var graph = new DirectedDynamicGraph(5, 1);

            graph.AddEdge(0, 1, 100, null);
            graph.AddEdge(1, 0, 101, null);
            graph.AddEdge(1, 2, 102, null);
            graph.AddEdge(2, 1, 103, null);
            graph.AddEdge(1, 3, 104, null);
            graph.AddEdge(3, 1, 105, null);
            graph.AddEdge(1, 4, 106, null);
            graph.AddEdge(4, 1, 107, null);
            graph.AddEdge(2, 3, 108, null);
            graph.AddEdge(3, 2, 109, null);

            Assert.AreEqual(5, graph.VertexCount);

            // verify all edges for 0.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().Neighbour);
            Assert.AreEqual(null, edges.First().Direction());
            Assert.AreEqual(100, edges.First().Weight());

            // verify all edges for 1.
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(4, edges.Count());
            var edge = edges.First(e => e.Neighbour == 0);

            Assert.AreEqual(0, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(101, edge.Weight());
            edge = edges.First(e => e.Neighbour == 0);
            Assert.AreEqual(0, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(101, edge.Weight());
            edge = edges.First(e => e.Neighbour == 2);
            Assert.AreEqual(2, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(102, edge.Weight());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(104, edge.Weight());
            edge = edges.First(e => e.Neighbour == 4);
            Assert.AreEqual(4, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(106, edge.Weight());

            // verify all edges for 2.
            edges = graph.GetEdgeEnumerator(2);
            edge  = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(103, edge.Weight());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(108, edge.Weight());

            // verify all edges for 3.
            edges = graph.GetEdgeEnumerator(3);
            edge  = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(105, edge.Weight());
            edge = edges.First(e => e.Neighbour == 2);
            Assert.AreEqual(2, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(109, edge.Weight());

            // verify all edges for 4.
            edges = graph.GetEdgeEnumerator(4);
            edge  = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(107, edge.Weight());

            // remove 3->2 and 1->2.
            graph.RemoveEdge(3, 2);
            graph.RemoveEdge(1, 2);

            // verify all edges for 0.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().Neighbour);
            Assert.AreEqual(null, edges.First().Direction());
            Assert.AreEqual(100, edges.First().Weight());

            // verify all edges for 1.
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(3, edges.Count());
            edge = edges.First(e => e.Neighbour == 0);
            Assert.AreEqual(0, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(101, edge.Weight());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(104, edge.Weight());
            edge = edges.First(e => e.Neighbour == 4);
            Assert.AreEqual(4, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(106, edge.Weight());

            // verify all edges for 2.
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(2, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(103, edge.Weight());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(108, edge.Weight());

            // verify all edges for 3.
            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(105, edge.Weight());

            // verify all edges for 4.
            edges = graph.GetEdgeEnumerator(4);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(107, edge.Weight());

            // remove 1->0.
            graph.RemoveEdge(1, 0);

            // verify all edges for 0.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().Neighbour);
            Assert.AreEqual(null, edges.First().Direction());
            Assert.AreEqual(100, edges.First().Weight());

            // verify all edges for 1.
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(2, edges.Count());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(104, edge.Weight());
            edge = edges.First(e => e.Neighbour == 4);
            Assert.AreEqual(4, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(106, edge.Weight());

            // verify all edges for 2.
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(2, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(103, edge.Weight());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(108, edge.Weight());

            // verify all edges for 3.
            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(105, edge.Weight());

            // verify all edges for 4.
            edges = graph.GetEdgeEnumerator(4);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(107, edge.Weight());

            // remove 1->4.
            graph.RemoveEdge(1, 4);

            // verify all edges for 0.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().Neighbour);
            Assert.AreEqual(null, edges.First().Direction());
            Assert.AreEqual(100, edges.First().Weight());

            // verify all edges for 1.
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(104, edge.Weight());

            // verify all edges for 2.
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(2, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(103, edge.Weight());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(108, edge.Weight());

            // verify all edges for 3.
            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(105, edge.Weight());

            // verify all edges for 4.
            edges = graph.GetEdgeEnumerator(4);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(107, edge.Weight());

            // remove 1->3.
            graph.RemoveEdge(1, 3);

            // verify all edges for 0.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().Neighbour);
            Assert.AreEqual(null, edges.First().Direction());
            Assert.AreEqual(100, edges.First().Weight());

            // verify all edges for 1.
            edges = graph.GetEdgeEnumerator(1);
            Assert.AreEqual(0, edges.Count());

            // verify all edges for 2.
            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(2, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(103, edge.Weight());
            edge = edges.First(e => e.Neighbour == 3);
            Assert.AreEqual(3, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(108, edge.Weight());

            // verify all edges for 3.
            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(105, edge.Weight());

            // verify all edges for 4.
            edges = graph.GetEdgeEnumerator(4);
            Assert.AreEqual(1, edges.Count());
            edge = edges.First(e => e.Neighbour == 1);
            Assert.AreEqual(1, edge.Neighbour);
            Assert.AreEqual(null, edge.Direction());
            Assert.AreEqual(107, edge.Weight());
        }
コード例 #29
0
        public void TestCompress()
        {
            var graph = new DirectedDynamicGraph(10, 1);

            // add and compress.
            graph.AddEdge(0, 1, 1);
            graph.Compress();

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(1, edges.First().Data[0]);
            Assert.AreEqual(1, edges.First().Neighbour);

            graph = new DirectedDynamicGraph(10, 1);

            // add and compress.
            graph.AddEdge(0, 1, 10);
            graph.AddEdge(1, 2, 20);
            graph.AddEdge(2, 3, 30);
            graph.AddEdge(3, 4, 40);
            graph.RemoveEdge(1, 2);

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(1, edges.First().Neighbour);

            edges = graph.GetEdgeEnumerator(1);
            Assert.IsFalse(edges.MoveNext());

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(30, edges.First().Data[0]);
            Assert.AreEqual(3, edges.First().Neighbour);

            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(40, edges.First().Data[0]);
            Assert.AreEqual(4, edges.First().Neighbour);

            // compress.
            graph.Compress();

            // verify all edges.
            edges = graph.GetEdgeEnumerator(0);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(10, edges.First().Data[0]);
            Assert.AreEqual(1, edges.First().Neighbour);

            edges = graph.GetEdgeEnumerator(1);
            Assert.IsFalse(edges.MoveNext());

            edges = graph.GetEdgeEnumerator(2);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(30, edges.First().Data[0]);
            Assert.AreEqual(3, edges.First().Neighbour);

            edges = graph.GetEdgeEnumerator(3);
            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(40, edges.First().Data[0]);
            Assert.AreEqual(4, edges.First().Neighbour);
        }