Class AbstractGraph<Node extends AbstractNode,Edge extends AbstractEdge>
- Type Parameters:
Node- represents a nodeEdge- represents an e dge
- All Implemented Interfaces:
Graphvizable,GraphvizDecorable,Iterable<Node>
- Direct Known Subclasses:
GraphAM
Methods throw an IllegalArgumentException if a given
node or edge is invalid, i.e., it equals null, or it is
not part of this graph.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassDefines iterator over all edges of an @{link AbstractGraph}. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract EdgeCreate and add new edge fromsourcetodestination.abstract NodeaddNode()create and add new nodeprotected Edgehelper: check if edge is validprotected Nodehelper: check if node is validedges()Get Edges instance to obtain iterator.ensureEdge(Node source, Node destination) Same asgetEdge(Node, Node)but throw if there is no such edge.get decoration ornullintGet total degree.abstract EdgeGet edge fromsourcetodestination.Get iterator over all edges.intgetInDegree(Node node) get number of edges incident to nodegetInEdges(Node node) Get incident edges of node.abstract intget number of nodesintgetOutDegree(Node node) get number of edges emanating from nodegetOutEdges(Node node) Get incident edges of node.abstract NodeGet some node.abstract booleanIs graph directed?abstract voidremoveEdge(Edge edge) Remove edge.abstract voidremoveNode(Node node) Remove node and all its incident and excident edges.toDot()Get dot representation.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Constructor Details
-
AbstractGraph
Constructor.The constructor takes two objects that serve as "templates" for creating new Node (
AbstractNode.create()) and Edge (AbstractEdge.create()) instances.Note: The instances are required due to the way how Java generics work (see type erasure). This is different (indeed much simpler but weaker) than, e.g., C++ templates.
- Parameters:
nodeGenerator- "template" for creating Node instancesedgeGenerator- "template" for creating Edge instances
-
-
Method Details
-
isDirected
public abstract boolean isDirected()Is graph directed? -
addNode
create and add new node- Returns:
- node instance
-
addEdge
Create and add new edge fromsourcetodestination.- For undirected graphs,
sourceanddestinationmay be swapped such that alwayssource.index()<=destination.index(). -
RuntimeExceptionis thrown if the edge already exists.
- Parameters:
source- source nodedestination- destination node- Returns:
- edge instance
- Throws:
IllegalArgumentExceptionRuntimeException- See Also:
- For undirected graphs,
-
removeNode
Remove node and all its incident and excident edges.- Throws:
IllegalArgumentException
-
removeEdge
Remove edge.- Throws:
IllegalArgumentException
-
getNumNodes
public abstract int getNumNodes()get number of nodes -
getSomeNode
Get some node.This may be any node of the graph.
- Returns:
- node
- Throws:
NoSuchElementException- (for empty graph)
-
getEdge
Get edge fromsourcetodestination.Order of arguments is not relevant for undirected graphs.
- Returns:
- edge or
nullif there is no such edge - Throws:
IllegalArgumentException
-
ensureEdge
Same asgetEdge(Node, Node)but throw if there is no such edge.- Returns:
- edge
- Throws:
RuntimeException- if there is no such edgeIllegalArgumentException
-
getInEdges
Get incident edges of node.For undirected graphs same as
getOutEdges(Node).- Returns:
- vector of edges
- Throws:
IllegalArgumentException
-
getOutEdges
Get incident edges of node. For undirected graphs same asgetInEdges(Node).- Returns:
- vector of edges
- Throws:
IllegalArgumentException
-
getInDegree
get number of edges incident to node -
getOutDegree
get number of edges emanating from node -
getDegree
Get total degree.- sum of
getInEdges(Node)andgetOutEdges(Node)for directed graphsisDirected() -
getInEdges(Node)(equalsgetOutEdges(Node)} for undirected graphs
- sum of
-
getEdgeIterator
Get iterator over all edges.Edges are traversed in arbitrary order.
-
check
helper: check if node is valid- Returns:
- node
- Throws:
IllegalArgumentException
-
check
helper: check if edge is valid- Returns:
- node
- Throws:
IllegalArgumentException
-
edges
Get Edges instance to obtain iterator. Provided for convenience.Use as
for (edge : graph.edges()) { ... } -
toString
-
getDecorator
Description copied from interface:GraphvizDecorableget decoration ornull- Specified by:
getDecoratorin interfaceGraphvizDecorable
-
toDot
Description copied from interface:GraphvizableGet dot representation.- Specified by:
toDotin interfaceGraphvizable
-