Ejemplo n.º 1
0
        public void TestDijkstra()
        {
            // 0 -- 1 -- 2 -- 3
            //  \        |   /
            //    \      | /
            //      4 -- 5
            bool[,] graph =
            {
                { false, true,  false, false, true,  false },
                { true,  false, true,  false, false, false },
                { false, true,  false, true,  false, true  },
                { false, false, true,  false, false, true  },
                { true,  false, false, false, false, true  },
                { false, false, true,  true,  true,  false },
            };
            var searchGraph = SearchGraphFromData(graph);
            var graphNodes  = GetGraphNodesIdIndex(searchGraph);

            var distanceLookup = new DistanceLookup(graphNodes.Values.ToList());

            Assert.AreEqual((uint)0, distanceLookup[graphNodes[0].DistancesIndex, graphNodes[0].DistancesIndex],
                            "Failed 0 distance test");
            Assert.AreEqual((uint)2, distanceLookup[graphNodes[0].DistancesIndex, graphNodes[5].DistancesIndex],
                            "Wrong distance");
            Assert.AreEqual((uint)3, distanceLookup[graphNodes[0].DistancesIndex, graphNodes[3].DistancesIndex],
                            "Wrong distance");
        }
Ejemplo n.º 2
0
        public void DijkstraUnconnected()
        {
            // 0 -- 1    2 -- 3
            //           |   /
            //           | /
            //      4 -- 5
            bool[,] graph =
            {
                { false, true,  false, false, false, false },
                { true,  false, false, false, false, false },
                { false, false, false, true,  false, true  },
                { false, false, true,  false, false, true  },
                { false, false, false, false, false, true  },
                { false, false, true,  true,  true,  false },
            };
            var searchGraph = SearchGraphFromData(graph);
            var graphNodes  = GetGraphNodesIdIndex(searchGraph);
            var mstNodes    = new List <GraphNode> {
                graphNodes[0], graphNodes[2], graphNodes[4], graphNodes[3]
            };

            var distances = new DistanceLookup(graphNodes.Values.ToArray());

            Assert.IsNull(distances.GetShortestPath(mstNodes[0].DistancesIndex, mstNodes[3].DistancesIndex));
        }
Ejemplo n.º 3
0
 public SteinerPreprocessorResult(
     IReadOnlyList <GraphNode> fixedTargetNodes, GraphNode startNode, IReadOnlyList <GraphNode> remainingNodes,
     DistanceLookup distanceLookup, ShortestPathLookup shortestPathLookup)
 {
     FixedTargetNodes   = fixedTargetNodes;
     StartNode          = startNode;
     RemainingNodes     = remainingNodes;
     DistanceLookup     = distanceLookup;
     ShortestPathLookup = shortestPathLookup;
 }
Ejemplo n.º 4
0
        public void TestMST()
        {
            // 0 -- 1 -- 2 -- 3
            //  \        |   /
            //    \      | /
            //      4 -- 5 -- 6 -- 7
            bool[,] graph =
            {
                { false, true,  false, false, true,  false, false, false },
                { true,  false, true,  false, false, false, false, false },
                { false, true,  false, true,  false, true,  false, false },
                { false, false, true,  false, false, true,  false, false },
                { true,  false, false, false, false, true,  false, false },
                { false, false, true,  true,  true,  false, true,  false },
                { false, false, false, false, false, true,  false, true  },
                { false, false, false, false, false, false, true,  false },
            };
            var searchGraph = SearchGraphFromData(graph);
            var graphNodes  = GetGraphNodesIdIndex(searchGraph);
            var mstNodes    = new List <GraphNode>
            {
                graphNodes[3], graphNodes[5], graphNodes[7], graphNodes[0]
            };
            var distances = new DistanceLookup(mstNodes);

            var mst = new MinimalSpanningTree(mstNodes.Select(n => n.DistancesIndex).ToList(), distances);

            mst.Span(graphNodes[0].DistancesIndex);

            Assert.AreEqual(3, mst.SpanningEdges.Count, "Wrong amount of spanning edges");
            var goalEdges = new[]
            {
                new[] { 0, 5 }, new[] { 5, 3 }, new[] { 5, 7 }
            };

            foreach (var edge in goalEdges)
            {
                Assert.AreEqual(1,
                                mst.SpanningEdges.Select(
                                    e => new Tuple <ushort, ushort>(distances.IndexToNode(e.Inside).Id,
                                                                    distances.IndexToNode(e.Outside).Id)).Count(
                                    t =>
                                    (t.Item1 == edge[0] && t.Item2 == edge[1]) ||
                                    (t.Item1 == edge[1] && t.Item2 == edge[0])),
                                "Edge " + edge + " not contained exactly once.");
            }
        }
 /// <summary>
 ///     Instantiates a new MinimalSpanningTree.
 /// </summary>
 /// <param name="mstNodes">The GraphNodes that should be spanned. (not null)</param>
 /// <param name="distances">The DistanceLookup used as cache.</param>
 public MinimalSpanningTree(IReadOnlyList <int> mstNodes, DistanceLookup distances)
 {
     _mstNodes  = mstNodes ?? throw new ArgumentNullException(nameof(mstNodes));
     _distances = distances;
 }