Class XGraphUtils
- Direct Known Subclasses:
GraphAlgos
Graph and ValueGraph data structures.- Since:
- 8.0.0
- Author:
- XIMA MEDIA GmbH
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <N> Set<N> arrivingNodes(com.google.common.graph.Graph<N> graph, Iterable<? extends N> startNodes) Returns the set of nodes from which thenodecan be reached.static <N> Set<N> arrivingNodes(com.google.common.graph.Graph<N> graph, N startNode) Returns the set of nodes from which thenodecan be reached.static <N,V> Set <N> arrivingNodes(com.google.common.graph.ValueGraph<N, ? extends V> graph, Iterable<? extends N> startNodes, IValueGraphEdgePredicate<? super N, ? super V> edgeFilter) Returns the set of nodes from which thenodecan be reached.static <N,V> Optional <V> edgeValue(com.google.common.graph.ValueGraph<N, V> graph, com.google.common.graph.EndpointPair<N> endpoints) Similar toValueGraph.edgeValueOrDefault(EndpointPair, Object), but does not throw when the nodes do not exist in the graph and returns the an empty optional instead.static <N,V> Optional <V> edgeValue(com.google.common.graph.ValueGraph<N, V> graph, N nodeU, N nodeV) Similar toValueGraph.edgeValue(EndpointPair), but does not throw when the nodes do not exist in the graph and returns an empty optional instead.static <N,V> V edgeValueOrDefault(com.google.common.graph.ValueGraph<N, V> graph, com.google.common.graph.EndpointPair<N> endpoints, V defaultValue) Similar toValueGraph.edgeValueOrDefault(EndpointPair, Object), but does not throw when the nodes do not exist in the graph and returns the default value instead.static <N,V> V edgeValueOrDefault(com.google.common.graph.ValueGraph<N, V> graph, N nodeU, N nodeV, V defaultValue) Similar toValueGraph.edgeValueOrDefault(EndpointPair, Object), but does not throw when the nodes do not exist in the graph and returns the default value instead.static <N,I, R> com.google.common.graph.ImmutableGraph <R> immutableFromTreeAccessor(N startNode, ITreeAccessor<N, I> treeAccessor, com.google.common.graph.GraphBuilder<Object> builder, BiFunction<N, I, R> mapper) Creates a directed graph from a given start node and its children, recursively.static <N,I, R> com.google.common.graph.ImmutableGraph <R> immutableFromTreeAccessor(N startNode, ITreeAccessor<N, I> treeAccessor, com.google.common.graph.GraphBuilder<Object> builder, BiFunction<N, I, R> mapper) Deprecated.static <N> Iterable<N> leafNodes(com.google.common.graph.Graph<N> graph) Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.static <N> Iterable<N> leafNodes(com.google.common.graph.ValueGraph<N, ?> graph) Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.static <N> Set<N> leafNodeSet(com.google.common.graph.Graph<N> graph) Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.static <N> Set<N> leafNodeSet(com.google.common.graph.ValueGraph<N, ?> graph) Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.static <N> Set<N> predecessors(com.google.common.graph.Graph<N> graph, N node) Same asGraph.predecessors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.static <N,V> Set <N> predecessors(com.google.common.graph.ValueGraph<N, V> graph, N node) Same asValueGraph.predecessors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.static <N> Set<N> reachableNodes(com.google.common.graph.Graph<N> graph, Iterable<? extends N> startNodes) Taken fromGraphs.reachableNodes(Graph, Object), but fixed so that a node is only reachable from itself when there exists has a 0+ loop cycle with the loop.static <N> Set<N> reachableNodes(com.google.common.graph.Graph<N> graph, N startNode) Taken fromGraphs.reachableNodes(Graph, Object), but fixed so that a node is only reachable from itself when there exists has a 0+ loop cycle with the loop.static <N,V> Set <N> reachableNodes(com.google.common.graph.ValueGraph<N, ? extends V> graph, Iterable<? extends N> startNodes, IValueGraphEdgePredicate<? super N, ? super V> edgeFilter) Taken fromGraphs.reachableNodes(Graph, Object), but for value graphs and fixed so that a node is only reachable from itself when there exists has a 0+ loop cycle with the loop.static <N> Iterable<N> rootNodes(com.google.common.graph.Graph<N> graph) Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.static <N> Iterable<N> rootNodes(com.google.common.graph.ValueGraph<N, ?> graph) Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.static <N> Set<N> rootNodeSet(com.google.common.graph.Graph<N> graph) Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.static <N> Set<N> rootNodeSet(com.google.common.graph.ValueGraph<N, ?> graph) Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.static <N> Set<N> successors(com.google.common.graph.Graph<N> graph, N node) Same asGraph.successors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.static <N,V> Set <N> successors(com.google.common.graph.ValueGraph<N, V> graph, N node) Same asValueGraph.successors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.static <N> List<N> topologicalSort(com.google.common.graph.Graph<N> graph) Performs a topological sort on the given graph, returning the sorted nodes as a list.static <N> List<N> topologicalSort(com.google.common.graph.Graph<N> graph, Comparator<N> comparator) Performs a topological sort on the given graph, returning the sorted nodes as a list.static <N> com.google.common.graph.Graph<N> transitiveClosure(com.google.common.graph.Graph<N> graph) Taken fromGraphs.transitiveClosure(Graph), but fixed so it does not add self-loops.static <N> NtreeRoot(com.google.common.graph.Graph<N> graph) Finds the root node of the given tree, i.e. the first of allrootNodes(Graph).static <N> NtreeRoot(com.google.common.graph.ValueGraph<N, ?> graph) Finds the root node of the given tree, i.e. the first of allrootNodes(Graph).
-
Constructor Details
-
XGraphUtils
public XGraphUtils()
-
-
Method Details
-
arrivingNodes
public static <N> Set<N> arrivingNodes(com.google.common.graph.Graph<N> graph, Iterable<? extends N> startNodes) Returns the set of nodes from which thenodecan be reached. Node B is defined as reachable from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A and ending at node B. Note that a node is only reachable from itself when there exists a 0+ cycle with node in the graph.This is a "snapshot" based on the current topology of
graph, rather than a live view of the set of nodes reachable fromnode. In other words, the returnedSetwill not be updated after modifications tograph.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to traverse.startNodes- Nodes where to start traversal.- Returns:
- A mutable set with all nodes that are from which the given start node can be reached by traversing the edges of the graph. This will not include the node itself, unless a 0+ cycle with the node exists in the graph.
- Since:
- 8.0.0
-
arrivingNodes
Returns the set of nodes from which thenodecan be reached. Node B is defined as reachable from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A and ending at node B. Note that a node is only reachable from itself when there exists a 0+ cycle with node in the graph.This is a "snapshot" based on the current topology of
graph, rather than a live view of the set of nodes reachable fromnode. In other words, the returnedSetwill not be updated after modifications tograph.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to traverse.startNode- Node where to start traversal.- Returns:
- A mutable set with all nodes that are from which the given start node can be reached by traversing the edges of the graph. This will not include the node itself, unless a 0+ cycle with the node exists in the graph.
-
arrivingNodes
public static <N,V> Set<N> arrivingNodes(com.google.common.graph.ValueGraph<N, ? extends V> graph, Iterable<? extends N> startNodes, IValueGraphEdgePredicate<? super N, ? super V> edgeFilter) Returns the set of nodes from which thenodecan be reached. Node B is defined as reachable from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A and ending at node B. Note that a node is only reachable from itself when there exists a 0+ cycle with node in the graph.This is a "snapshot" based on the current topology of
graph, rather than a live view of the set of nodes reachable fromnode. In other words, the returnedSetwill not be updated after modifications tograph.- Type Parameters:
N- Type of the nodes in the graph.V- Type of the values associated with the edges in he graph.- Parameters:
graph- Graph to traverse.startNodes- Nodes where to start traversal.edgeFilter- Filter for the edges to respect when computing the arriving nodes. Each edge for which this filter returnfalseis excluded. In other words, this method behaves as if it would first construct a new graph with the same nodes of the original graph and those edge for which the filter returnstrue; and then compute the arriving nodes on that new graph.- Returns:
- A mutable set with all nodes that are from which the given start node can be reached by traversing the edges of the graph. This will not include the nodes themselves, unless a 0+ cycle with the node exists in the graph.
- Since:
- 8.0.0
-
edgeValue
public static <N,V> Optional<V> edgeValue(com.google.common.graph.ValueGraph<N, V> graph, com.google.common.graph.EndpointPair<N> endpoints) Similar toValueGraph.edgeValueOrDefault(EndpointPair, Object), but does not throw when the nodes do not exist in the graph and returns the an empty optional instead.- Type Parameters:
N- Type of the nodes in the graph.V- Type of the values of each node.- Parameters:
graph- Graph to process.endpoints- End points of the edge.- Returns:
- The value for the edge, or empty when not found.
-
edgeValue
public static <N,V> Optional<V> edgeValue(com.google.common.graph.ValueGraph<N, V> graph, N nodeU, N nodeV) Similar toValueGraph.edgeValue(EndpointPair), but does not throw when the nodes do not exist in the graph and returns an empty optional instead.- Type Parameters:
N- Type of the nodes in the graph.V- Type of the values of each node.- Parameters:
graph- Graph to process.nodeU- Start point of the edge.nodeV- End point of the edge.- Returns:
- The value for the edge, or empty when not found.
-
edgeValueOrDefault
public static <N,V> V edgeValueOrDefault(com.google.common.graph.ValueGraph<N, V> graph, com.google.common.graph.EndpointPair<N> endpoints, V defaultValue) Similar toValueGraph.edgeValueOrDefault(EndpointPair, Object), but does not throw when the nodes do not exist in the graph and returns the default value instead.- Type Parameters:
N- Type of the nodes in the graph.V- Type of the values of each node.- Parameters:
graph- Graph to process.endpoints- End points of the edge.defaultValue- Default value to return when no data is found.- Returns:
- The value for the edge, or the default value when not found.
-
edgeValueOrDefault
public static <N,V> V edgeValueOrDefault(com.google.common.graph.ValueGraph<N, V> graph, N nodeU, N nodeV, V defaultValue) Similar toValueGraph.edgeValueOrDefault(EndpointPair, Object), but does not throw when the nodes do not exist in the graph and returns the default value instead.- Type Parameters:
N- Type of the nodes in the graph.V- Type of the values of each node.- Parameters:
graph- Graph to process.nodeU- Start point of the edge.nodeV- End point of the edge.defaultValue- Default value to return when no data is found.- Returns:
- The value for the edge, or the default value when not found.
-
immutableFromTreeAccessor
public static <N,I, com.google.common.graph.ImmutableGraph<R> immutableFromTreeAccessorR> (N startNode, ITreeAccessor<N, I> treeAccessor, com.google.common.graph.GraphBuilder<Object> builder, BiFunction<N, I, R> mapper) Creates a directed graph from a given start node and its children, recursively. An edge p -> n in the graph means that node p is a parent of node n.- Type Parameters:
N- Type of the input nodes.I- Type of the ID of each node.R- Type of the output nodes.- Parameters:
startNode- Start or root nodetreeAccessor- How to access the children of an input node.builder- Graph builder to use for creating a new graph.mapper- How to map an input node o an output node.- Returns:
- A graph with one edge for each (parent, child) from the input.
- Since:
- 8.4.0
-
immutableFromTreeAccessor
@Deprecated public static <N,I, com.google.common.graph.ImmutableGraph<R> immutableFromTreeAccessorR> (N startNode, ITreeAccessor<N, I> treeAccessor, com.google.common.graph.GraphBuilder<Object> builder, BiFunction<N, I, R> mapper) Deprecated.Creates a directed graph from a given start node and its children, recursively.- Type Parameters:
N- Type of the input nodes.I- Type of the ID of each node.R- Type of the output nodes.- Parameters:
startNode- Start or root nodetreeAccessor- How to access the children of an input node.builder- Graph builder to use for creating a new graph.mapper- How to map an input node o an output node.- Returns:
- A graph with one edge for each (parent, child)from the input.
-
leafNodeSet
Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
-
leafNodeSet
Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
-
leafNodes
Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
-
leafNodes
Finds all nodes in the graph that are leaf nodes, i.e. that do not have outgoing edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
-
predecessors
Same asGraph.predecessors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to process.node- A node of the graph.- Returns:
- The set of predecessors of the node, empty when the graph does not contain the node.
-
predecessors
Same asValueGraph.predecessors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to process.node- A node of the graph.- Returns:
- The set of predecessors of the node, empty when the graph does not contain the node.
-
reachableNodes
public static <N> Set<N> reachableNodes(com.google.common.graph.Graph<N> graph, Iterable<? extends N> startNodes) Taken fromGraphs.reachableNodes(Graph, Object), but fixed so that a node is only reachable from itself when there exists has a 0+ loop cycle with the loop. Also does not throw an illegal argument exception when the node is not contained in the graph.Returns the set of nodes that are reachable from
node. Node B is defined as reachable from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A and ending at node B. Note that a node is only reachable from itself when there exists a 0+ cycle with node in the graph.This is a "snapshot" based on the current topology of
graph, rather than a live view of the set of nodes reachable fromnode. In other words, the returnedSetwill not be updated after modifications tograph.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to traverse.startNodes- Nodes where to start traversal.- Returns:
- A mutable set with all nodes that are reachable from the given start node by traversing the edges of the graph. This will not include the node itself, unless a 0+ cycle with the node exists in the graph.
- Since:
- 8.0.0
-
reachableNodes
Taken fromGraphs.reachableNodes(Graph, Object), but fixed so that a node is only reachable from itself when there exists has a 0+ loop cycle with the loop. Also does not throw an illegal argument exception when the node is not contained in the graph.Returns the set of nodes that are reachable from
node. Node B is defined as reachable from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A and ending at node B. Note that a node is only reachable from itself when there exists a 0+ cycle with node in the graph.This is a "snapshot" based on the current topology of
graph, rather than a live view of the set of nodes reachable fromnode. In other words, the returnedSetwill not be updated after modifications tograph.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to traverse.startNode- Node where to start traversal.- Returns:
- A mutable set with all nodes that are reachable from the given start node by traversing the edges of the graph. This will not include the node itself, unless a 0+ cycle with the node exists in the graph.
-
reachableNodes
public static <N,V> Set<N> reachableNodes(com.google.common.graph.ValueGraph<N, ? extends V> graph, Iterable<? extends N> startNodes, IValueGraphEdgePredicate<? super N, ? super V> edgeFilter) Taken fromGraphs.reachableNodes(Graph, Object), but for value graphs and fixed so that a node is only reachable from itself when there exists has a 0+ loop cycle with the loop. Also does not throw an illegal argument exception when the node is not contained in the graph.Returns the set of nodes that are reachable from
node. Node B is defined as reachable from node A if there exists a path (a sequence of adjacent outgoing edges) starting at node A and ending at node B. Note that a node is only reachable from itself when there exists a 0+ cycle with node in the graph.This is a "snapshot" based on the current topology of
graph, rather than a live view of the set of nodes reachable fromnode. In other words, the returnedSetwill not be updated after modifications tograph.- Type Parameters:
N- Type of the nodes in the graph.V- Type of the values associated with the edges in the graph.- Parameters:
graph- Graph to traverse.startNodes- Nodes where to start traversal.edgeFilter- Filter for the edges to respect when computing the reachable nodes. Each edge for which this filter returnfalseis excluded. In other words, this method behaves as if it would first construct a new graph with the same nodes of the original graph and those edge for which the filter returnstrue; and then compute the reachable nodes on that new graph.- Returns:
- A mutable set with all nodes that are reachable from the given start node by traversing the edges of the graph. This will not include the nodes themselves, unless a 0+ cycle with the node exists in the graph.
- Since:
- 8.0.0
-
rootNodeSet
Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
-
rootNodeSet
Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
-
rootNodes
Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
-
rootNodes
Finds all nodes in the graph that are root nodes, i.e. that do not have incoming edges.- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Graph to process.- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
-
successors
Same asGraph.successors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to process.node- A node of the graph.- Returns:
- The set of successors of the node, empty when the graph does not contain the node.
-
successors
Same asValueGraph.successors(Object), but instead of throwing when the graph does not contain the node, returns an empty set.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to process.node- A node of the graph.- Returns:
- The set of successors of the node, empty when the graph does not contain the node.
-
topologicalSort
Performs a topological sort on the given graph, returning the sorted nodes as a list. The graph must be an acyclic-directed graph (DAG); otherwise, anIllegalArgumentExceptionis thrown.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to sort.- Returns:
- The sorted nodes.
- Throws:
IllegalArgumentException- When the graph contains cycles.
-
topologicalSort
public static <N> List<N> topologicalSort(com.google.common.graph.Graph<N> graph, Comparator<N> comparator) Performs a topological sort on the given graph, returning the sorted nodes as a list. The graph must be an acyclic-directed graph (DAG); otherwise, anIllegalArgumentExceptionis thrown. Compared withtopologicalSort(Graph), this method allows to specify a comparator to determine the order of nodes with the same dependency level, resulting in a unique sort order.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to sort.comparator- Comparator to determine the order of nodes with the same dependency level.- Returns:
- The sorted nodes.
- Throws:
IllegalArgumentException- When the graph contains cycles.
-
transitiveClosure
public static <N> com.google.common.graph.Graph<N> transitiveClosure(com.google.common.graph.Graph<N> graph) Taken fromGraphs.transitiveClosure(Graph), but fixed so it does not add self-loops. Can be replaced withGraphs.transitiveClosure(Graph)when https://github.com/google/guava/issues/2778 is resolved.Returns the transitive closure of
graph. The transitive closure of a graph is another graph with an edge connecting node A to node B if node B isreachablefrom node A.This is a "snapshot" based on the current topology of
graph, rather than a live view of the transitive closure ofgraph. In other words, the returnedGraphwill not be updated after modifications tograph.- Type Parameters:
N- Type of the nodes in the graph.- Parameters:
graph- Graph to traverse.- Returns:
- The transitive closure of the given graph.
-
treeRoot
public static <N> N treeRoot(com.google.common.graph.Graph<N> graph) Finds the root node of the given tree, i.e. the first of allrootNodes(Graph). Use this method only when you are certain your graph is a tree!- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Tree to process.- Returns:
- The root node in the given graph, may be
nullwhen the graph is not a tree. - Since:
- 8.1.0
-
treeRoot
public static <N> N treeRoot(com.google.common.graph.ValueGraph<N, ?> graph) Finds the root node of the given tree, i.e. the first of allrootNodes(Graph). Use this method only when you are certain your graph is a tree!- Type Parameters:
N- Type of the nodes.- Parameters:
graph- Tree to process.- Returns:
- The root node in the given graph, may be
nullwhen the graph is not a tree. - Since:
- 8.1.0
-
immutableFromTreeAccessor(Object, ITreeAccessor, GraphBuilder, BiFunction)instead.