コード例 #1
0
ファイル: HeuristicsHelper.cs プロジェクト: hugodahl/fast
        public static Dictionary <ulong, double> BellmanFordDistances(
            IWeightedGraph <FindingDirectionsState, double> graph,
            FindingDirectionsState landmark
            )
        {
            // TODO: move this to fast.search
            // https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
            // this is a version of Bellman-Ford that assumes no prior knowledge
            // of the full node set and only tracks reachable nodes, anything not
            // in the result of this function should be considered infinite
            // we also only care about the distances here and do not maintain prev
            var result  = new Dictionary <ulong, double>();
            var openSet = new OpenSet <double, FindingDirectionsState>();

            openSet.PushOrImprove(0d, landmark);
            while (!openSet.IsEmpty)
            {
                var dist = openSet.MinCost;
                var node = openSet.PopMin();
                if (result.ContainsKey(node.NodeId))
                {
                    continue;
                }
                result.Add(node.NodeId, dist);
                var neighbors = graph.GetNeighbors(node);
                foreach (var neighbor in neighbors)
                {
                    openSet.PushOrImprove(dist + graph.GetEdgeWeight(node, neighbor), neighbor);
                }
            }
            return(result);
        }
コード例 #2
0
ファイル: OpenSetTests.cs プロジェクト: hugodahl/fast
        public void TestSorting10()
        {
            var openSet = new OpenSet <int, int>();
            var values  = new int[] { 9, 2, 3, 8, 0, 4, 7, 5, 6, 1 };

            for (int j = 0; j < 2; j++)
            {
                foreach (var i in values)
                {
                    openSet.PushOrImprove(i, i);
                }
            }
            int nextValue = 0;

            while (!openSet.IsEmpty)
            {
                var val = openSet.PopMin();
                if (val != nextValue)
                {
                    break;
                }
                nextValue += 1;
            }
            Assert.Equal(nextValue, values.Length);
        }
コード例 #3
0
ファイル: OpenSetTests.cs プロジェクト: hugodahl/fast
        public void PopRemovesMin()
        {
            var openSet = new OpenSet <int, int>();

            openSet.PushOrImprove(5, 99);
            openSet.PushOrImprove(8, 33);
            int minValue = openSet.PopMin();

            Assert.Equal(99, minValue);
            Assert.Equal(1, openSet.Size);
            Assert.Equal(8, openSet.MinCost);
        }
コード例 #4
0
ファイル: OpenSetTests.cs プロジェクト: hugodahl/fast
        public void PoppingEmptySetFails()
        {
            var openSet = new OpenSet <int, int>();

            try
            {
                openSet.PopMin();
                Assert.True(false, "PopMin from an empty set did not throw InvalidOperationException.");
            }
            catch (InvalidOperationException)
            {
                Assert.True(true);
            }
        }