Class Graph<N,E>
- Type Parameters:
N
- Value type that the graph node stores.E
- Value type that the graph edge stores.
- All Implemented Interfaces:
AdjacencyGraph<N,
E>
Nodes and edges in the graph can store a piece of data that this graph is used to represent.
For example, a variable interference graph might store a variable in the node. This piece of data
can be accessed with GraphNode.getValue()
and Graph.GraphEdge.getValue()
. However, in some
cases wrapping the nodes and/or edges in a custom class may be preferrable, as this additional
piece of data must be explicitly cast down from Annotation
to its true type.
Algorithms and analysis can annotate information on the nodes and edges using GraphNode.getValue()
and Graph.GraphEdge.getValue()
. For example, a graph coloring algorithm can
store the color as an annotation. If multiple analyses are required, it is up to the user of the
analysis to save the annotated solution between passes.
We implemented our own graph data structure (as opposed to using com.google.common.graph
) for three reasons. First, aside from the node's label value, we would like to annotate
information on the nodes and edges. Using a map to annotate would introduce too much overhead
during fix point analysis. Also, com.google.common.graph
does not support labeling
of edges. Second, avoiding using an external package limits our dependencies. Third,
com.google.common.graph
uses WeakRefs which J2CL does not support.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal void
Makes each edge's annotation null.final void
Makes each node's annotation null.abstract void
Connects two nodes in the graph with an edge.final void
connectIfNotFound
(N n1, E edge, N n2) Connects two nodes in the graph with an edge if such edge does not already exists between the nodes.createNode
(N value) Gets a node from the graph given a value.abstract void
disconnect
(N n1, N n2) Disconnects two nodes in the graph by removing all edges between them.abstract List
<? extends Graph.GraphEdge<N, E>> getEdges()
Gets an immutable list of all edges.abstract List
<? extends Graph.GraphEdge<N, E>> Retrieves an edge from the graph.abstract Graph.GraphEdge
<N, E> getFirstEdge
(N n1, N n2) Retrieves any edge from the graph.getNeighborNodes
(N value) Gets the neighboring nodes.abstract int
abstract int
getNodeDegree
(N value) Gets the degree of a node.abstract Collection
<? extends GraphNode<N, E>> getNodes()
Gets an immutable list of all nodes.int
Returns a weight for the given value to be used in ordering nodes, e.g.final boolean
Checks whether the node exists in the graph (createNode(Object)
has been called with that value).abstract boolean
isConnected
(N n1, E e, N n2) Checks whether two nodes in the graph are connected by the given edge type.abstract boolean
isConnected
(N n1, N n2) Checks whether two nodes in the graph are connected.final void
Restores edges' annotation values to state before lastpushEdgeAnnotations()
.final void
Restores nodes' annotation values to state before lastpushNodeAnnotations()
.final void
Pushes edges' annotation values.final void
Pushes nodes' annotation values.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.google.javascript.jscomp.graph.AdjacencyGraph
getNode, newSubGraph
-
Constructor Details
-
Graph
public Graph()
-
-
Method Details
-
connect
Connects two nodes in the graph with an edge.- Parameters:
n1
- First node.edge
- The edge.n2
- Second node.
-
disconnect
Disconnects two nodes in the graph by removing all edges between them.- Parameters:
n1
- First node.n2
- Second node.
-
connectIfNotFound
Connects two nodes in the graph with an edge if such edge does not already exists between the nodes.- Parameters:
n1
- First node.edge
- The edge.n2
- Second node.
-
createNode
Gets a node from the graph given a value. New nodes are created if that value has not been assigned a graph node. Values equality are compared usingObject.equals
.- Parameters:
value
- The node's value.- Returns:
- The corresponding node in the graph.
-
getNodes
Gets an immutable list of all nodes.- Specified by:
getNodes
in interfaceAdjacencyGraph<N,
E>
-
getNodeCount
public abstract int getNodeCount()- Specified by:
getNodeCount
in interfaceAdjacencyGraph<N,
E>
-
getEdges
Gets an immutable list of all edges. -
getEdges
Retrieves an edge from the graph.- Parameters:
n1
- Node one.n2
- Node two.- Returns:
- The list of edges between those two values in the graph.
-
getNodeDegree
Gets the degree of a node.- Parameters:
value
- The node's value.- Returns:
- The degree of the node.
-
getWeight
Description copied from interface:AdjacencyGraph
Returns a weight for the given value to be used in ordering nodes, e.g. inGraphColoring
.- Specified by:
getWeight
in interfaceAdjacencyGraph<N,
E>
-
getNeighborNodes
Gets the neighboring nodes.- Parameters:
value
- The node's value.- Returns:
- A list of neighboring nodes.
-
getFirstEdge
Retrieves any edge from the graph.- Parameters:
n1
- Node one.n2
- Node two.- Returns:
- The first edges between those two values in the graph. null if there are none.
-
hasNode
Checks whether the node exists in the graph (createNode(Object)
has been called with that value).- Parameters:
n
- Node.- Returns:
true
if it exist.
-
isConnected
Checks whether two nodes in the graph are connected.- Parameters:
n1
- Node 1.n2
- Node 2.- Returns:
true
if the two nodes are connected.
-
isConnected
Checks whether two nodes in the graph are connected by the given edge type.- Parameters:
n1
- Node 1.e
- The edge type.n2
- Node 2.
-
clearNodeAnnotations
public final void clearNodeAnnotations()Description copied from interface:AdjacencyGraph
Makes each node's annotation null.- Specified by:
clearNodeAnnotations
in interfaceAdjacencyGraph<N,
E>
-
clearEdgeAnnotations
public final void clearEdgeAnnotations()Makes each edge's annotation null. -
pushNodeAnnotations
public final void pushNodeAnnotations()Pushes nodes' annotation values. Restored withpopNodeAnnotations()
. Nodes' annotation values are cleared. -
popNodeAnnotations
public final void popNodeAnnotations()Restores nodes' annotation values to state before lastpushNodeAnnotations()
. -
pushEdgeAnnotations
public final void pushEdgeAnnotations()Pushes edges' annotation values. Restored withpopEdgeAnnotations()
. Edges' annotation values are cleared. -
popEdgeAnnotations
public final void popEdgeAnnotations()Restores edges' annotation values to state before lastpushEdgeAnnotations()
.
-