//T.C and S.C is O(A+R) where A is the no of airports and R is the number of routes public static Dictionary <string, AirportNode> createAirportGraph(List <string> airports, List <List <string> > routes) { Dictionary <string, AirportNode> airportGraph = new Dictionary <string, AirportNode>(); foreach (string airport in airports) { airportGraph[airport] = new AirportNode(airport); } foreach (List <string> route in routes) { string airport = route[0]; string connection = route[1]; airportGraph[airport].connections.Add(connection); } return(airportGraph); }
//T.C is O(ALogA + A + R) and S.C is O(1) where A is the no of airports //and R is the number of routes public static List <AirportNode> getUnReachableAirportNodes (Dictionary <string, AirportNode> airportGraph, List <string> airports, string startingAirport) { HashSet <string> visitedAirports = new HashSet <string>(); depthFirstTraverseAirports(airportGraph, startingAirport, visitedAirports); List <AirportNode> unReachableAirportNodes = new List <AirportNode>(); foreach (string airport in airports) { if (visitedAirports.Contains(airport)) { continue; } AirportNode airportNode = airportGraph[airport]; airportNode.isReachable = false; unReachableAirportNodes.Add(airportNode); } return(unReachableAirportNodes); }