Ejemplo n.º 1
0
        // Dijkstra's algorithm.
        private bool search()
        {
            // KeyHeap is used as a heap that sorts indices
            // based on the elements at their locations in
            // accumulativeWeights.
            KeyHeap<double> cheapestNodesHeap =
                new KeyHeap<double>(
                    HeapSorting.Min,
                    accumulativeWeights,
                    numNodes
                );

            cheapestNodesHeap.Insert(src);
            while (!cheapestNodesHeap.IsEmpty)
            {
                int cheapestNode = cheapestNodesHeap.Remove();
                shortestPathTree[cheapestNode] = edgeFrontier[cheapestNode];

                if (cheapestNode == tgt)
                    return true;

                foreach (Edge e in g.EdgesFromNode(cheapestNode))
                {
                    double weight = accumulativeWeights[cheapestNode] + e.Weight;

                    if (edgeFrontier[e.NodeTo] == null)
                    {
                        accumulativeWeights[e.NodeTo] = weight;
                        cheapestNodesHeap.Insert(e.NodeTo);
                        edgeFrontier[e.NodeTo] = e;
                    }
                    else if (weight < accumulativeWeights[e.NodeTo] && shortestPathTree[e.NodeTo] == null)
                    {
                        accumulativeWeights[e.NodeTo] = weight;
                        cheapestNodesHeap.Reorder(e.NodeTo);
                        edgeFrontier[e.NodeTo] = e;
                    }
                }
            }

            // If no target was specified (or target did not exist) search
            // returns false. The instance still holds valuable info though
            // in the shortest path tree and the accumulative weights.
            return false;
        }
Ejemplo n.º 2
0
        // The A* search.
        private bool search()
        {
            // KeyHeap is used as a heap that sorts indices
            // based on the elements at their locations in
            // weightsPlusHeuristic.
            KeyHeap<double> cheapestNodesHeap =
                new KeyHeap<double>(
                    HeapSorting.Min,
                    weightsPlusHeuristic,
                    numNodes
                );

            cheapestNodesHeap.Insert(src);
            while (!cheapestNodesHeap.IsEmpty)
            {
                int cheapestNode = cheapestNodesHeap.Remove();
                shortestPathTree[cheapestNode] = edgeFrontier[cheapestNode];

                if (cheapestNode == tgt)
                    return true;

                foreach (Edge e in g.EdgesFromNode(cheapestNode))
                {
                    double actualWeight = weights[cheapestNode] + e.Weight;
                    double heuristicWeight = actualWeight + heuristic(g, e.NodeTo, tgt);

                    if (edgeFrontier[e.NodeTo] == null)
                    {
                        weights[e.NodeTo] = actualWeight;
                        weightsPlusHeuristic[e.NodeTo] = heuristicWeight; // Heuristic weight, not actual!
                        cheapestNodesHeap.Insert(e.NodeTo);
                        edgeFrontier[e.NodeTo] = e;
                    }
                    else if (actualWeight < weights[e.NodeTo] && shortestPathTree[e.NodeTo] == null)
                    {
                        weights[e.NodeTo] = actualWeight;
                        weightsPlusHeuristic[e.NodeTo] = heuristicWeight; // Heuristic weight, not actual!
                        cheapestNodesHeap.Reorder(e.NodeTo);
                        edgeFrontier[e.NodeTo] = e;
                    }
                }
            }

            return false;
        }