Class LinkedDirectedGraph<N,E>
- java.lang.Object
-
- com.google.javascript.jscomp.graph.Graph<N,E>
-
- com.google.javascript.jscomp.graph.DiGraph<N,E>
-
- com.google.javascript.jscomp.graph.LinkedDirectedGraph<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>
,GraphvizGraph
- Direct Known Subclasses:
ControlFlowGraph
public class LinkedDirectedGraph<N,E> extends DiGraph<N,E> implements GraphvizGraph
A directed graph using ArrayLists within nodes to store edge information.This implementation favors directed graph operations inherited from
DirectedGraph
. Operations fromGraph
would tend to be slower.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LinkedDirectedGraph.LinkedDiGraphEdge<N,E>
A directed graph edge that stores the source and destination nodes at each edge.static class
LinkedDirectedGraph.LinkedDiGraphNode<N,E>
A directed graph node that stores outgoing edges and incoming edges as an list within the node itself.-
Nested classes/interfaces inherited from class com.google.javascript.jscomp.graph.DiGraph
DiGraph.DiGraphEdge<N,E>, DiGraph.DiGraphNode<N,E>
-
Nested classes/interfaces inherited from class com.google.javascript.jscomp.graph.Graph
Graph.GraphEdge<N,E>
-
Nested classes/interfaces inherited from interface com.google.javascript.jscomp.graph.GraphvizGraph
GraphvizGraph.GraphvizEdge, GraphvizGraph.GraphvizNode
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<N,LinkedDirectedGraph.LinkedDiGraphNode<N,E>>
nodes
-
Constructor Summary
Constructors Modifier Constructor Description protected
LinkedDirectedGraph(boolean useNodeAnnotations, boolean useEdgeAnnotations)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connect(DiGraph.DiGraphNode<N,E> src, E edgeValue, DiGraph.DiGraphNode<N,E> dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer this method if you have the DiGraphNode available.void
connect(N srcValue, E edgeValue, N destValue)
Connects two nodes in the graph with an edge.void
connectIfNotConnectedInDirection(N srcValue, E edgeValue, N destValue)
DiGraphNode look ups can be expensive for a large graph operation, prefer this method if you have the DiGraphNode available.static <N,E>
LinkedDirectedGraph<N,E>create()
LinkedDirectedGraph.LinkedDiGraphNode<N,E>
createNode(N nodeValue)
Gets a node from the graph given a value.static <N,E>
LinkedDirectedGraph<N,E>createWithoutAnnotations()
void
disconnect(N n1, N n2)
Disconnects two nodes in the graph by removing all edges between them.void
disconnectInDirection(N srcValue, N destValue)
Disconnects all edges from n1 to n2.java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>>
getDirectedPredNodes(DiGraph.DiGraphNode<N,E> dNode)
java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>>
getDirectedPredNodes(N nodeValue)
java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>>
getDirectedSuccNodes(DiGraph.DiGraphNode<N,E> dNode)
java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>>
getDirectedSuccNodes(N nodeValue)
java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>>
getEdges()
Gets an immutable list of all edges.java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>>
getEdges(N n1, N n2)
Retrieves an edge from the graph.java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>>
getEdgesInDirection(N n1, N n2)
@Nullable Graph.GraphEdge<N,E>
getFirstEdge(N n1, N n2)
Retrieves any edge from the graph.java.util.List<GraphvizGraph.GraphvizEdge>
getGraphvizEdges()
Retrieve a list of edges in the graph.java.util.List<GraphvizGraph.GraphvizNode>
getGraphvizNodes()
Retrieve a list of nodes in the graph.java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>>
getInEdges(N nodeValue)
Gets an immutable list of in edges of the given node.java.lang.String
getName()
Name of the graph.java.util.List<GraphNode<N,E>>
getNeighborNodes(N value)
Gets the neighboring nodes.LinkedDirectedGraph.LinkedDiGraphNode<N,E>
getNode(N nodeValue)
Gets a node from the graph given a value.int
getNodeCount()
int
getNodeDegree(N value)
Gets the degree of a node.java.util.Collection<LinkedDirectedGraph.LinkedDiGraphNode<N,E>>
getNodes()
Gets an immutable list of all nodes.java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>>
getOutEdges(N nodeValue)
Gets an immutable list of out edges of the given node.boolean
isConnectedInDirection(LinkedDirectedGraph.LinkedDiGraphNode<N,E> source, com.google.common.base.Predicate<E> edgeFilter, LinkedDirectedGraph.LinkedDiGraphNode<N,E> dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer this method if you have the DiGraphNodes available.boolean
isConnectedInDirection(N source, E edgeValue, N dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer the version below that takes DiGraphNodes, if you have them available.boolean
isConnectedInDirection(N source, N dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer the version below that takes DiGraphNodes, if you have them available.boolean
isDirected()
Graph type.SubGraph<N,E>
newSubGraph()
Returns an empty SubGraph for this Graph.-
Methods inherited from class com.google.javascript.jscomp.graph.DiGraph
isConnected, isConnected
-
Methods inherited from class com.google.javascript.jscomp.graph.Graph
clearEdgeAnnotations, clearNodeAnnotations, connectIfNotFound, getWeight, hasNode, popEdgeAnnotations, popNodeAnnotations, pushEdgeAnnotations, pushNodeAnnotations
-
-
-
-
Field Detail
-
nodes
protected final java.util.Map<N,LinkedDirectedGraph.LinkedDiGraphNode<N,E>> nodes
-
-
Method Detail
-
newSubGraph
public SubGraph<N,E> newSubGraph()
Description copied from interface:AdjacencyGraph
Returns an empty SubGraph for this Graph.- Specified by:
newSubGraph
in interfaceAdjacencyGraph<N,E>
-
createWithoutAnnotations
public static <N,E> LinkedDirectedGraph<N,E> createWithoutAnnotations()
-
create
public static <N,E> LinkedDirectedGraph<N,E> create()
-
connect
public void connect(N srcValue, E edgeValue, N destValue)
Description copied from class:Graph
Connects two nodes in the graph with an edge.
-
connect
public void connect(DiGraph.DiGraphNode<N,E> src, E edgeValue, DiGraph.DiGraphNode<N,E> dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer this method if you have the DiGraphNode available.
-
connectIfNotConnectedInDirection
public void connectIfNotConnectedInDirection(N srcValue, E edgeValue, N destValue)
DiGraphNode look ups can be expensive for a large graph operation, prefer this method if you have the DiGraphNode available.
-
disconnect
public void disconnect(N n1, N n2)
Description copied from class:Graph
Disconnects two nodes in the graph by removing all edges between them.- Specified by:
disconnect
in classGraph<N,E>
- Parameters:
n1
- First node.n2
- Second node.
-
disconnectInDirection
public void disconnectInDirection(N srcValue, N destValue)
Description copied from class:DiGraph
Disconnects all edges from n1 to n2.- Specified by:
disconnectInDirection
in classDiGraph<N,E>
- Parameters:
srcValue
- Source node.destValue
- Destination node.
-
getNodes
public java.util.Collection<LinkedDirectedGraph.LinkedDiGraphNode<N,E>> getNodes()
Description copied from class:Graph
Gets an immutable list of all nodes.
-
getNode
public LinkedDirectedGraph.LinkedDiGraphNode<N,E> getNode(N nodeValue)
Description copied from interface:AdjacencyGraph
Gets a node from the graph given a value. Values equality are compared usingObject.equals
.
-
getInEdges
public java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>> getInEdges(N nodeValue)
Description copied from class:DiGraph
Gets an immutable list of in edges of the given node.- Specified by:
getInEdges
in classDiGraph<N,E>
-
getOutEdges
public java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>> getOutEdges(N nodeValue)
Description copied from class:DiGraph
Gets an immutable list of out edges of the given node.- Specified by:
getOutEdges
in classDiGraph<N,E>
-
createNode
public LinkedDirectedGraph.LinkedDiGraphNode<N,E> createNode(N nodeValue)
Description copied from class:Graph
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
.- Specified by:
createNode
in classDiGraph<N,E>
- Parameters:
nodeValue
- The node's value.- Returns:
- The corresponding node in the graph.
-
getEdges
public java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>> getEdges(N n1, N n2)
Description copied from class:Graph
Retrieves an edge from the graph.
-
getEdges
public java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>> getEdges()
Description copied from class:Graph
Gets an immutable list of all edges.
-
getFirstEdge
public @Nullable Graph.GraphEdge<N,E> getFirstEdge(N n1, N n2)
Description copied from class:Graph
Retrieves any edge from the graph.- Specified by:
getFirstEdge
in classGraph<N,E>
- Parameters:
n1
- Node one.n2
- Node two.- Returns:
- The first edges between those two values in the graph. null if there are none.
-
getEdgesInDirection
public java.util.List<LinkedDirectedGraph.LinkedDiGraphEdge<N,E>> getEdgesInDirection(N n1, N n2)
- Specified by:
getEdgesInDirection
in classDiGraph<N,E>
-
isConnectedInDirection
public boolean isConnectedInDirection(N source, N dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer the version below that takes DiGraphNodes, if you have them available.- Specified by:
isConnectedInDirection
in classDiGraph<N,E>
- Parameters:
source
- the source node from which we traverse outwardsdest
- Node 2.- Returns:
true
if the graph contains edge from n1 to n2.
-
isConnectedInDirection
public boolean isConnectedInDirection(N source, E edgeValue, N dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer the version below that takes DiGraphNodes, if you have them available.- Specified by:
isConnectedInDirection
in classDiGraph<N,E>
- Parameters:
source
- the source node from which we traverse outwardsedgeValue
- only edges equal to the given value will be traverseddest
- Node 2.- Returns:
true
if the edge exists.
-
isConnectedInDirection
public boolean isConnectedInDirection(LinkedDirectedGraph.LinkedDiGraphNode<N,E> source, com.google.common.base.Predicate<E> edgeFilter, LinkedDirectedGraph.LinkedDiGraphNode<N,E> dest)
DiGraphNode look ups can be expensive for a large graph operation, prefer this method if you have the DiGraphNodes available.- Parameters:
source
- the source node from which we traverse outwardsedgeFilter
- only edges matching this filter will be traverseddest
- the destination node
-
getDirectedPredNodes
public java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>> getDirectedPredNodes(N nodeValue)
- Specified by:
getDirectedPredNodes
in classDiGraph<N,E>
-
getDirectedPredNodes
public java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>> getDirectedPredNodes(DiGraph.DiGraphNode<N,E> dNode)
- Specified by:
getDirectedPredNodes
in classDiGraph<N,E>
-
getDirectedSuccNodes
public java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>> getDirectedSuccNodes(N nodeValue)
- Specified by:
getDirectedSuccNodes
in classDiGraph<N,E>
-
getDirectedSuccNodes
public java.util.List<LinkedDirectedGraph.LinkedDiGraphNode<N,E>> getDirectedSuccNodes(DiGraph.DiGraphNode<N,E> dNode)
- Specified by:
getDirectedSuccNodes
in classDiGraph<N,E>
-
getGraphvizEdges
public java.util.List<GraphvizGraph.GraphvizEdge> getGraphvizEdges()
Description copied from interface:GraphvizGraph
Retrieve a list of edges in the graph.- Specified by:
getGraphvizEdges
in interfaceGraphvizGraph
- Returns:
- A list of edges in the graph.
-
getGraphvizNodes
public java.util.List<GraphvizGraph.GraphvizNode> getGraphvizNodes()
Description copied from interface:GraphvizGraph
Retrieve a list of nodes in the graph.- Specified by:
getGraphvizNodes
in interfaceGraphvizGraph
- Returns:
- A list of nodes in the graph.
-
getName
public java.lang.String getName()
Description copied from interface:GraphvizGraph
Name of the graph.- Specified by:
getName
in interfaceGraphvizGraph
- Returns:
- Name of the graph.
-
isDirected
public boolean isDirected()
Description copied from interface:GraphvizGraph
Graph type.- Specified by:
isDirected
in interfaceGraphvizGraph
- Returns:
- True if the graph is a directed graph.
-
getNodeCount
public int getNodeCount()
- Specified by:
getNodeCount
in interfaceAdjacencyGraph<N,E>
- Specified by:
getNodeCount
in classGraph<N,E>
-
getNeighborNodes
public java.util.List<GraphNode<N,E>> getNeighborNodes(N value)
Description copied from class:Graph
Gets the neighboring nodes.- Specified by:
getNeighborNodes
in classGraph<N,E>
- Parameters:
value
- The node's value.- Returns:
- A list of neighboring nodes.
-
-