public override bool Find(DirectionGraph graph, Node start, Node end)
    {
        priorityQueue = new FastPriorityQueue <Node>(graph.VerticeCount());
        searchSteps   = new Queue <Node>();

        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();

        costForStartToTarget        = new Dictionary <Node, int>();
        costForStartToTarget[start] = 0;

        priorityQueue.Enqueue(start, 0);
        while (priorityQueue.Count > 0)
        {
            Node min = priorityQueue.Dequeue();
            if (min == end)
            {
                watch.Stop();
                executionTimes = watch.Elapsed.TotalMilliseconds;
                CalculateCostAndGeneratePath(start, end, graph);
                return(true);
            }
            List <DirectionEdge> minNeighbors = graph.GetNeighborEdge(min);
            int costForStartToMin             = costForStartToTarget[min];
            for (int i = 0, count = minNeighbors.Count; i < count; i++)
            {
                Node neighbor = minNeighbors[i].to;
                int  costForStartToNeighbor = int.MaxValue;
                if (costForStartToTarget.ContainsKey(neighbor))
                {
                    costForStartToNeighbor = costForStartToTarget[neighbor];
                }
                int newCostSoFar = costForStartToMin + minNeighbors[i].weight;
                if (newCostSoFar < costForStartToNeighbor)
                {
                    costForStartToTarget[neighbor] = newCostSoFar;
                    nodesComeFrom[neighbor]        = min;
                    if (priorityQueue.Contains(neighbor))
                    {
                        priorityQueue.UpdatePriority(neighbor, newCostSoFar);
                    }
                    else
                    {
                        priorityQueue.Enqueue(neighbor, newCostSoFar);
                    }
                }
            }
            if (min != start)
            {
                searchSteps.Enqueue(min);
            }
        }
        watch.Stop();
        executionTimes = watch.Elapsed.TotalMilliseconds;
        searchSteps.Clear();
        return(false);
    }
Exemplo n.º 2
0
    public override bool Find(DirectionGraph graph, Node start, Node end)
    {
        priorityQueue = new FastPriorityQueue <Node>(graph.VerticeCount());
        searchSteps   = new Queue <Node>();

        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();

        priorityQueue.Enqueue(start, 0);
        while (priorityQueue.Count > 0)
        {
            Node min = priorityQueue.Dequeue();
            if (min == end)
            {
                watch.Stop();
                executionTimes = watch.Elapsed.TotalMilliseconds;
                CalculateCostAndGeneratePath(start, end, graph);
                return(true);
            }
            List <DirectionEdge> minNeighbors = graph.GetNeighborEdge(min);
            for (int i = 0, count = minNeighbors.Count; i < count; i++)
            {
                Node neighbor = minNeighbors[i].to;
                if (neighbor.visited)
                {
                    continue;
                }
                neighbor.visited = true;
                int newCostSoFar = Heuristic(neighbor, end);//Mathf.Abs(neighbor.coord_x - end.coord_x) + Mathf.Abs(neighbor.coord_y - end.coord_y);// costForStartToMin + minNeighbors[i].weight;
                nodesComeFrom[neighbor] = min;
                priorityQueue.Enqueue(neighbor, newCostSoFar);
            }
            if (min != start)
            {
                searchSteps.Enqueue(min);
            }
        }
        watch.Stop();
        executionTimes = watch.Elapsed.TotalMilliseconds;
        searchSteps.Clear();
        return(false);
    }
 public override bool Find(DirectionGraph graph, Node start, Node end)
 {
     stack = new Stack <Node>(graph.VerticeCount());
     System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
     watch.Start();
     start.visited = true;
     stack.Push(start);
     while (stack.Count > 0)
     {
         Node min = stack.Pop();
         if (min == end)
         {
             watch.Stop();
             executionTimes = watch.Elapsed.TotalMilliseconds;
             CalculateCostAndGeneratePath(start, end, graph);
             return(true);
         }
         List <DirectionEdge> minNeighbors = graph.GetNeighborEdge(min);
         for (int i = 0, count = minNeighbors.Count; i < count; i++)
         {
             Node neighbor = minNeighbors[i].to;
             if (neighbor.visited)
             {
                 continue;
             }
             stack.Push(neighbor);
             neighbor.visited        = true;
             nodesComeFrom[neighbor] = min;
         }
         if (min != start)
         {
             searchSteps.Enqueue(min);
         }
     }
     watch.Stop();
     executionTimes = watch.Elapsed.TotalMilliseconds;
     searchSteps.Clear();
     return(false);
 }