Exemple #1
0
        static void Main(string[] args)
        {
            var graph = new Graph <int, string>();

            graph.AddNode(1);
            graph.AddNode(2);

            graph.Connect(0, 1, 5, "some custom information in edge"); //First node has key equal 0

            var dijkstra = new Dijkstra <int, string>(graph);
            IShortestPathResult result = dijkstra.Process(0, 1); //result contains the shortest path

            var path = result.GetPath();

            graph.Reset();

            var bfs = new BfsParallel <int, string>(graph);

            IShortestPathResult bfsResult = bfs.Process(0, 1);

            var bfsPath = bfsResult.GetPath();

            if (!bfsPath.SequenceEqual(path))
            {
                throw new Exception("The path should be the same.");
            }
        }
Exemple #2
0
        static List <Position> findP(string key)
        {
            lock (_locker)
            {
                initG();
                updateG();

                List <Position> path = null;
                if (self.Bases != null && self.Bases.Count > 0 &&
                    self.Bases[0] != null)
                {
                    int from = _nodes[self.Bases[0].ToString()];
                    int to   = _nodes[key];
                    Dijkstra <string, string> _dijkstra = new Dijkstra <string, string>(_graph);
                    IShortestPathResult       result    = _dijkstra.Process((uint)from, (uint)to); //result contains the shortest path

                    if (result != null && result.IsFounded)
                    {
                        var routes = result.GetPath();
                        path = new List <Position>();
                        foreach (var route in routes)
                        {
                            Position pos = _nodes2[(int)route];
                            path.Add(pos);
                        }
                    }
                }

                return(path);
            }
        }
Exemple #3
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="source"></param>
    /// <param name="destination"></param>
    /// <param name="dijkstra"></param>
    /// <returns></returns>
    public IEnumerable <uint> GetShortestPath(string source, string destination, Dijkstra <int, string> dijkstra)
    {
        IShortestPathResult iShortestPathResult = dijkstra.Process(Convert.ToUInt32(routeNodeMapIDDict[source]), Convert.ToUInt32(routeNodeMapIDDict[destination]));
        var shortestPathResult = iShortestPathResult.GetPath();

        return(shortestPathResult);
    }
        protected Road MakeCompositeRoad(Graph <Node, string> graph, IShortestPathResult result, TransportStats transportStat)
        {
            var roads = new RangeMap <Road>(0, result.Distance);

            Node GetNodeByIndex(uint index) => graph[index].Item;

            var optimalPaths = result.GetPath().ToArray();

            if (optimalPaths.Length <= 1)
            {
                throw new Exception($"Route for {transportStat}  not found");
            }
            roads[0] =
                RoadByFromTo[
                    transportStat.From
                    + "-"
                    + GetNodeByIndex(optimalPaths[1]).Name
                ];
            var prev = roads[0];

            for (var i = 1; i < optimalPaths.Length; i++)
            {
                roads[(int)prev.Length] = RoadByFromTo[
                    GetNodeByIndex(optimalPaths[i - 1]).Name
                    + "-"
                    + GetNodeByIndex(optimalPaths[i]).Name
                                          ];
            }

            return(new Road(transportStat.Name, roads)
            {
                From = NodesByName[transportStat.From],
                To = NodesByName[transportStat.To],
            });
        }
        public IEnumerable <uint> GetPath(uint from, uint to)
        {
            var dijkstra = new Dijkstra <uint, string>(graph.Clone());
            IShortestPathResult result = dijkstra.Process(from, to);

            return(result.GetPath());
        }
        public int GetDistance(uint from, uint to)
        {
            var dijkstra = new Dijkstra <uint, string>(graph.Clone());
            IShortestPathResult result = dijkstra.Process(from, to);

            return(result.Distance);
        }
Exemple #7
0
        public int CalulateDistanceDijkstra(int OriginCountry, int targetCountry)
        {
            CountriesGraph = new Graph <int, string>();
            SetCountriesGraph();
            var dijkstra = new Dijkstra <int, string>(CountriesGraph);
            IShortestPathResult result = dijkstra.Process(Convert.ToUInt16(OriginCountry - 1), Convert.ToUInt16(targetCountry - 1)); //result contains the shortest path

            return(result.Distance);
        }
Exemple #8
0
    /// <summary>
    /// Finding the shortest distance to move the truck along the city
    /// </summary>
    /// <param name="startNumber"></param>
    /// <param name="endNumber"></param>
    /// <returns></returns>
    public string[] wpRouteFinder(int startingWPNum, int endingWPNumber)
    {
        List <string> pathList = new List <string>();
        var           graph    = new Graph <int, string>();

        for (int i = 0; i < 24; i++)
        {
            graph.AddNode(i + 1);
        }

        for (int i = 0; i < 24; i++)
        {
            if (i != 23)
            {
                graph.Connect(Convert.ToUInt32(i), Convert.ToUInt32(i + 1), 1, "");
                graph.Connect(Convert.ToUInt32(i + 1), Convert.ToUInt32(i), 1, "");
            }

            else
            {
                graph.Connect(Convert.ToUInt32(i), Convert.ToUInt32(0), 1, "");
                graph.Connect(Convert.ToUInt32(0), Convert.ToUInt32(i), 1, "");
            }
        }

        var dijkstra = new Dijkstra <int, string>(graph);

        IShortestPathResult result = dijkstra.Process(Convert.ToUInt32(startingWPNum), Convert.ToUInt32(endingWPNumber));

        result.GetPath();

        foreach (var item in result.GetPath())
        {
            pathList.Add(item.ToString());
        }
        return(pathList.ToArray());
    }