T
- the type of object stored as nodes in this graphpublic final class MutableDirectedGraph<T> extends Object implements TraversableGraph<T>
Constructor and Description |
---|
MutableDirectedGraph()
Creates a new graph with no nodes or edges.
|
Modifier and Type | Method and Description |
---|---|
void |
addEdge(T source,
T sink)
Adds an edge between
source and sink . |
boolean |
addNode(T node)
Adds the specified node to the graph.
|
boolean |
containsEdge(T source,
T sink) |
boolean |
containsNode(T node) |
static <T> MutableDirectedGraph<T> |
createConcurrent() |
com.google.common.collect.ImmutableSet<com.google.common.collect.ImmutableSet<T>> |
findCycles() |
Set<Set<T>> |
findStronglyConnectedComponents()
For this graph, returns the set of strongly connected components using Tarjan's algorithm.
|
int |
getEdgeCount() |
Iterable<T> |
getIncomingNodesFor(T sink) |
int |
getNodeCount() |
Set<T> |
getNodes() |
Iterable<T> |
getNodesWithNoIncomingEdges() |
Iterable<T> |
getNodesWithNoOutgoingEdges() |
Iterable<T> |
getOutgoingNodesFor(T source) |
boolean |
hasIncomingEdges(T node) |
boolean |
isAcyclic() |
void |
removeEdge(T source,
T sink)
Removes the edge between
source and sink . |
boolean |
removeNode(T node)
Removes the specified node from the graph.
|
public MutableDirectedGraph()
public static <T> MutableDirectedGraph<T> createConcurrent()
public int getNodeCount()
public int getEdgeCount()
public boolean containsNode(T node)
public boolean containsEdge(T source, T sink)
public boolean addNode(T node)
public boolean removeNode(T node)
public void addEdge(T source, T sink)
source
and sink
. Adds the nodes to the graph if they are
not already present.public void removeEdge(T source, T sink)
source
and sink
. This does not remove source
or sink
from the graph. Note that this may leave either source
or sink
as unconnected nodes in the graph.public Iterable<T> getOutgoingNodesFor(T source)
getOutgoingNodesFor
in interface TraversableGraph<T>
Iterable
that the caller is not allowed to mutate.public Iterable<T> getIncomingNodesFor(T sink)
getIncomingNodesFor
in interface TraversableGraph<T>
Iterable
that the caller is not allowed to mutate.public boolean hasIncomingEdges(T node)
public Set<T> getNodes()
getNodes
in interface TraversableGraph<T>
public boolean isAcyclic()
public com.google.common.collect.ImmutableSet<com.google.common.collect.ImmutableSet<T>> findCycles()
public Set<Set<T>> findStronglyConnectedComponents()
O(|V| + |E|)
.public Iterable<T> getNodesWithNoIncomingEdges()
getNodesWithNoIncomingEdges
in interface TraversableGraph<T>
Iterable
that the caller is not allowed to mutate.public Iterable<T> getNodesWithNoOutgoingEdges()
getNodesWithNoOutgoingEdges
in interface TraversableGraph<T>
Iterable
that the caller is not allowed to mutate.