Example #1
0
 protected static internal void SearchPointsWithBfs(string sourceCode, string searchCode)
 {
     try
     {
         var watch = Stopwatch.StartNew();
         AirlineData.LoadData();
         var     dictOfNeighboursByCode = AirlineData.DictOfNeighbours;
         Airport source  = AirlineData.GetAirPort(sourceCode);
         var     next    = AirlineData.GetNextStation(sourceCode);
         var     que     = new Queue <NextAirport>();
         var     visited = new List <Airport>();
         que.Enqueue(next);
         visited.Add(source);
         var counter = 0;
         while (que.Count > 0)
         {
             var vertex = que.Dequeue();
             if (vertex.IATA == searchCode)
             {
                 watch.Stop();
                 Console.WriteLine("Airport with code {0} and name {1} finded from start airport {2}", vertex.IATA, vertex.Current.AirportName, sourceCode);
                 Console.WriteLine("Count of vetrices between two vertices = {0}", counter);
                 Console.WriteLine("Count of visited vertices = {0}", visited.Count);
                 Console.WriteLine("Time spended {0}", watch.ElapsedMilliseconds);
                 return;
             }
             foreach (var neighbour in vertex.adjacementList)
             {
                 if (neighbour != null)
                 {
                     if (!(visited.Contains(neighbour)))
                     {
                         que.Enqueue(dictOfNeighboursByCode[neighbour.IATA]);
                         visited.Add(neighbour);
                         counter++;
                     }
                 }
             }
         }
     }
     catch (Exception r)
     {
         Console.WriteLine(r);
     }
 }
Example #2
0
        protected static internal void AStarMinPath(string sourceCode, string destinationCode)
        {
            // отримуємо найкоротший шлях від аеропорта А до аеропорта Б
            AirlineData.LoadData();
            var watch     = Stopwatch.StartNew();
            var closedSet = new Queue <NextAirport>();
            var openSet   = new Queue <NextAirport>();

            Airport source = AirlineData.GetAirPort(sourceCode);
            var     next   = AirlineData.GetNextStation(sourceCode);

            Graph.Source = next;
            var qq = AirlineData.GetAirPort(destinationCode);

            next.HeuristicEstimatePathLength = GetPath(source, qq);
            next.previous                    = null;
            next.PathLengthFromStart         = 0;
            next.HeuristicEstimatePathLength = GetPath(source, AirlineData.GetAirPort(destinationCode));

            openSet.Enqueue(next);
            while (openSet.Count > 0)
            {
                var currentAirport = openSet.OrderBy(node => node.EstimateFullPathLength).First();
                if (currentAirport.Current.IATA == destinationCode)
                {
                    //Console.WriteLine("SUCCESS!");
                    Console.WriteLine("Time spended {0}", watch.ElapsedMilliseconds);
                    Console.WriteLine($"There is the way from {source.AirportName} to {AirlineData.GetAirPort(destinationCode).AirportName} ");
                    ReturnMinPath(currentAirport);
                    return;
                    // ReturnMinPath();
                }
                var x = openSet.Dequeue(); // openSet.(currentAirport);
                                           // Console.WriteLine($"{x.IATA} ------- {x.Current.CountryName}");
                closedSet.Enqueue(x);
                //Console.WriteLine($"Number of neighbours ={currentAirport.adjacementList.Count}");
                foreach (var neighbourNode in currentAirport.adjacementList)
                {
                    var neighbourNode1 = AirlineData.GetNextStation(neighbourNode.IATA);
                    neighbourNode1.previous            = currentAirport;
                    neighbourNode1.PathLengthFromStart = currentAirport.PathLengthFromStart + GetPath(currentAirport.Current, AirlineData.GetAirPort(destinationCode)); // виглядає дивно, працює швидко, результат коректний
                    //neighbourNode1.PathLengthFromStart = currentAirport.PathLengthFromStart + GetPath(currentAirport.Current, AirlineData.GetAirPort(neighbourNode.IATA)); // виглядає правильніше, працює довше, результат коректний
                    neighbourNode1.HeuristicEstimatePathLength = GetPath(neighbourNode1.Current, AirlineData.GetAirPort(destinationCode));

                    if (closedSet.Count(node => node.IATA == neighbourNode1.IATA) > 0)
                    {
                        continue;
                    }
                    var openNode = openSet.FirstOrDefault(node => node.IATA == neighbourNode1.IATA);
                    if (openNode == null)
                    {
                        openSet.Enqueue(neighbourNode1);
                    }

                    else
                    {
                        if (openNode.PathLengthFromStart > neighbourNode1.PathLengthFromStart)
                        {
                            openNode.previous            = currentAirport;
                            openNode.PathLengthFromStart = neighbourNode1.PathLengthFromStart;
                        }
                    }
                }
            }
            Console.WriteLine($"There is no way from airport with name {source.AirportName} to airport {AirlineData.GetAirPort(destinationCode).AirportName}");
            return;
        }
Example #3
0
        protected static internal void DijkstraMinPath(string sourceCode, string destinationCode)
        {
            try
            {
                AirlineData.LoadData();
                var           dictOfNeighboursByCode = AirlineData.DictOfNeighbours;
                var           watch         = Stopwatch.StartNew();
                var           priotityQueue = new Queue <NextAirport>();
                List <string> visited       = new List <string>();
                var           path          = new Dictionary <NextAirport, NextAirport>();
                Airport       source        = AirlineData.GetAirPort(sourceCode);
                var           next          = AirlineData.GetNextStation(sourceCode);
                next.Weight = 0;//sorce
                priotityQueue.Enqueue(next);
                Graph.Source = next;
                double BestPrice = 0;

                path[next] = null;
                while (priotityQueue.Count > 0)
                {
                    var _flight = priotityQueue.Dequeue();
                    if (_flight.Current.IATA == destinationCode)
                    {
                        watch.Stop();
                        Graph.Destination = _flight;
                        Graph.Path        = path;
                        Console.WriteLine("Time spended {0}", watch.ElapsedMilliseconds);
                        Console.WriteLine("Count of visited vertices = {0}", visited.Count);
                        ReturnMinPath();

                        return;
                    }
                    visited.Add(_flight.IATA);

                    foreach (var neighbour in _flight.adjacementList)
                    {
                        if (neighbour != null)
                        {
                            NextAirport _next = dictOfNeighboursByCode[neighbour.IATA];
                            if (!visited.Contains(neighbour.IATA) && _next != null)
                            {
                                var best = GetPriceByPath(_flight.IATA, neighbour.IATA) + _flight.Weight;//maybe best price
                                if (best < _next.Weight)
                                {
                                    _next.Weight = best;
                                    priotityQueue.Enqueue(_next);
                                    path[_next] = _flight;
                                    SortQueue(ref priotityQueue);
                                }
                            }
                        }
                    }
                }

                Console.ReadKey();
            }
            catch (Exception r)

            {
                Console.WriteLine(r);
            }
        }