寻找最便宜的飞行算法

时间:2019-07-26 23:21:13

标签: c# .net entity-framework

我正在开发我的API应用程序,我的目标是寻找从A市到B市的最便宜航班。

数据结构如下:

public class Airport
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string IATA { get; set; }
    public string ICAO { get; set; }
    public decimal Latitude { get; set; }
    public decimal Longitude{ get; set; }
    public int Altitude { get; set; }
    public double? Timezone { get; set; }
    public string DST { get; set; }
    public string TzDatabaseTimezone { get; set; }
    public string Type { get; set; }
    public string Source { get; set; }
}


public class Route
{
    public int Id { get; set; }
    public string Airline { get; set; }
    public int? AirlineId { get; set; }
    public string SourceAirport { get; set; }
    public int? SourceAirportId { get; set; }
    public string DestinationAirport { get; set; }
    public int? DestinationAirportId { get; set; }
    public string Codeshare { get; set; }
    public int Stops { get; set; }
    public string Equipment { get; set; }
    public double Price { get; set; }
}

表路线有两个外键,SourceAirportId和DestinationAirportId,它们是表Airport(“ Id”列)中的主键。

因此,基本上,我的API中的输入将是城市A和城市B,而我的输出应该是该模型,其中将包含最便宜的一次航班,其中包括奖励,站点和所有路线,这对我来说很清楚。

我的问题是,由于该算法将无法轻松实现,是否可以使用任何外部API来简化该算法,还是应该创建自己的算法?如果是这样,那么任何代码或建议如何实现它都是无价的。

1 个答案:

答案 0 :(得分:0)

快速查看您的问题,这似乎是实现对您有利的算法的一种非常好的方法。

问题描述很容易像众所周知的算法Dijkstra's Algortihm

快速概述:

Dijkstra的算法是SSSP算法(单源最短路径),在您的情况下,源将是源机场,目的地是目的地机场,并且图形将由以下组成(机场为节点,路线为边)

因此,对于您的情况,实现算法将非常有用。

该算法的主要优点是其运行时复杂度,这对您的情况O(|E| + |V| log(|V|) ); where V is the number of nodes (airports) and E is the number of edges (routes)

非常有用

如果需要进一步的信息,我们将很乐意为您提供帮助!

相关问题