Пример #1
0
        /// <summary>
        /// Finds the shortest path between the specified nodes and returns it as a list of nodes.
        /// </summary>
        /// <param name="start">The starting node for the path</param>
        /// <param name="end">The ending node for the path</param>
        /// <returns>The path, represented as a list of nodes beginning with start and ending with end.</returns>
        public List <Node> FindPath(Node start, Node end)
        {
            BinaryHeap path = new BinaryHeap(Nodes.Length);

            foreach (Node n in Nodes)
            {
                n.NodeCost    = double.PositiveInfinity;
                n.Predecessor = null;
            }

            start.NodeCost = 0;

            foreach (Node n in Nodes)
            {
                path.Add(n, n.NodeCost);
            }

            Node   j, k;
            double newCost;

            while (path.Count != 0)
            {
                j = path.ExtractMin();

                if (j == end)
                {
                    List <Node> Path = new List <Node>();
                    while (j != null)
                    {
                        Path.Add(j);
                        j = j.Predecessor;
                    }

                    Path.Reverse();
                    return(Path);
                }

                foreach (UndirectedEdge e in j.Edges)
                {
                    k = e.OtherNode(j);

                    newCost = j.NodeCost + e.Cost;
                    if (newCost < k.NodeCost)
                    {
                        path.DecreasePriority(k, newCost);
                        k.NodeCost    = newCost;
                        k.Predecessor = j;
                    }
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Finds the shortest path between the specified nodes and returns it as a list of nodes.
        /// </summary>
        /// <param name="start">The starting node for the path</param>
        /// <param name="end">The ending node for the path</param>
        /// <returns>The path, represented as a list of nodes beginning with start and ending with end.</returns>
        public List <Node> FindPath(Node start, Node end)
        {
            BinaryHeap pq = new BinaryHeap(Nodes.Length);

            foreach (Node n in Nodes)
            {
                if (n == start)
                {
                    n.NodeCost = 0;
                }
                else
                {
                    n.NodeCost = Double.PositiveInfinity;
                }
                n.Predecessor = null;
                pq.Add(n, n.NodeCost);
            }
            while (pq.Count != 0)
            {
                Node n = pq.ExtractMin();
                if (n == end)
                {
                    break;
                }
                foreach (UndirectedEdge v in n.Edges)
                {
                    Node temp;
                    if (v.A == n)
                    {
                        temp = v.B;
                    }
                    else
                    {
                        temp = v.A;
                    }
                    double w       = v.Cost;
                    double newCost = w + n.NodeCost;
                    if (newCost < temp.NodeCost)
                    {
                        pq.DecreasePriority(temp, newCost);
                        temp.NodeCost    = newCost;
                        temp.Predecessor = n;
                    }
                }
            }
            Stack <Node> s = new Stack <Node>();
            Node         t = end;

            s.Push(t);
            while (t.Predecessor != null)
            {
                s.Push(t.Predecessor);
                t = t.Predecessor;
            }
            List <Node> l = new List <Node>();

            while (s.Count != 0)
            {
                l.Add(s.Pop());
            }
            return(l);
        }