|
| 1 | +def airportConnections(airports, routes, startingAirport): |
| 2 | + airportGraph = createAirportGraph(airports, routes) |
| 3 | + unreachableAirportNodes = getUnreachableAirportNodes(airportGraph, airports, startingAirport) |
| 4 | + markUnreachableConnections(airportGraph, unreachableAirportNodes) |
| 5 | + return getMinNumberOfNewConnections(airportGraph, unreachableAirportNodes) |
| 6 | + |
| 7 | + |
| 8 | +def createAirportGraph(airports, routes): |
| 9 | + graph = {} |
| 10 | + for airport in airports: |
| 11 | + graph[airport] = AirportNode(airport) |
| 12 | + for route in routes: |
| 13 | + airport, connection = route |
| 14 | + graph[airport].connections.append(connection) |
| 15 | + return graph |
| 16 | + |
| 17 | + |
| 18 | +def getUnreachableAirportNodes(airportGraph, airports, startingAirport): |
| 19 | + visitedAirports = {} |
| 20 | + dfsFromStartingAirport(airportGraph, startingAirport, visitedAirports) |
| 21 | + |
| 22 | + unreachableAirportNodes = [] |
| 23 | + for airport in airports: |
| 24 | + if airport in visitedAirports: |
| 25 | + continue |
| 26 | + airportNode = airportGraph[airport] |
| 27 | + airportNode.isRechable = False |
| 28 | + unreachableAirportNodes.append(airportNode) |
| 29 | + return unreachableAirportNodes |
| 30 | + |
| 31 | + |
| 32 | +def dfsFromStartingAirport(airportGraph, startingAirport, visitedAirports): |
| 33 | + if startingAirport in visitedAirports: |
| 34 | + return |
| 35 | + visitedAirports[startingAirport] = True |
| 36 | + connections = airportGraph[startingAirport].connections |
| 37 | + for connection in connections: |
| 38 | + dfsFromStartingAirport(airportGraph, connection, visitedAirports) |
| 39 | + |
| 40 | + |
| 41 | +def markUnreachableConnections(airportGraph, unreachableAirportNodes): |
| 42 | + for airportNode in unreachableAirportNodes: |
| 43 | + airport = airportNode.airport |
| 44 | + unreachableCOnnections = [] |
| 45 | + dfsAddUnreachableConnections(airportGraph, airport, unreachableCOnnections, {}) |
| 46 | + airportNode.unreachableConnections = unreachableCOnnections |
| 47 | + |
| 48 | + |
| 49 | +def dfsAddUnreachableConnections(airportGraph, airport, unreachableCOnnections, visitedAirports): |
| 50 | + if airportGraph[airport].isRechable: |
| 51 | + return |
| 52 | + if airport in visitedAirports: |
| 53 | + return |
| 54 | + visitedAirports[airport] = True |
| 55 | + unreachableCOnnections.append(airport) |
| 56 | + connections = airportGraph[airport].connections |
| 57 | + for connection in connections: |
| 58 | + dfsAddUnreachableConnections(airportGraph, connection, unreachableCOnnections, visitedAirports) |
| 59 | + |
| 60 | + |
| 61 | +def getMinNumberOfNewConnections(airportGraph, unreachableAirportNodes): |
| 62 | + unreachableAirportNodes.sort(key=lambda airport: len(airport.unreachableConnections), reverse=True) |
| 63 | + |
| 64 | + numberOfNewConnections = 0 |
| 65 | + for airportNode in unreachableAirportNodes: |
| 66 | + if airportNode.isRechable: |
| 67 | + continue |
| 68 | + numberOfNewConnections += 1 |
| 69 | + for connection in airportNode.unreachableConnections: |
| 70 | + airportGraph[connection].isRechable = True |
| 71 | + return numberOfNewConnections |
| 72 | + |
| 73 | + |
| 74 | +class AirportNode: |
| 75 | + def __init__(self, airport): |
| 76 | + self.airport = airport |
| 77 | + self.connections = [] |
| 78 | + self.isRechable = True |
| 79 | + self.unreachableConnections = [] |
0 commit comments