concept short path in category algorithms

This is an excerpt from Manning's book Algorithms and Data Structures in Action.
The takeaway being: it’s even more important to take the memory factor into account as early as possible when we design a system that needs to scale.
Listing 14.3 describes the algorithm: it starts from the goal vertex (the last vertex in the path), and reconstructs the path backwards, at each step looking for the parent of current vertex. In simple graphs, since there is only one edge between an ordered pair of vertices, if we know that we moved from vertex v to vertex u, the edge traversed becomes implicit; in multigraphs, we would have to keep track of the actual edge chosen at each step.

This is an excerpt from Manning's book Classic Computer Science Problems in Java.
A heuristic is an intuition about the way to solve a problem.[2] In the case of maze solving, a heuristic aims to choose the best maze location to search next, in the quest to get to the goal. In other words, it is an educated guess about which nodes on the frontier are closest to the goal. As was mentioned previously, if a heuristic used with an A* search produces an accurate relative result and is admissible (never overestimates the distance), then A* will deliver the shortest path. Heuristics that calculate smaller values end up leading to a search through more states, whereas heuristics closer to the exact real distance (but not over it, which would make them inadmissible) lead to a search through fewer states. Therefore, ideal heuristics come as close to the real distance as possible without ever going over it.
In an unweighted graph, finding the shortest path means finding the path that has the fewest edges between the starting vertex and the destination vertex. To build out the Hyperloop network, it might make sense to first connect the furthest cities on the highly populated seaboards. That raises the question “What is the shortest path between Boston and Miami?”
Listing 4.13 WeightedGraph.java
public static final class DijkstraNode implements Comparable<DijkstraNode> { public final int vertex; public final double distance; public DijkstraNode(int vertex, double distance) { this.vertex = vertex; this.distance = distance; } @Override public int compareTo(DijkstraNode other) { Double mine = distance; Double theirs = other.distance; return mine.compareTo(theirs); } } public static final class DijkstraResult { public final double[] distances; public final Map<Integer, WeightedEdge> pathMap; public DijkstraResult(double[] distances, Map<Integer, WeightedEdge> pathMap) { this.distances = distances; this.pathMap = pathMap; } } public DijkstraResult dijkstra(V root) { int first = indexOf(root); // find starting index // distances are unknown at first double[] distances = new double[getVertexCount()]; distances[first] = 0; // root's distance to root is 0 boolean[] visited = new boolean[getVertexCount()]; visited[first] = true; // how we got to each vertex HashMap<Integer, WeightedEdge> pathMap = new HashMap<>(); PriorityQueue<DijkstraNode> pq = new PriorityQueue<>(); pq.offer(new DijkstraNode(first, 0)); while (!pq.isEmpty()) { int u = pq.poll().vertex; // explore next closest vertex double distU = distances[u]; // should already have seen // look at every edge/vertex from the vertex in question for (WeightedEdge we : edgesOf(u)) { // the old distance to this vertex double distV = distances[we.v]; // the new distance to this vertex double pathWeight = we.weight + distU; // new vertex or found shorter path? if (!visited[we.v] || (distV > pathWeight)) { visited[we.v] = true; // update the distance to this vertex distances[we.v] = pathWeight; // update the edge on the shortest path pathMap.put(we.v, we); // explore it in the future pq.offer(new DijkstraNode(we.v, pathWeight)); } } } return new DijkstraResult(distances, pathMap); } // Helper function to get easier access to dijkstra results public Map<V, Double> distanceArrayToDistanceMap(double[] distances) { HashMap<V, Double> distanceMap = new HashMap<>(); for (int i = 0; i < distances.length; i++) { distanceMap.put(vertexAt(i), distances[i]); } return distanceMap; } // Takes a map of edges to reach each node and return a list of // edges that goes from *start* to *end* public static List<WeightedEdge> pathMapToPath(int start, int end, Map<Integer, WeightedEdge> pathMap) { if (pathMap.size() == 0) { return List.of(); } LinkedList<WeightedEdge> path = new LinkedList<>(); WeightedEdge edge = pathMap.get(end); path.add(edge); while (edge.u != start) { edge = pathMap.get(edge.u); path.add(edge); } Collections.reverse(path); return path; }

This is an excerpt from Manning's book Grokking Algorithms: An illustrated guide for programmers and other curious people.
You already saw breadth-first search once, when you calculated the shortest route from Twin Peaks to the Golden Gate Bridge. That was a question of type 2: “What is the shortest path?” Now let’s look at the algorithm in more detail. You’ll ask a question of type 1: “Is there a path?”

This is an excerpt from Manning's book Classic Computer Science Problems in Python.
As we learn in geometry, the shortest path between two points is a straight line. It makes sense, then, that a straight-line heuristic will always be admissible for the maze-solving problem. The Euclidean distance, derived from the Pythagorean theorem, states that distance = √((difference in x)2 + (difference in y)2). For our mazes, the difference in x is equivalent to the difference in columns between two maze locations, and the difference in y is equivalent to the difference in rows. Note that we are implementing this back in maze.py.
In an unweighted graph, finding the shortest path means finding the path that has the fewest edges between the starting vertex and the destination vertex. To build out the Hyperloop network, it might make sense to first connect the furthest cities on the highly populated seaboards. That raises the question “What is the shortest path between Boston and Miami?”
Listing 4.13. dijkstra.py
from __future__ import annotations from typing import TypeVar, List, Optional, Tuple, Dict from dataclasses import dataclass from mst import WeightedPath, print_weighted_path from weighted_graph import WeightedGraph from weighted_edge import WeightedEdge from priority_queue import PriorityQueue V = TypeVar('V') # type of the vertices in the graph @dataclass class DijkstraNode: vertex: int distance: float def __lt__(self, other: DijkstraNode) -> bool: return self.distance < other.distance def __eq__(self, other: DijkstraNode) -> bool: return self.distance == other.distance def dijkstra(wg: WeightedGraph[V], root: V) -> Tuple[List[Optional[float]], Dict[int, WeightedEdge]]: first: int = wg.index_of(root) # find starting index # distances are unknown at first distances: List[Optional[float]] = [None] * wg.vertex_count distances[first] = 0 # the root is 0 away from the root path_dict: Dict[int, WeightedEdge] = {} # how we got to each vertex pq: PriorityQueue[DijkstraNode] = PriorityQueue() pq.push(DijkstraNode(first, 0)) while not pq.empty: u: int = pq.pop().vertex # explore the next closest vertex dist_u: float = distances[u] # should already have seen it # look at every edge/vertex from the vertex in question for we in wg.edges_for_index(u): # the old distance to this vertex dist_v: float = distances[we.v] # no old distance or found shorter path if dist_v is None or dist_v > we.weight + dist_u: # update distance to this vertex distances[we.v] = we.weight + dist_u # update the edge on the shortest path to this vertex path_dict[we.v] = we # explore it soon pq.push(DijkstraNode(we.v, we.weight + dist_u)) return distances, path_dict # Helper function to get easier access to dijkstra results def distance_array_to_vertex_dict(wg: WeightedGraph[V], distances: List[Optional[float]]) -> Dict[V, Optional[float]]: distance_dict: Dict[V, Optional[float]] = {} for i in range(len(distances)): distance_dict[wg.vertex_at(i)] = distances[i] return distance_dict # Takes a dictionary of edges to reach each node and returns a list of # edges that goes from `start` to `end` def path_dict_to_path(start: int, end: int, path_dict: Dict[int, WeightedEdge]) -> WeightedPath: if len(path_dict) == 0: return [] edge_path: WeightedPath = [] e: WeightedEdge = path_dict[end] edge_path.append(e) while e.u != start: e = path_dict[e.u] edge_path.append(e) return list(reversed(edge_path)) !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":27,\"ch\":46},{\"line\":27,\"ch\":50}],[{\"line\":35,\"ch\":36},{\"line\":35,\"ch\":40}],[{\"line\":59,\"ch\":18},{\"line\":59,\"ch\":22}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":15}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":25}],[{\"line\":6,\"ch\":20},{\"line\":6,\"ch\":26}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":27,\"ch\":46},{\"line\":27,\"ch\":50}],[{\"line\":35,\"ch\":36},{\"line\":35,\"ch\":40}],[{\"line\":59,\"ch\":18},{\"line\":59,\"ch\":22}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":15}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":25}],[{\"line\":6,\"ch\":20},{\"line\":6,\"ch\":26}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":27,\"ch\":46},{\"line\":27,\"ch\":50}],[{\"line\":35,\"ch\":36},{\"line\":35,\"ch\":40}],[{\"line\":59,\"ch\":18},{\"line\":59,\"ch\":22}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":15}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":25}],[{\"line\":6,\"ch\":20},{\"line\":6,\"ch\":26}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":27,\"ch\":46},{\"line\":27,\"ch\":50}],[{\"line\":35,\"ch\":36},{\"line\":35,\"ch\":40}],[{\"line\":59,\"ch\":18},{\"line\":59,\"ch\":22}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":15}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":25}],[{\"line\":6,\"ch\":20},{\"line\":6,\"ch\":26}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":27,\"ch\":46},{\"line\":27,\"ch\":50}],[{\"line\":35,\"ch\":36},{\"line\":35,\"ch\":40}],[{\"line\":59,\"ch\":18},{\"line\":59,\"ch\":22}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":15}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":25}],[{\"line\":6,\"ch\":20},{\"line\":6,\"ch\":26}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":27,\"ch\":46},{\"line\":27,\"ch\":50}],[{\"line\":35,\"ch\":36},{\"line\":35,\"ch\":40}],[{\"line\":59,\"ch\":18},{\"line\":59,\"ch\":22}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":15}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":25}],[{\"line\":6,\"ch\":20},{\"line\":6,\"ch\":26}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":27,\"ch\":46},{\"line\":27,\"ch\":50}],[{\"line\":35,\"ch\":36},{\"line\":35,\"ch\":40}],[{\"line\":59,\"ch\":18},{\"line\":59,\"ch\":22}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":17},{\"line\":2,\"ch\":23}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":15}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":25}],[{\"line\":6,\"ch\":20},{\"line\":6,\"ch\":26}],[{\"line\":8,\"ch\":12},{\"line\":8,\"ch\":15}],[{\"line\":2,\"ch\":9},{\"line\":2,\"ch\":14}],[{\"line\":11,\"ch\":5},{\"line\":11,\"ch\":10}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":5}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":7}],[{\"line\":19,\"ch\":4},{\"line\":19,\"ch\":7}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":3}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":3}],[{\"line\":60,\"ch\":0},{\"line\":60,\"ch\":3}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":14}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":14}],[{\"line\":48,\"ch\":4},{\"line\":48,\"ch\":10}],[{\"line\":56,\"ch\":4},{\"line\":56,\"ch\":10}],[{\"line\":58,\"ch\":53},{\"line\":58,\"ch\":59}],[{\"line\":63,\"ch\":8},{\"line\":63,\"ch\":14}],[{\"line\":70,\"ch\":4},{\"line\":70,\"ch\":10}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":7}],[{\"line\":19,\"ch\":4},{\"line\":19,\"ch\":7}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":3}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":3}],[{\"line\":60,\"ch\":0},{\"line\":60,\"ch\":3}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":14}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":14}],[{\"line\":48,\"ch\":4},{\"line\":48,\"ch\":10}],[{\"line\":56,\"ch\":4},{\"line\":56,\"ch\":10}],[{\"line\":58,\"ch\":53},{\"line\":58,\"ch\":59}],[{\"line\":63,\"ch\":8},{\"line\":63,\"ch\":14}],[{\"line\":70,\"ch\":4},{\"line\":70,\"ch\":10}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":7}],[{\"line\":19,\"ch\":4},{\"line\":19,\"ch\":7}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":3}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":3}],[{\"line\":60,\"ch\":0},{\"line\":60,\"ch\":3}],[{\"line\":26,\"ch\":40},{\"line\":26,\"ch\":44}],[{\"line\":40,\"ch\":25},{\"line\":40,\"ch\":29}],[{\"line\":32,\"ch\":4},{\"line\":32,\"ch\":13}],[{\"line\":36,\"ch\":8},{\"line\":36,\"ch\":11}],[{\"line\":54,\"ch\":4},{\"line\":54,\"ch\":7}],[{\"line\":1,\"ch\":8},{\"line\":1,\"ch\":10}],[{\"line\":3,\"ch\":32},{\"line\":3,\"ch\":34}],[{\"line\":8,\"ch\":40},{\"line\":8,\"ch\":42}],[{\"line\":13,\"ch\":12},{\"line\":13,\"ch\":14}],[{\"line\":23,\"ch\":10},{\"line\":23,\"ch\":12}],[{\"line\":24,\"ch\":11},{\"line\":24,\"ch\":13}],[{\"line\":28,\"ch\":20},{\"line\":28,\"ch\":22}],[{\"line\":33,\"ch\":11},{\"line\":33,\"ch\":13}],[{\"line\":35,\"ch\":52},{\"line\":35,\"ch\":54}],[{\"line\":36,\"ch\":15},{\"line\":36,\"ch\":17}],[{\"line\":54,\"ch\":10},{\"line\":54,\"ch\":12}],[{\"line\":60,\"ch\":29},{\"line\":60,\"ch\":31}],[{\"line\":40,\"ch\":12},{\"line\":40,\"ch\":14}],[{\"line\":62,\"ch\":4},{\"line\":62,\"ch\":6}],[{\"line\":40,\"ch\":22},{\"line\":40,\"ch\":32}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":14}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":14}],[{\"line\":48,\"ch\":4},{\"line\":48,\"ch\":10}],[{\"line\":56,\"ch\":4},{\"line\":56,\"ch\":10}],[{\"line\":58,\"ch\":53},{\"line\":58,\"ch\":59}],[{\"line\":63,\"ch\":8},{\"line\":63,\"ch\":14}],[{\"line\":70,\"ch\":4},{\"line\":70,\"ch\":10}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":7}],[{\"line\":19,\"ch\":4},{\"line\":19,\"ch\":7}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":3}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":3}],[{\"line\":60,\"ch\":0},{\"line\":60,\"ch\":3}],[{\"line\":36,\"ch\":8},{\"line\":36,\"ch\":11}],[{\"line\":54,\"ch\":4},{\"line\":54,\"ch\":7}],[{\"line\":1,\"ch\":8},{\"line\":1,\"ch\":10}],[{\"line\":3,\"ch\":32},{\"line\":3,\"ch\":34}],[{\"line\":8,\"ch\":40},{\"line\":8,\"ch\":42}],[{\"line\":13,\"ch\":12},{\"line\":13,\"ch\":14}],[{\"line\":23,\"ch\":10},{\"line\":23,\"ch\":12}],[{\"line\":24,\"ch\":11},{\"line\":24,\"ch\":13}],[{\"line\":28,\"ch\":20},{\"line\":28,\"ch\":22}],[{\"line\":33,\"ch\":11},{\"line\":33,\"ch\":13}],[{\"line\":35,\"ch\":52},{\"line\":35,\"ch\":54}],[{\"line\":36,\"ch\":15},{\"line\":36,\"ch\":17}],[{\"line\":54,\"ch\":10},{\"line\":54,\"ch\":12}],[{\"line\":60,\"ch\":29},{\"line\":60,\"ch\":31}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":14}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":14}],[{\"line\":48,\"ch\":4},{\"line\":48,\"ch\":10}],[{\"line\":56,\"ch\":4},{\"line\":56,\"ch\":10}],[{\"line\":58,\"ch\":53},{\"line\":58,\"ch\":59}],[{\"line\":63,\"ch\":8},{\"line\":63,\"ch\":14}],[{\"line\":70,\"ch\":4},{\"line\":70,\"ch\":10}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":7}],[{\"line\":19,\"ch\":4},{\"line\":19,\"ch\":7}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":3}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":3}],[{\"line\":60,\"ch\":0},{\"line\":60,\"ch\":3}],[{\"line\":40,\"ch\":12},{\"line\":40,\"ch\":14}],[{\"line\":62,\"ch\":4},{\"line\":62,\"ch\":6}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":14}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":14}],[{\"line\":48,\"ch\":4},{\"line\":48,\"ch\":10}],[{\"line\":56,\"ch\":4},{\"line\":56,\"ch\":10}],[{\"line\":58,\"ch\":53},{\"line\":58,\"ch\":59}],[{\"line\":63,\"ch\":8},{\"line\":63,\"ch\":14}],[{\"line\":70,\"ch\":4},{\"line\":70,\"ch\":10}],[{\"line\":32,\"ch\":4},{\"line\":32,\"ch\":9}],[{\"line\":67,\"ch\":4},{\"line\":67,\"ch\":9}],[{\"line\":17,\"ch\":8},{\"line\":17,\"ch\":14}],[{\"line\":20,\"ch\":8},{\"line\":20,\"ch\":14}],[{\"line\":48,\"ch\":4},{\"line\":48,\"ch\":10}],[{\"line\":56,\"ch\":4},{\"line\":56,\"ch\":10}],[{\"line\":58,\"ch\":53},{\"line\":58,\"ch\":59}],[{\"line\":63,\"ch\":8},{\"line\":63,\"ch\":14}],[{\"line\":70,\"ch\":4},{\"line\":70,\"ch\":10}]]"} !@%STYLE%@!

This is an excerpt from Manning's book Classic Computer Science Problems in Swift: Essential techniques for practicing programmers.
As we learn in geometry, the shortest path between two points is a straight line. It makes sense, then, that a straight-line heuristic will always be admissible for the maze-solving problem. The Euclidean distance, derived from the Pythagorean theorem, states that distance = √((difference in x)2 + (difference in y)2). For our mazes, the difference in x is equivalent to the difference in columns of two maze locations, and the difference in y is equivalent to the difference in rows.
public extension WeightedGraph { /// Represents a node in the priority queue used /// for selecting the next struct DijkstraNode: Comparable, Equatable { let vertex: Int let distance: W public static func < (lhs: DijkstraNode, rhs: DijkstraNode) -> Bool { return lhs.distance < rhs.distance } public static func == (lhs: DijkstraNode, rhs: DijkstraNode) -> Bool { return lhs.distance == rhs.distance } } /// Finds the shortest paths from some route vertex to every other vertex in the graph. /// /// - parameter graph: The WeightedGraph to look within. /// - parameter root: The index of the root node to build the shortest paths from. /// - parameter startDistance: The distance to get to the root node (typically 0). /// - returns: Returns a tuple of two things: the first, an array containing the distances, the second, a dictionary containing the edge to reach each vertex. Use the function pathDictToPath() to convert the dictionary into something useful for a specific point. public func dijkstra(root: Int, startDistance: W) -> ([W?], [Int: WeightedEdge<W>]) { var distances: [W?] = [W?](repeating: nil, count: vertexCount) // how far each vertex is from start distances[root] = startDistance // the start vertex is startDistance away var pq: PriorityQueue<DijkstraNode> = PriorityQueue<DijkstraNode>(ascending: true) var pathDict: [Int: WeightedEdge<W>] = [Int: WeightedEdge<W>]() // how we got to each vertex pq.push(DijkstraNode(vertex: root, distance: startDistance)) while let u = pq.pop()?.vertex { // explore the next closest vertex guard let distU = distances[u] else { continue } // should already have seen it for we in edgesForIndex(u) { // look at every edge/vertex from the vertex in question let distV = distances[we.v] // the old distance to this vertex if distV == nil || distV! > we.weight + distU { // if we have no old distance or we found a shorter path distances[we.v] = we.weight + distU // update the distance to this vertex pathDict[we.v] = we // update the edge on the shortest path to this vertex pq.push(DijkstraNode(vertex: we.v, distance: we.weight + distU)) // explore it soon } } } return (distances, pathDict) } /// A convenience version of dijkstra() that allows the supply of the root /// vertex instead of the index of the root vertex. public func dijkstra(root: V, startDistance: W) -> ([W?], [Int: WeightedEdge<W>]) { if let u = indexOfVertex(root) { return dijkstra(root: u, startDistance: startDistance) } return ([], [:]) } /// Helper function to get easier access to Dijkstra results. public func distanceArrayToVertexDict(distances: [W?]) -> [V : W?] { var distanceDict: [V: W?] = [V: W?]() for i in 0..<distances.count { distanceDict[vertexAtIndex(i)] = distances[i] } return distanceDict } }
We can use our old friend, pathDictToPath(), to find the shortest path between Los Angeles and a specific other MSA—say Boston. Finally, we can use printWeightedPath() to pretty-print the result.
let path = pathDictToPath(from: cityGraph2.indexOfVertex("Los Angeles")!, to: cityGraph2.indexOfVertex("Boston")!, pathDict: pathDict) cityGraph2.printWeightedPath(path as! [WeightedEdge<Int>])The shortest path from Los Angeles to Boston is