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 TypeClassDescriptionclass
Defines iterator over all edges of an @{link AbstractGraph}. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract Edge
Create and add new edge fromsource
todestination
.abstract Node
addNode()
create and add new nodeprotected Edge
helper: check if edge is validprotected Node
helper: 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 ornull
int
Get total degree.abstract Edge
Get edge fromsource
todestination
.Get iterator over all edges.int
getInDegree
(Node node) get number of edges incident to nodegetInEdges
(Node node) Get incident edges of node.abstract int
get number of nodesint
getOutDegree
(Node node) get number of edges emanating from nodegetOutEdges
(Node node) Get incident edges of node.abstract Node
Get some node.abstract boolean
Is graph directed?abstract void
removeEdge
(Edge edge) Remove edge.abstract void
removeNode
(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, wait
Methods 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 fromsource
todestination
.- For undirected graphs,
source
anddestination
may be swapped such that alwayssource.index()<=destination.index()
. -
RuntimeException
is thrown if the edge already exists.
- Parameters:
source
- source nodedestination
- destination node- Returns:
- edge instance
- Throws:
IllegalArgumentException
RuntimeException
- 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 fromsource
todestination
.Order of arguments is not relevant for undirected graphs.
- Returns:
- edge or
null
if 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:GraphvizDecorable
get decoration ornull
- Specified by:
getDecorator
in interfaceGraphvizDecorable
-
toDot
Description copied from interface:Graphvizable
Get dot representation.- Specified by:
toDot
in interfaceGraphvizable
-