Skip to content

Instantly share code, notes, and snippets.

@tdierks
Created October 10, 2018 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdierks/c9ed55287c16732367ca3465596f6d47 to your computer and use it in GitHub Desktop.
Save tdierks/c9ed55287c16732367ca3465596f6d47 to your computer and use it in GitHub Desktop.
# coding: utf-8
# In[76]:
from collections import defaultdict
merge_cities = {
'NYC': ['EWR', 'JFK', 'LGA'],
}
airport_to_cities = dict()
for city in merge_cities:
for airport in merge_cities[city]:
airport_to_cities[airport] = city
international = set(['AMS', 'CDG', 'YYZ', 'FRA', 'IST', 'PEK', 'DME', 'MUC', 'DXB', 'LHR', 'LGW'])
all_routes = defaultdict(set)
with open("/home/dierks/routes.dat") as routes:
for route_line in (line.strip() for line in routes):
(airline, airline_id, src, src_id, dest, dest_id, codeshare, stops, equipment) = route_line.split(",")
if stops <> "0":
continue
if src in international or dest in international:
continue
if src in airport_to_cities:
src = airport_to_cities[src]
if dest in airport_to_cities:
dest = airport_to_cities[dest]
all_routes[src].add(dest)
# In[77]:
len(all_routes)
# In[78]:
for src in all_routes.keys():
for dest in all_routes[src]:
if src not in all_routes[dest]:
all_routes[dest].add(src) # pretend you can get back
# In[79]:
most_connected = sorted(all_routes.keys(), key = lambda k: len(all_routes[k]), reverse = True)
# In[80]:
most_connected[0:20]
# In[81]:
# find a greedy clique
def greedy(seed):
clique = set()
clique.update(seed)
for airport in most_connected:
if airport in clique:
next
if all((dest in all_routes[airport] for dest in clique)):
clique.add(airport)
return clique
# In[82]:
us_set = greedy(['NYC', 'LAX', 'SEA', 'ATL'])
us_set
# In[83]:
all_routes["DCA"].difference(all_routes["IAD"])
# In[ ]:
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment