4.4 Find the distances from several start nodes, and the largest distance
You get the distance of each node from the nearest of several start nodes, and the largest distance a search reaches.
Problem
Section titled “Problem”You have several start nodes and want to know how far each node is from the nearest of them. You may also want to know how far a search goes, that is, the largest distance of any node it reaches. Such questions come up for:
- the nearest of several servers or exits, for every place of a network;
- the methods that may end up calling one of several changed methods, to review the nearest first;
- how many calls away from an entry point the farthest method lies, or after how many steps a message has reached every node it can reach.
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 far is each method from the nearer of the two methods
load and save? And how many calls away from main is the farthest method? The picture after
shows in green the methods that the search from load and save reaches.
- Start node or method asked about
- Reached method
Solution
Section titled “Solution”BfsDistances.ofBfsDistances::of_sourcesBfsDistances.ofBfsDistances.Of searches from both start nodes at once:
// The search starts at both load and save.BfsDistances fromEither = BfsDistances.of(calls, IntList.of(load, save));int distance = fromEither.distanceOf(format); // 1 - save calls itIntList path = fromEither.pathTo(format); // [4, 7] - save, formatboolean reached = fromEither.isReachable(main); // false// The search starts at both load and save.let from_either = BfsDistances::of_sources(&calls, &[load, save]);let distance = from_either.distance_of(format); // Some(1) - save calls itlet path = from_either.path_to(format); // [4, 7] - save, formatlet reached = from_either.is_reachable(main); // false// The search starts at both load and save.const fromEither = BfsDistances.of(calls, [load, save]);const distance = fromEither.distanceOf(format); // 1 - save calls itconst path = fromEither.pathTo(format); // [4, 7] - save, formatconst reached = fromEither.isReachable(main); // false// The search starts at both load and save.BfsDistances fromEither = BfsDistances.Of(calls, [load, save]);int distance = fromEither.DistanceOf(format); // 1 - save calls itImmutableArray<int> path = fromEither.PathTo(format); // [4, 7] - save, formatbool reached = fromEither.IsReachable(main); // falseEach method gets its distance from the nearer start node. format is one call from save, and
the path to it starts at save. main is reached by neither start node, so
it has no distance, and BfsDistances.isReachableBfsDistances::is_reachableBfsDistances.isReachableBfsDistances.IsReachable returns false for it.
The largest distance tells how far a search goes. From main, it is the number of calls on a
shortest chain to the farthest method:
// How many calls away from main is the farthest method?BfsDistances fromMain = BfsDistances.of(calls, main);// 3 - every method is at most 3 calls away from mainint farthest = fromMain.maxDistance();// How many calls away from main is the farthest method?let from_main = BfsDistances::of(&calls, main);// Some(3) - every method is at most 3 calls awaylet farthest = from_main.max_distance();// How many calls away from main is the farthest method?const fromMain = BfsDistances.of(calls, main);// 3 - every method is at most 3 calls away from mainconst farthest = fromMain.maxDistance;// How many calls away from main is the farthest method?BfsDistances fromMain = BfsDistances.Of(calls, main);// 3 - every method is at most 3 calls away from mainint farthest = fromMain.MaxDistance;Along a shortest chain of calls, every method that main may end up calling is at most three calls
away from it.
Good to know
Section titled “Good to know”- Start nodes have the distance 0. A path starts at the nearest start node. If several are equally near, it starts at the one that comes first in the list. A start node given twice counts once.
- Sources. Graphity calls the start nodes sources. They have nothing to do with the source of an edge.
- Without start nodes, no node is reached, and
BfsDistances.maxDistanceBfsDistances::max_distanceBfsDistances.maxDistanceBfsDistances.MaxDistancereturns-1None-1-1. - Distances to the start nodes instead of from them need the transposed graph. An example is the distance from every method to the nearest changed method it may end up calling. See Find everything that reaches a node for how to search the transposed graph.
- Not the longest path. The largest distance counts the edges of shortest paths. A longer
chain of calls can exist, such as
main -> run -> save -> format -> format. A path through a cycle can be as long as you like. In a DAG (directed acyclic graph), the number of edges on a longest path from a root node to a node is called its level. See Find which nodes can be processed in parallel. - Between all pairs of nodes, run one search per node.
- From one start node. See Find a shortest path between two nodes for
BfsDistances.distanceOfBfsDistances::distance_ofBfsDistances.distanceOfBfsDistances.DistanceOfandBfsDistances.pathToBfsDistances::path_toBfsDistances.pathToBfsDistances.PathTofrom a single start node.
API used
Section titled “API used”BfsDistances.of(Graph, IntList)BfsDistances::of_sourcesBfsDistances.of(graph: Graph, sources: IntList)BfsDistances.Of(Graph, ReadOnlySpan<int>)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.IsReachableBfsDistances.maxDistance()BfsDistances::max_distanceBfsDistances.maxDistanceBfsDistances.MaxDistance