4.3 Find a shortest path between two nodes
You get the distance from a node to every other node, and a path with the fewest edges.
Problem
Section titled “Problem”How do you get from one node to another along as few edges as possible? Each edge counts as one step. The number of edges on a shortest path is called the distance. Such questions come up for:
- the shortest chain of calls from the entry point of a program to a method, which explains how the method is reached;
- the fewest connections between two machines of a network;
- the fewest moves between two states of a game or a puzzle.
Example
Section titled “Example”The example graph calls models the calls between the methods of a small program. It has an edge
from each method to each method it calls. How many calls away from main is write, and along
which chain of calls? The picture after shows in green the methods on a shortest path from the start
node main to write.
- Start node or method asked about
- Method on the path
Solution
Section titled “Solution”BfsDistances.ofBfsDistances::ofBfsDistances.ofBfsDistances.Of searches from main and keeps the distance of every node
it reaches:
BfsDistances fromMain = BfsDistances.of(calls, main);int distance = fromMain.distanceOf(write); // 3 - three calls away// [0, 2, 4, 9] - main, run, save, writeIntList path = fromMain.pathTo(write);boolean reached = fromMain.isReachable(parseArgs); // truelet from_main = BfsDistances::of(&calls, main);let distance = from_main.distance_of(write); // Some(3) - three calls awaylet path = from_main.path_to(write); // [0, 2, 4, 9] - main, run, save, writelet reached = from_main.is_reachable(parse_args); // trueconst fromMain = BfsDistances.of(calls, main);const distance = fromMain.distanceOf(write); // 3 - three calls away// [0, 2, 4, 9] - main, run, save, writeconst path = fromMain.pathTo(write);const reached = fromMain.isReachable(parseArgs); // trueBfsDistances fromMain = BfsDistances.Of(calls, main);int distance = fromMain.DistanceOf(write); // 3 - three calls away// [0, 2, 4, 9] - main, run, save, writeImmutableArray<int> path = fromMain.PathTo(write);bool reached = fromMain.IsReachable(parseArgs); // truewrite is three calls away. BfsDistances.pathToBfsDistances::path_toBfsDistances.pathToBfsDistances.PathTo lists the nodes of the path
from the start node to write. They are the green methods of the picture after: main calls run,
run calls save and save calls write. BfsDistances.isReachableBfsDistances::is_reachableBfsDistances.isReachableBfsDistances.IsReachable tells
whether the search reaches a node at all. It reaches parseArgs.
Good to know
Section titled “Good to know”- One search, all nodes. The search computes the distances of all nodes at once, so you can ask
it about as many nodes as you like. A node it does not reach has no distance.
BfsDistances.distanceOfBfsDistances::distance_ofBfsDistances.distanceOfBfsDistances.DistanceOfreturns-1None-1-1for such a node, andBfsDistances.pathToBfsDistances::path_toBfsDistances.pathToBfsDistances.PathToreturns an empty list. - Several shortest paths. If there are several,
BfsDistances.pathToBfsDistances::path_toBfsDistances.pathToBfsDistances.PathToreturns the one the search finds first. The search goes through the out-nodes of each node in ascending order. - Only which nodes are reached?
Graphs.reachableFromgraphs::reachable_fromGraphs.reachableFromGraphs.ReachableFromreturns them with less memory than a search that keeps distances and paths. See Find everything a node reaches. - Several start nodes, and the largest distance. See Find the distances from several start nodes, and the largest distance.
- Against the direction of the edges, pass the transposed graph. See Find everything that reaches a node.
API used
Section titled “API used”BfsDistances.of(Graph, int)BfsDistances::ofBfsDistances.of(graph: Graph, source: number)BfsDistances.Of(Graph, int)BfsDistances.distanceOf(int)BfsDistances::distance_ofBfsDistances.distanceOfBfsDistances.DistanceOfBfsDistances.pathTo(int)BfsDistances::path_toBfsDistances.pathToBfsDistances.PathToBfsDistances.isReachable(int)BfsDistances::is_reachableBfsDistances.isReachableBfsDistances.IsReachable