Skip to content

Commit ac64493

Browse files
author
Partho Biswas
committed
Airport connections
1 parent f86b326 commit ac64493

File tree

5 files changed

+83
-51
lines changed

5 files changed

+83
-51
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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>
Lines changed: 79 additions & 0 deletions
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 = []
Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,17 @@
1-
## Airport Connections
1+
## Airport_Connections
22

33
#### Problem Statement
44

5-
You are given a list of airports (three-letter codes like 'JFK'), a list of routes (one-way ights from one airport to another like ['JFK', 'SFO']), and
6-
a starting airport. Write a function that returns the minimum number of airport connections (one-way ights) that need to be added in order
7-
for someone to be able to reach any airport in the list, starting at the starting airport. Note that the connections don't have to be direct; it's
8-
okay if an airport can only be reached from the starting airport by stopping at other airports rst.
95

10-
Sample input:
11-
[
12-
"BGI",
13-
"CDG",
14-
"DEL",
15-
"DOH",
16-
"DSM",
17-
"EWR",
18-
"EYW",
19-
"HND",
20-
"ICN",
21-
"JFK",
22-
"LGA",
23-
"LHR",
24-
"ORD",
25-
"SAN",
26-
"SFO",
27-
"SIN",
28-
"TLV",
29-
"BUD",
30-
],
31-
[
32-
["DSM","ORD"],
33-
["ORD","BGI"],
34-
["BGI","LGA"],
35-
["SIN","CDG"],
36-
["CDG","SIN"],
37-
["CDG","BUD"],
38-
["DEL","DOH"],
39-
["DEL","CDG"],
40-
["TLV","DEL"],
41-
["EWR","HND"],
42-
["HND","ICN"],
43-
["HND","JFK"]
44-
["ICN","JFK"],
45-
["JFK","LGA"],
46-
["EYW","LHR"],
47-
["LHR","SFO"],
48-
["SFO","SAN"],
49-
["SFO","DSM"],
50-
["SAN","EYW"],
51-
],
52-
"LGA"
6+
![alt text](Airport_Connections_1.png "Airport_Connections")
7+
![alt text](Airport_Connections_2.png "Airport_Connections")
538

54-
Sample output: 3
559

5610

5711
#### Explanation
5812

59-
We can use a Stack here
6013

6114

6215
#### Solution
6316

6417
Check this [Python](../python/Airport_Connections.py) code.
65-
Loading
Loading

0 commit comments

Comments
 (0)