CPSC 203, 2025 W2
March 24, 2026
You’ve got 9 errands to run. What order do you do them in?
Determine the least cost route through a set of given locations, returning to the start.


“Given a list of cities, and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?”
Classic problem on graphs
First known mention was in 1930
________________________________________
Suppose you have 6 locations. How many different candidate solutions are there? Generalize to k locations?

________________________
________________________
________________________
________________________
________________________
https://medium.com/data-science/around-the-world-in-90-414-kilometers-ce84c03b8552
There are lots of applications:
Steps to assemble our solution:
________________________________________
________________________________________
________________________________________
________________________________________
________________________________________
What structure should we use?
dist['Vancouver']['Bellingham'] -> int path['Vancouver']['Bellingham'] -> list
example: dist['Van']['Bel'] = nx.shortest_path(G, 'Van', 'Bel', weight='length')
tours = list(itertools.permutations(list(dferrands['node'])))
We assemble each candidate solution as an arrangement of all of the errands:
Ex: A D B E F C
How do we find the total distance if we do the errands in the suggested order?
lambda Do?Goal: Create a new column (latlong) and fill it with coordinates for each errand in the dataframe.
How it works:
df.apply(..., axis=1): Run a function once for each row.
lambda row: ox.geocode(row['errand']): A tiny inline function. Read it as: “Given a row, look up the place name in row['errand'],geocode it, and return the (lat, lon).”
The returned value becomes the entry in the new latlong column.
Why lambda? We only need this function once, so we write it right where it’s used.