Skip to content

Commit 97b7436

Browse files
author
Partho Biswas
committed
Airport connections
1 parent 3323dde commit 97b7436

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ BTW, it's a great interview prep tool. You will never go wrong with this and you
860860
|05| [Breadth_First_Search](algoexpert.io/questions/Breadth-first_Search.md) | [Python](algoexpert.io/python/Breadth_First_Search.py)|
861861
|06| [Boggle_Board](algoexpert.io/questions/Boggle_Board.md) | [Python](algoexpert.io/python/Boggle_Board.py) |
862862
|07| [Rectangle_Mania](algoexpert.io/questions/Rectangle_Mania.md) | [Python](algoexpert.io/python/Rectangle_Mania.py) |
863+
|07| [Airport_Connections](algoexpert.io/questions/Airport_Connections.md) | [Python](algoexpert.io/python/Airport_Connections.py) |
863864

864865
</p>
865866
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)