Class ShortestPath<N>
- Type Parameters:
N- the type of graph nodes
Compared to traditional imperative implementations, this incremental algorithm supports more flexible use cases that'd otherwise require either full traversal of the graph (which can be large), or copying and tweaking the implementation code for each individual use case.
For example, without traversing the entire map, find 3 nearest Sushi restaurants:
List<Location> sushiPlaces = shortestPathsFrom(myLocation, Location::locationsAroundMe)
.map(ShortestPath::to)
.filter(this::isSushiRestaurant)
.limit(3)
.collect(toList());
Or, find all gas stations within 5 miles:
List<Location> gasStations = shortestPathsFrom(myLocation, Location::locationsAroundMe)
.takeWhile(path -> path.distance() <= 5)
.map(ShortestPath::to)
.filter(this::isGasStation)
.collect(toList());
The streams returned by this class are not safe to run in parallel.
- Since:
- 4.0
-
Method Summary
Modifier and TypeMethodDescriptiondoubledistance()Returns the non-negative distance between the starting node and thelast nodeof this path.static <N> Stream<ShortestPath<N>> shortestPathsFrom(N startNode, Function<? super N, ? extends BiStream<? extends N, Double>> findSuccessors) Returns a lazy stream of shortest paths starting fromstartNode.stream()Returns all nodes from the starting node along this path, with the cumulative distances from the starting node up to each node in the stream, respectively.to()returns the last node of this path.toString()static <N> Stream<ShortestPath<N>> unweightedShortestPathsFrom(N startNode, Function<? super N, ? extends Stream<? extends N>> findSuccessors) Returns a lazy stream of unweighted shortest paths starting fromstartNode.
-
Method Details
-
shortestPathsFrom
public static <N> Stream<ShortestPath<N>> shortestPathsFrom(N startNode, Function<? super N, ? extends BiStream<? extends N, Double>> findSuccessors) Returns a lazy stream of shortest paths starting fromstartNode.The
findSuccessorsfunction is called on-the-fly to find the successors of the current node. It's is expected to return aBiStreamwith these direct neighbor nodes and their distances from the passed-in current node, respectively; null or empty stream if there are no successors.startNodewill correspond to the first element in the returned stream, withdistanceequal to0, followed by the next closest node, etc.- Type Parameters:
N- The node type. Must implementObject.equals(java.lang.Object)andObject.hashCode().
-
unweightedShortestPathsFrom
public static <N> Stream<ShortestPath<N>> unweightedShortestPathsFrom(N startNode, Function<? super N, ? extends Stream<? extends N>> findSuccessors) Returns a lazy stream of unweighted shortest paths starting fromstartNode.The
findSuccessorsfunction is called on-the-fly to find the successors of the current node. It may return Null or empty stream when there are no successors.startNodewill correspond to the first element in the returned stream, withdistanceequal to0, followed by its successor nodes, etc.In the returned stream of
ShortestPathobjects,distancewill be in terms of number of nodes, with the successor nodes of the starting node returning 1.- Type Parameters:
N- The node type. Must implementObject.equals(java.lang.Object)andObject.hashCode().
-
to
returns the last node of this path. -
distance
public double distance()Returns the non-negative distance between the starting node and thelast nodeof this path. Zero for the first path in the stream returned byshortestPathsFrom(N, java.util.function.Function<? super N, ? extends com.google.mu.util.stream.BiStream<? extends N, java.lang.Double>>), in which caseto()will return the starting node. -
stream
Returns all nodes from the starting node along this path, with the cumulative distances from the starting node up to each node in the stream, respectively.Note that while the stream of
ShortestPathobjects are lazy, calling this method will incurO(N)cost of copying all the nodes along this path into a stream. So you should only do this for the path you care about. And if you need to access the nodes on this path repetitively, collect the nodes into a collection first. -
toString
-