Skip to content

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.

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.

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.

The calls, the start nodes and the methods asked about
The methods that load and save reach
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
0: main1: parseArgs2: run3: load4: save5: show6: parse7: format8: read9: write
Input
  • Start node or method asked about
Output
  • Reached method

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 it
IntList path = fromEither.pathTo(format); // [4, 7] - save, format
boolean reached = fromEither.isReachable(main); // false
Full example
// 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 it
let path = from_either.path_to(format); // [4, 7] - save, format
let reached = from_either.is_reachable(main); // false
Full example
// The search starts at both load and save.
const fromEither = BfsDistances.of(calls, [load, save]);
const distance = fromEither.distanceOf(format); // 1 - save calls it
const path = fromEither.pathTo(format); // [4, 7] - save, format
const reached = fromEither.isReachable(main); // false
Full example
// The search starts at both load and save.
BfsDistances fromEither = BfsDistances.Of(calls, [load, save]);
int distance = fromEither.DistanceOf(format); // 1 - save calls it
ImmutableArray<int> path = fromEither.PathTo(format); // [4, 7] - save, format
bool reached = fromEither.IsReachable(main); // false
Full example

Each 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 main
int farthest = fromMain.maxDistance();
Full example
// 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 away
let farthest = from_main.max_distance();
Full example
// 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 main
const farthest = fromMain.maxDistance;
Full example
// 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 main
int farthest = fromMain.MaxDistance;
Full example

Along a shortest chain of calls, every method that main may end up calling is at most three calls away from it.