Class XGraphUtils
- java.lang.Object
- 
- de.xima.fc.utils.XGraphUtils
 
- 
- Direct Known Subclasses:
- GraphAlgos
 
 public class XGraphUtils extends Object Helper methods forGraphandValueGraphdata structures.- Since:
- 8.0.0
- Author:
- XIMA MEDIA GmbH
 
- 
- 
Constructor SummaryConstructors Constructor Description XGraphUtils()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description 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.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>
 VedgeValueOrDefault(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>
 VedgeValueOrDefault(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> Iterable<N>leafNodes(com.google.common.graph.Graph<N> graph)Finds all nodes in the graph that are leaf nodes, i.e.static <N> Iterable<N>leafNodes(com.google.common.graph.ValueGraph<N,?> graph)Finds all nodes in the graph that are leaf nodes, i.e.static <N> Set<N>leafNodeSet(com.google.common.graph.Graph<N> graph)Finds all nodes in the graph that are leaf nodes, i.e.static <N> Set<N>leafNodeSet(com.google.common.graph.ValueGraph<N,?> graph)Finds all nodes in the graph that are leaf nodes, i.e.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.static <N> Iterable<N>rootNodes(com.google.common.graph.ValueGraph<N,?> graph)Finds all nodes in the graph that are root nodes, i.e.static <N> Set<N>rootNodeSet(com.google.common.graph.Graph<N> graph)Finds all nodes in the graph that are root nodes, i.e.static <N> Set<N>rootNodeSet(com.google.common.graph.ValueGraph<N,?> graph)Finds all nodes in the graph that are root nodes, i.e.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> 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.static <N> NtreeRoot(com.google.common.graph.ValueGraph<N,?> graph)Finds the root node of the given tree, i.e.
 
- 
- 
- 
Method Detail- 
arrivingNodespublic 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
 
 - 
arrivingNodespublic static <N> Set<N> arrivingNodes(com.google.common.graph.Graph<N> graph, N startNode) 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.
 
 - 
arrivingNodespublic 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 return- falseis 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 returns- true; 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
 
 - 
edgeValuepublic 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.
 
 - 
edgeValuepublic 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.
 
 - 
edgeValueOrDefaultpublic 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.
 
 - 
edgeValueOrDefaultpublic 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.
 
 - 
immutableFromTreeAccessorpublic 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.- 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 node
- treeAccessor- 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.
 
 - 
leafNodespublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
 
 - 
leafNodespublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
 
 - 
leafNodeSetpublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
 
 - 
leafNodeSetpublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All leaf nodes in the given graph.
- Since:
- 8.1.0
 
 - 
predecessorspublic 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.- 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.
 
 - 
predecessorspublic 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.- 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.
 
 - 
reachableNodespublic 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
 
 - 
reachableNodespublic 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. 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.
 
 - 
reachableNodespublic 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 he 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 return- falseis 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 returns- true; 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
 
 - 
rootNodespublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
 
 - 
rootNodespublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
 
 - 
rootNodeSetpublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
 
 - 
rootNodeSetpublic 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.- Type Parameters:
- N- Type of the nodes.
- Parameters:
- graph- Graph to process.
- Returns:
- All root nodes in the given graph.
- Since:
- 8.1.0
 
 - 
successorspublic 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.- 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.
 
 - 
successorspublic 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.- 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.
 
 - 
transitiveClosurepublic 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.
 
 - 
treeRootpublic 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
 
 - 
treeRootpublic 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
 
 
- 
 
-