Skip to content

Commit 5915cb9

Browse files
committed
Tweaks to dijkstra's
1 parent 53cecd5 commit 5915cb9

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

dijkstra.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"""
2929

3030
import json
31+
import sys
3132

3233

3334
def unroll_shortest_path(current, optimal_parent_map, path=()):
@@ -38,6 +39,9 @@ def unroll_shortest_path(current, optimal_parent_map, path=()):
3839

3940

4041
def dijkstra(start_city, end_city, city_data, verbose=True):
42+
if start_city == end_city:
43+
return (start_city,)
44+
4145
# Inefficiency: should be implemented as a priority queue
4246
start_city_distance_entry = [0, start_city]
4347
city_node_lookup = {start_city: start_city_distance_entry}
@@ -91,7 +95,16 @@ def get_city_data():
9195
return city_data
9296

9397
if __name__ == '__main__':
94-
#print(dijkstra("Milwaukee, WI", "Orlando, FL", get_city_data()))
95-
#print(dijkstra("Milwaukee, WI", "Houston, TX", get_city_data()))
96-
#print(dijkstra("Milwaukee, WI", "St. Louis, MO", get_city_data()))
97-
print(dijkstra("Milwaukee, WI", "Portland, OR", get_city_data(), False))
98+
city_data = get_city_data()
99+
try:
100+
city_from = sys.argv[1]
101+
city_to = sys.argv[2]
102+
except IndexError:
103+
print("Usage:", sys.argv[0], "\"from city\" \"to city>\"")
104+
print("City choices:")
105+
for city in city_data:
106+
print(" -", city)
107+
sys.exit(1)
108+
109+
print(dijkstra(city_from, city_to, city_data, False))
110+

get_city_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def get_city_distance(city_from, city_to):
3939
"Chicago, IL": ["Milwaukee, WI", "St. Louis, MO", "Indianapolis, IN", "Detroit, MI", "Cleveland, OH"],
4040
"Detroit, MI": ["Chicago, IL", "Cleveland, OH"],
4141
"St. Louis, MO": ["Chicago, IL", "Kansas City, MO", "Indianapolis, IN", "Atlanta, GA", "Dallas, TX"],
42-
"Kansas City, MO": ["St. Louis, MO", "Dallas, TX"],
42+
"Kansas City, MO": ["St. Louis, MO", "Dallas, TX", "Denver, CO"],
4343
"Minneapolis, MN": ["Milwaukee, WI", "Seattle, WA"],
4444
"Seattle, WA": ["Minneapolis, MN", "Portland, OR"],
4545
"Portland, OR": ["Seattle, WA", "Denver, CO", "San Francisco, CA"],
46-
"Denver, CO": ["Portland, OR", "Las Vegas, NV", "Albuquerque, NM"],
46+
"Denver, CO": ["Portland, OR", "Las Vegas, NV", "Albuquerque, NM", "Kansas City, MO"],
4747
"San Francisco, CA": ["Portland, OR", "Los Angeles, CA"],
4848
"Los Angeles, CA": ["San Francisco, CA", "Las Vegas, NV", "Phoenix, AZ"],
4949
"Las Vegas, NV": ["Los Angeles, CA", "Phoenix, AZ", "Denver, CO"],

0 commit comments

Comments
 (0)