Пример #1
0
        public static List <SortedSetNode> ExtractSolution(SortedSetNode[] results)
        {
            List <SortedSetNode> Route = new List <SortedSetNode>();


            SortedSetNode         CurrentNode   = results[DestinationID]; // the virtual end h7otha fl currentt to dsply
            Stack <SortedSetNode> ReversedRoute = new Stack <SortedSetNode>();

            ReversedRoute.Push(CurrentNode);

            while (CurrentNode.Parent != null)
            {
                CurrentNode = results[CurrentNode.Parent.ID];     // basawy nfsy bl ably
                ReversedRoute.Push(CurrentNode);
            }

            while (ReversedRoute.Count > 0)     // bamshy 3l stack
            {
                Route.Add(ReversedRoute.Pop());
            }

            return(Route);
        }
Пример #2
0
        public static SortedSetNode[] Dijkstra(Node CurrentNode)                    // 3 thngs nedded
        {
            SortedSetNode[]           results    = new SortedSetNode[Nodes.Count];  //1.array of obj from class ssn
            SortedSet <SortedSetNode> NextLowest = new SortedSet <SortedSetNode>(); // forimg ssn frm class

            bool[] visited = new bool[Nodes.Count];


            NextLowest.Add(new SortedSetNode(CurrentNode.ID, 0, 0, null));

            results[CurrentNode.ID] = new SortedSetNode(CurrentNode.ID, 0, 0, null);     //id,c,d,p

            int NumberOfNodes = Nodes.Count;

            while (NumberOfNodes != 0)                      /////////////////////////////////////////////
            {
                CurrentNode = Nodes[NextLowest.First().ID]; // hshof el 3ndo  asghr w aro7 l node-o
                NumberOfNodes--;

                if (CurrentNode.ID == DestinationID)
                {
                    return(results);
                }


                NextLowest.Remove(NextLowest.First());
                visited[CurrentNode.ID] = true;     // hashel l node mn l visited dic

                int EdgeCount = Edges[CurrentNode.ID].Count;

                for (int i = 0; i < EdgeCount; i++)           // looping on all neighbours
                {
                    if (!visited[Edges[CurrentNode.ID][i].NeighbourID])
                    {
                        double CurrentNodeTime     = results[CurrentNode.ID].weight;                                 // node 2 time
                        double CurrentNodeDistance = results[CurrentNode.ID].distance;

                        int NeighbourID = Edges[CurrentNode.ID][i].NeighbourID;

                        double NeighbourEdgeTime     = Edges[CurrentNode.ID][i].time;                // edge between
                        double NeighbourEdgeDistance = Edges[CurrentNode.ID][i].distance;

                        double NeighbourNodeTime;

                        if (results[NeighbourID] == null)
                        {
                            NeighbourNodeTime = Int32.MaxValue;
                        }
                        else
                        {
                            NeighbourNodeTime = results[NeighbourID].weight;
                        }

                        if (NeighbourEdgeTime + CurrentNodeTime < NeighbourNodeTime)
                        {
                            SortedSetNode NewNode = new SortedSetNode(NeighbourID, NeighbourEdgeTime + CurrentNodeTime, NeighbourEdgeDistance + CurrentNodeDistance, Nodes[CurrentNode.ID]);      // updating the shortest path results for node 3
                            SortedSetNode OldNode = results[NeighbourID];
                            results[NeighbourID] = NewNode;

                            NextLowest.Remove(OldNode);
                            NextLowest.Add(NewNode);
                        }
                    }
                }
            }

            return(results);
        }