Class Graphs
- java.lang.Object
-
- com.google.common.graph.Graphs
-
@Beta public final class Graphs extends java.lang.Object
- Since:
- 20.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <N> MutableGraph<N>copyOf(Graph<N> graph)Creates a mutable copy ofgraphwith the same nodes and edges.static <N,E>
MutableNetwork<N,E>copyOf(Network<N,E> network)Creates a mutable copy ofnetworkwith the same nodes and edges.static <N,V>
MutableValueGraph<N,V>copyOf(ValueGraph<N,V> graph)Creates a mutable copy ofgraphwith the same nodes, edges, and edge values.static booleanequivalent(Graph<?> graphA, Graph<?> graphB)ReturnstrueifgraphAandgraphBhave the same elements and the same relationships between elements, as exposed via theGraphinterface.static booleanequivalent(Network<?,?> networkA, Network<?,?> networkB)ReturnstrueifnetworkAandnetworkBhave the same elements and the same relationships between elements, as exposed via theNetworkinterface.static booleanequivalent(ValueGraph<?,?> graphA, ValueGraph<?,?> graphB)ReturnstrueifgraphAandgraphBhave the same elements (including edge values) and the same relationships between elements, as exposed via theValueGraphinterface.static booleanhasCycle(Graph<?> graph)Returns true ifgraphhas at least one cycle.static booleanhasCycle(Network<?,?> network)Returns true ifnetworkhas at least one cycle.static <N> MutableGraph<N>inducedSubgraph(Graph<N> graph, java.lang.Iterable<? extends N> nodes)Returns the subgraph ofgraphinduced bynodes.static <N,E>
MutableNetwork<N,E>inducedSubgraph(Network<N,E> network, java.lang.Iterable<? extends N> nodes)Returns the subgraph ofnetworkinduced bynodes.static <N,V>
MutableValueGraph<N,V>inducedSubgraph(ValueGraph<N,V> graph, java.lang.Iterable<? extends N> nodes)Returns the subgraph ofgraphinduced bynodes.static <N> java.util.Set<N>reachableNodes(Graph<N> graph, java.lang.Object node)Returns the set of nodes that are reachable fromnode.static <N> Graph<N>transitiveClosure(Graph<N> graph)Returns the transitive closure ofgraph.static <N> Graph<N>transpose(Graph<N> graph)Returns a view ofgraphwith the direction (if any) of every edge reversed.static <N,E>
Network<N,E>transpose(Network<N,E> network)Returns a view ofnetworkwith the direction (if any) of every edge reversed.static <N,V>
ValueGraph<N,V>transpose(ValueGraph<N,V> graph)Returns a view ofgraphwith the direction (if any) of every edge reversed.
-
-
-
Method Detail
-
hasCycle
public static boolean hasCycle(Graph<?> graph)
Returns true ifgraphhas at least one cycle. A cycle is defined as a non-empty subset of edges in a graph arranged to form a path (a sequence of adjacent outgoing edges) starting and ending with the same node.This method will detect any non-empty cycle, including self-loops (a cycle of length 1).
-
hasCycle
public static boolean hasCycle(Network<?,?> network)
Returns true ifnetworkhas at least one cycle. A cycle is defined as a non-empty subset of edges in a graph arranged to form a path (a sequence of adjacent outgoing edges) starting and ending with the same node.This method will detect any non-empty cycle, including self-loops (a cycle of length 1).
-
transitiveClosure
public static <N> Graph<N> transitiveClosure(Graph<N> graph)
Returns the transitive closure ofgraph. 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.
-
reachableNodes
public static <N> java.util.Set<N> reachableNodes(Graph<N> graph, java.lang.Object node)
Returns the set of nodes that are reachable fromnode. 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 always reachable from itself via a zero-length path.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.- Throws:
java.lang.IllegalArgumentException- ifnodeis not present ingraph
-
equivalent
public static boolean equivalent(@Nullable Graph<?> graphA, @Nullable Graph<?> graphB)ReturnstrueifgraphAandgraphBhave the same elements and the same relationships between elements, as exposed via theGraphinterface.Thus, two graphs A and B are equivalent if both are null or all of the following are true:
- A and B have equal
directedness. - A and B have equal
node sets. - A and B have equal
edge sets.
Graph properties besides
directednessdo not affect equivalence. For example, two graphs may be considered equivalent even if one allows self-loops and the other doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order in which they are iterated over, are irrelevant. - A and B have equal
-
equivalent
public static boolean equivalent(@Nullable ValueGraph<?,?> graphA, @Nullable ValueGraph<?,?> graphB)ReturnstrueifgraphAandgraphBhave the same elements (including edge values) and the same relationships between elements, as exposed via theValueGraphinterface.Thus, two value graphs A and B are equivalent if both are null or all of the following are true:
- A and B have equal
directedness. - A and B have equal
node sets. - A and B have equal
edge sets. - Each edge in A has a
valueequal to thevalueof the corresponding edge in B.
Graph properties besides
directednessdo not affect equivalence. For example, two graphs may be considered equivalent even if one allows self-loops and the other doesn't. Additionally, the order in which nodes or edges are added to the graph, and the order in which they are iterated over, are irrelevant. - A and B have equal
-
equivalent
public static boolean equivalent(@Nullable Network<?,?> networkA, @Nullable Network<?,?> networkB)ReturnstrueifnetworkAandnetworkBhave the same elements and the same relationships between elements, as exposed via theNetworkinterface.Thus, two networks A and B are equivalent if both are null or all of the following are true:
- A and B have equal
directedness. - A and B have equal
node sets. - A and B have equal
edge sets. - Each edge in A connects the same nodes in the same direction (if any) as the corresponding edge in B.
Network properties besides
directednessdo not affect equivalence. For example, two networks may be considered equal even if one allows parallel edges and the other doesn't. Additionally, the order in which nodes or edges are added to the network, and the order in which they are iterated over, are irrelevant. - A and B have equal
-
transpose
public static <N> Graph<N> transpose(Graph<N> graph)
Returns a view ofgraphwith the direction (if any) of every edge reversed. All other properties remain intact, and further updates tographwill be reflected in the view.
-
transpose
public static <N,V> ValueGraph<N,V> transpose(ValueGraph<N,V> graph)
Returns a view ofgraphwith the direction (if any) of every edge reversed. All other properties remain intact, and further updates tographwill be reflected in the view.
-
transpose
public static <N,E> Network<N,E> transpose(Network<N,E> network)
Returns a view ofnetworkwith the direction (if any) of every edge reversed. All other properties remain intact, and further updates tonetworkwill be reflected in the view.
-
inducedSubgraph
public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, java.lang.Iterable<? extends N> nodes)
Returns the subgraph ofgraphinduced bynodes. This subgraph is a new graph that contains all of the nodes innodes, and all of theedgesfromgraphfor which both nodes are contained bynodes.- Throws:
java.lang.IllegalArgumentException- if any element innodesis not a node in the graph
-
inducedSubgraph
public static <N,V> MutableValueGraph<N,V> inducedSubgraph(ValueGraph<N,V> graph, java.lang.Iterable<? extends N> nodes)
Returns the subgraph ofgraphinduced bynodes. This subgraph is a new graph that contains all of the nodes innodes, and all of theedges(and associated edge values) fromgraphfor which both nodes are contained bynodes.- Throws:
java.lang.IllegalArgumentException- if any element innodesis not a node in the graph
-
inducedSubgraph
public static <N,E> MutableNetwork<N,E> inducedSubgraph(Network<N,E> network, java.lang.Iterable<? extends N> nodes)
Returns the subgraph ofnetworkinduced bynodes. This subgraph is a new graph that contains all of the nodes innodes, and all of theedgesfromnetworkfor which theincident nodesare both contained bynodes.- Throws:
java.lang.IllegalArgumentException- if any element innodesis not a node in the graph
-
copyOf
public static <N> MutableGraph<N> copyOf(Graph<N> graph)
Creates a mutable copy ofgraphwith the same nodes and edges.
-
copyOf
public static <N,V> MutableValueGraph<N,V> copyOf(ValueGraph<N,V> graph)
Creates a mutable copy ofgraphwith the same nodes, edges, and edge values.
-
copyOf
public static <N,E> MutableNetwork<N,E> copyOf(Network<N,E> network)
Creates a mutable copy ofnetworkwith the same nodes and edges.
-
-