People call the above series of steps Dijkstra's algorithm. Dijkstra's algorithm can be slow, because it will look at many places that might be going the wrong way from Z. If you asked the computer how to get from one city to a near one, Dijkstra's algorithm might end up looking in another state.