-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject SkyRoute (graph search)
More file actions
275 lines (233 loc) · 10.2 KB
/
Project SkyRoute (graph search)
File metadata and controls
275 lines (233 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#skyroute file, includes main programs
from graph_search import bfs, dfs
from vc_metro import vc_metro
from vc_landmarks import vc_landmarks
from landmark_choices import landmark_choices
landmark_string = ''
for letter, landmark in landmark_choices.items():
landmark_string += "{0} - {1}\n".format(letter,landmark)
def greeting():
print("Hi there and welcome to SkyRoute!")
print("We'll help you find the shortest route between the following Vancouver landmarks: \n"+landmark_string)
def goodbye():
print("Thanks for using SkyRoute!")
def set_start_and_end(start_point,end_point):
if start_point is not None:
change_point = input("What would you like to change? You can enter 'o' for 'origin', 'd' for 'destination', or 'b' for 'both': ")
if change_point == "b":
start_point = get_start()
end_point = get_end()
elif change_point == "o":
start_point = get_start()
elif change_point == "d":
end_point = get_end()
else:
print("Oops, that isn't 'o', 'd', or 'b'...")
set_start_and_end(start_point,end_point)
else:
start_point = get_start()
end_point = get_end()
return start_point,end_point
def get_start():
start_point_letter = input("Where are you coming from? Type in the corresponding letter: ")
if start_point_letter in landmark_choices:
start_point = landmark_choices[start_point_letter]
return start_point
else:
print("Sorry, that's not a landmark we have data on. Let's try this again...")
return get_start()
def get_end():
end_point_letter = input("Ok, where are you headed? Type in the corresponding letter: ")
if end_point_letter in landmark_choices:
end_point = landmark_choices[end_point_letter]
return end_point
else:
print("Sorry, that's not a landmark we have data on. Let's try this again...")
return get_end()
def show_landmarks():
see_landmarks = input("Would you like to see the list of landmarks again? Enter y/n: ")
if see_landmarks == 'y':
print(landmark_string)
stations_under_construction = ['Aberdeen']
def get_active_stations():
updated_metro = vc_metro
for station_under_construction in stations_under_construction:
for current_station, neighboring_stations in vc_metro.items():
if current_station != station_under_construction:
updated_metro[current_station] -= set(stations_under_construction)
else:
updated_metro[current_station] = set([])
return updated_metro
def new_route(start_point=None,end_point=None):
start_point,end_point = set_start_and_end(start_point,end_point)
shortest_route = get_route(start_point,end_point)
if shortest_route:
shortest_route_string = '\n'.join(shortest_route)
print("\nThe shortest metro route from {0} to {1} is:\n{2}".format(start_point, end_point, shortest_route_string))
else:
print("Unfortunately, there is currently no path between {0} and {1} due to maintenance.".format(start_point, end_point))
again = input("\nWould you like to see another route? Enter y/n: ")
if again == 'y':
show_landmarks()
new_route(start_point,end_point)
def get_route(start_point,end_point):
start_stations = vc_landmarks[start_point]
end_stations = vc_landmarks[end_point]
routes = []
for start_station in start_stations:
for end_station in end_stations:
metro_system = get_active_stations() if stations_under_construction else vc_metro
if stations_under_construction:
possible_route = dfs(metro_system,start_station,end_station)
if not possible_route:
return None
route = bfs(metro_system,start_station,end_station)
if route:
routes.append(route)
shortest_route = min(routes, key=len)
return shortest_route
def skyroute():
greeting()
new_route()
goodbye()
skyroute()
#graph file for generating graphs
def bfs(graph, start_vertex, target_value):
path = [start_vertex]
vertex_and_path = [start_vertex, path]
bfs_queue = [vertex_and_path]
visited = set()
while bfs_queue:
current_vertex, path = bfs_queue.pop(0)
visited.add(current_vertex)
for neighbor in graph[current_vertex]:
if neighbor not in visited:
if neighbor == target_value:
return path + [neighbor]
else:
bfs_queue.append([neighbor, path + [neighbor]])
def dfs(graph, current_vertex, target_value, visited = None):
if visited is None:
visited = []
visited.append(current_vertex)
if current_vertex == target_value:
return visited
for neighbor in graph[current_vertex]:
if neighbor not in visited:
path = dfs(graph, neighbor, target_value, visited)
if path:
return path
#Vancouver metro file which holds all the landmarks and connections for the system
vc_landmarks = {
'Marine Building': set(['Burrard', 'Waterfront']),
'Scotiabank Field at Nat Bailey Stadium': set(['King Edward']),
'Vancouver Aquarium': set(['Burrard']),
'Vancouver Lookout': set(['Waterfront']),
'Canada Place': set(['Burrard', 'Waterfront']),
'Cathedral of Our Lady of the Holy Rosary': set(['Vancouver City Centre', 'Granville']),
'Library Square': set(['Vancouver City Centre', 'Stadium-Chinatown']),
'B.C. Place Stadium': set(['Stadium-Chinatown']),
'Lions Gate Bridge': set(['Burrard']),
'Gastown Steam Clock': set(['Waterfront']),
'Waterfront Station': set(['Waterfront']),
'Granville Street': set(['Granville', 'Vancouver City Centre']),
'Pacific Central Station': set(['Main Street-Science World']),
'Prospect Point Lighthouse': set(['Burrard']),
'Queen Elizabeth Theatre': set(['Stadium-Chinatown']),
'Stanley Park': set(['Burrard']),
'Granville Island Public Market': set(['Yaletown-Roundhouse']),
'Kitsilano Beach': set(['Olympic Village']),
'Dr. Sun Yat-Sen Classical Chinese Garden': set(['Stadium-Chinatown']),
'Museum of Vancouver': set(['Yaletown-Roundhouse']),
'Science World': set(['Main Street-Science World']),
'Robson Square': set(['Vancouver City Centre']),
'Samson V Maritime Museum': set(['Columbia']),
'Burnaby Lake': set(['Sperling / Burnaby Lake', 'Lake City Way', 'Production Way / University']),
'Nikkei National Museum & Cultural Centre': set(['Edmonds']),
'Central Park': set(['Patterson', 'Metrotown'])
}
#vancouver metro file that holds all the landmarks
landmark_choices = {
'a': 'Marine Building',
'b': 'Scotiabank Field at Nat Bailey Stadium',
'c': 'Vancouver Aquarium',
'd': 'Vancouver Lookout',
'e': 'Canada Place',
'f': 'Cathedral of Our Lady of the Holy Rosary',
'g': 'Library Square',
'h': 'B.C. Place Stadium',
'i': 'Lions Gate Bridge',
'j': 'Gastown Steam Clock',
'k': 'Waterfront Station',
'l': 'Granville Street',
'm': 'Pacific Central Station',
'n': 'Prospect Point Lighthouse',
'o': 'Queen Elizabeth Theatre',
'p': 'Stanley Park',
'q': 'Granville Island Public Market',
'r': 'Kitsilano Beach',
's': 'Dr. Sun Yat-Sen Classical Chinese Garden',
't': 'Museum of Vancouver',
'u': 'Science World',
'v': 'Robson Square',
'w': 'Samson V Maritime Museum',
'x': 'Burnaby Lake',
'y': 'Nikkei National Museum & Cultural Centre',
'z': 'Central Park'
}
#metro file that includes the station names and connections
vc_metro = {
'Richmond-Brighouse': set(['Lansdowne']),
'Lansdowne': set(['Richmond-Brighouse', 'Aberdeen']),
'Aberdeen': set(['Lansdowne', 'Bridgeport']),
'Bridgeport': set(['Aberdeen', 'Templeton', 'Marine Drive']),
'YVR-Airport': set(['Sea Island Centre']),
'Sea Island Centre': set(['YVR-Airport', 'Templeton']),
'Templeton': set(['Sea Island Centre', 'Bridgeport']),
'Marine Drive': set(['Bridgeport', 'Langara-49th Avenue']),
'Langara-49th Avenue': set(['Marine Drive', 'Oakbridge-41st Avenue']),
'Oakbridge-41st Avenue': set(['Langara-49th Avenue', 'King Edward']),
'King Edward': set(['Oakbridge-41st Avenue', 'Broadway-City Hall']),
'Broadway-City Hall': set(['King Edward', 'Olympic Village']),
'Olympic Village': set(['Broadway-City Hall', 'Yaletown-Roundhouse']),
'Yaletown-Roundhouse': set(['Olympic Village', 'Vancouver City Centre']),
'Vancouver City Centre': set(['Yaletown-Roundhouse', 'Waterfront']),
'Waterfront': set(['Vancouver City Centre', 'Burrard']),
'Burrard': set(['Waterfront', 'Granville']),
'Granville': set(['Burrard', 'Stadium-Chinatown']),
'Stadium-Chinatown': set(['Granville', 'Main Street-Science World']),
'Main Street-Science World': set(['Stadium-Chinatown', 'Commercial-Broadway']),
'Commercial-Broadway': set(['VCC-Clark', 'Main Street-Science World', 'Renfrew', 'Nanaimo']),
'VCC-Clark': set(['Commercial-Broadway']),
'Nanaimo': set(['Commercial-Broadway', '29th Avenue']),
'29th Avenue': set(['Nanaimo', 'Joyce-Collingwood']),
'Joyce-Collingwood': set(['29th Avenue', 'Patterson']),
'Patterson': set(['Joyce-Collingwood', 'Metrotown']),
'Metrotown': set(['Patterson', 'Royal Oak']),
'Royal Oak': set(['Metrotown', 'Edmonds']),
'Edmonds': set(['Royal Oak', '22nd Street']),
'22nd Street': set(['Edmonds', 'New Westminster']),
'New Westminster': set(['22nd Street', 'Columbia']),
'Columbia': set(['New Westminster', 'Sapperton', 'Scott Road']),
'Scott Road': set(['Columbia', 'Gateway']),
'Gateway': set(['Scott Road', 'Surrey Central']),
'Surrey Central': set(['Gateway', 'King George']),
'King George': set(['Surrey Central']),
'Sapperton': set(['Columbia', 'Braid']),
'Braid': set(['Sapperton', 'Lougheed Town Centre']),
'Lougheed Town Centre': set(['Braid', 'Production Way / University', 'Burquitlam']),
'Burquitlam': set(['Lougheed Town Centre', 'Moody Centre']),
'Moody Centre': set(['Burquitlam', 'Inlet Centre']),
'Inlet Centre': set(['Moody Centre', 'Coquitlam Central']),
'Coquitlam Central': set(['Inlet Centre', 'Lincoln']),
'Lincoln': set(['Coquitlam Central', 'Lafarge Lake-Douglas']),
'Lafarge Lake-Douglas': set(['Lincoln']),
'Production Way / University': set(['Lougheed Town Centre', 'Lake City Way']),
'Lake City Way': set(['Production Way / University', 'Sperling / Burnaby Lake']),
'Sperling / Burnaby Lake': set(['Lake City Way', 'Holdom']),
'Holdom': set(['Sperling / Burnaby Lake', 'Brentwood Town Centre']),
'Brentwood Town Centre': set(['Holdom', 'Gilmore']),
'Gilmore': set(['Brentwood Town Centre', 'Rupert']),
'Rupert': set(['Gilmore', 'Renfrew']),
'Renfrew': set(['Rupert', 'Commercial-Broadway'])
}