Hello Aspiring Real Data and Decision Scientists,
In case you missed, the introduction to the series of problems, please go through the following discussion:
.
.
What THEY Do Not Teach YOU In Data Science! - By Anish Arya (Machine Learning and Reinforcement Learning Consultant)
.
.
PROBLEM #3 STATEMENT: Problem Level: Hard
The following is a Graph problem which will test the knowledge of Hash tables and node traversing techniques.
The CEO of a leading airline company wants to know if his flights are servicing all major cities /airports such that if a passenger wants to travel from a given starting city, they can reach any of the other cities using either a direct flight (0 stops) or an indirect flight (multiple stops at intermediate cities).
If there are any cities that are not reachable, the CEO wants to know the minimum number of flights (oneway flights) that need to be added so that the passenger can reach any destination city from the starting city using either direct or indirect flights.
You are given a list of city airports (three-letter codes like “JFK”), a list of routes (one-way flights from one airport to another like [“JFK” , “SFO”] ), and a starting city / airport.
Note that routes only allow you to fly in one direction; for instance, the route [“JFK”, “SFO”] only allows you to fly from “JFK” to “SFO” and not from SFO to JFK
Also note that the connections don’t have to be direct (take off at the starting airport and land in the destination airport).
It is okay if the destination airport can be reached from the starting airport by stopping at other intermediary airports in between.
Sample Input:
airports = [ “BGI”, “CDG”, “DEL”, “DOH”, “DSM”, “EWR”, “EYW”, “HND”, “ICN”, “JFK”, “LGA”, “LHR”, “ORD”, “SAN”, “SFO”, “SIN”, “TLV”, “BUD” ]
routes = [ [“DSM”, “ORD”], [“ORD”, “BGI”], [“BGI”, “LGA”], [“SIN”, “CDG”], [“CDG”, “SIN”], [“CDG”, “BUD”], [“DEL”, “DOH”], [“DEL”, “CDG”], [“TLV”, “DEL”], [“EWR”, “HND”], [“HND”, “ICN”], [“HND”, “JFK”], [“ICN”, “JFK”], [“JFK”, “LGA”], [“EYW”, “LHR”], [“LHR”, “SFO”], [“SFO”, “SAN”], [“SFO”, “DSM”], [“SAN”, “EYW”] ]
Starting Airport = LGA
Sample Output:
The minimum flights that need to be added = 3
The flights that need to be added are: [LGA, TLV][LGA, SFO][LGA, EWR]
Will post the answer by eod.