protected static internal void ReturnMinPath(NextAirport pathNode) // відновлення шляху після алгоритму А* { var result = new List <NextAirport>(); var currentNode = pathNode; result.Add(pathNode); while (currentNode != null) { result.Add(currentNode.previous); currentNode = currentNode.previous; } result.Reverse(); result.RemoveAt(0); foreach (var x in result) { Console.Write($" {x.IATA} ->>>"); } }
protected internal static NextAirport GetNextStation(string stationCode) { try { foreach (var line in Neighbours) { string[] temp = line.Split(";".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries); if (temp.Length == 3 && temp[0] == stationCode) { var airport = GetAirPort(temp[0]); if (airport != null) { NextAirport nextAirport = new NextAirport(airport, temp[1], int.MaxValue, JsonConvert.DeserializeObject <LinkedList <Airport> >(temp[2])); return(nextAirport); } } } } catch (Exception a) { Console.WriteLine(a.Message); } return(null); }
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); } }