I have this Flight class:
public class Flight
{
public string Origin { get; set; }
public string Destination { get; set; }
public decimal Price { get; set; }
}
Let's say a user wants to travel from India to China. It might be the case that an India-China pair is not available in a list of Flight objects, but it's possible to achieve this by "hopping" between Flight objects, e.g.:
India-Budapest 100EUR
Budapest-Romania 200EUR
Romania-China 100EUR
The result would be India -> Budapest -> Romania -> China and the cost would be 400 EUR.
I want to be able to identify, in the following order:
- A direct flight, and if not:
- The shortest amount of hops to achieve the flight, if such a hop/combo exists.
I'm looking for the least amount of flights rather than least distance. All are equal weighted.
I can brute force this, by first checking that any such Origin-Destination pair exists for a given Flight object, and if not, find Destination matches and try to go backwards, but this seems like a slow implementation.
What would be the best way to achieve this rather than brute forcing the solution?