N- Node parameter type
@Beta public interface PredecessorsFunction<N>
This interface is meant to be used as the type of a parameter to graph algorithms (such as topological sort) that only need a way of accessing the predecessors of a node in a graph.
you will invoke it depending on the graph representation you're using.
public <N> someGraphAlgorithm(N startNode, PredecessorsFunction<N> predecessorsFunction);
If you have an instance of one of the primary
common.graph types (
This works because those types each implement
PredecessorsFunction. It will also work with any other implementation of this interface.
If you have your own graph implementation based around a custom node type
which has a method
getParents() that retrieves its predecessors in a graph:
If you have some other mechanism for returning the predecessors of a node, or one that doesn't
Iterable<? extends N>, then you can use a lambda to perform a more general
someGraphAlgorithm(startNode, node -> ImmutableList.of(node.mother(), node.father()));
Graph algorithms that need additional capabilities (accessing both predecessors and
successors, iterating over the edges, etc.) should declare their input to be of a type that
provides those capabilities, such as
See the Guava User Guide for the
common.graph package ("Graphs Explained") for
additional documentation, including notes for
|Modifier and Type||Method and Description|
Returns all nodes in this graph adjacent to
Iterable<? extends N> predecessors(N node)
nodewhich can be reached by traversing
node's incoming edges against the direction (if any) of the edge.
Some algorithms that operate on a
PredecessorsFunction may produce undesired results
if the returned
Iterable contains duplicate elements. Implementations of such
algorithms should document their behavior in the presence of duplicates.
The elements of the returned
Iterable must each be:
Mapkeys (see the Guava User Guide's section on graph elements for details)
nodeis not an element of this graph
Copyright © 2010–2018. All rights reserved.