AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge > Class Template Referenceabstract

Interface to a graph. More...

+ Inheritance diagram for aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >:
+ Collaboration diagram for aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >:

Classes

class  Edges
 Defines iterator over all edges of anlink AbstractGraph}. More...
 

Public Member Functions

 AbstractGraph (Node nodeGenerator, Edge edgeGenerator)
 Constructor. More...
 
abstract boolean isDirected ()
 Is graph directed? More...
 
abstract Node addNode ()
 create and add new node More...
 
abstract Edge addEdge (Node source, Node destination)
 Create and add new edge from source to
destination
. More...
 
abstract void removeNode (Node node)
 Remove node and all its incident and excident edges. More...
 
abstract void removeEdge (Edge edge)
 Remove edge. More...
 
abstract int getNumNodes ()
 get number of nodes More...
 
abstract Node getSomeNode ()
 Get some node. More...
 
abstract Edge getEdge (Node source, Node destination)
 Get edge from source to destination. More...
 
Edge ensureEdge (Node source, Node destination)
 Same as getEdge but throw if there is no such edge. More...
 
abstract Vector< Edge > getInEdges (Node node)
 Get incident edges of node. More...
 
abstract Vector< Edge > getOutEdges (Node node)
 Get incident edges of node. More...
 
int getInDegree (Node node)
 get number of edges incident to node More...
 
int getOutDegree (Node node)
 get number of edges emanating from node More...
 
int getDegree (Node node)
 Get total degree. More...
 
abstract Iterator< Edge > getEdgeIterator ()
 Get iterator over all edges. More...
 
Edges edges ()
 Get Edges instance to obtain iterator. More...
 
String toString ()
 
GraphvizDecorator getDecorator ()
 get decoration or null
More...
 
String toDot ()
 Get dot representation. More...
 
String toDot ()
 Get dot representation. More...
 
GraphvizDecorator getDecorator ()
 get decoration or null
More...
 

Protected Member Functions

Node check (Node node)
 helper: check if node is valid More...
 
Edge check (Edge edge)
 helper: check if edge is valid More...
 

Detailed Description

Interface to a graph.

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.

Parameters
<Node>represents a node
<Edge>represents an e dge

Definition at line 22 of file AbstractGraph.java.

Constructor & Destructor Documentation

◆ AbstractGraph()

aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.AbstractGraph ( Node  nodeGenerator,
Edge  edgeGenerator 
)

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 instances
edgeGenerator"template" for creating Edge instances

Definition at line 46 of file AbstractGraph.java.

46 {
47 nodeGenerator_=nodeGenerator;
48 edgeGenerator_=edgeGenerator;
49 }

Member Function Documentation

◆ addEdge()

abstract Edge aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.addEdge ( Node  source,
Node  destination 
)
abstract

Create and add new edge from source to
destination
.

  • For undirected graphs, source and destination may be swapped such that always source.index()<=destination.index().

  • RuntimeException is thrown if the edge already exists.

Parameters
sourcesource node
destinationdestination node
Returns
edge instance
Exceptions
IllegalArgumentException
RuntimeException
See also
AbstractNode::index

Referenced by aud.util.GraphDemo.main(), aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ addNode()

abstract Node aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.addNode ( )
abstract

create and add new node

Returns
node instance

Referenced by aud.util.GraphDemo.main(), aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ check() [1/2]

Edge aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.check ( Edge  edge)
protected

helper: check if edge is valid

Returns
node
Exceptions
IllegalArgumentException

Definition at line 179 of file AbstractGraph.java.

179 {
180 if (edge==null)
181 throw new IllegalArgumentException("null edge");
182 if (edge.graph_!=this)
183 throw new IllegalArgumentException("edge is bound to other graph");
184
185 return edge;
186 }

◆ check() [2/2]

Node aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.check ( Node  node)
protected

helper: check if node is valid

Returns
node
Exceptions
IllegalArgumentException

Definition at line 167 of file AbstractGraph.java.

167 {
168 if (node==null)
169 throw new IllegalArgumentException("null node");
170 if (node.graph_!=this)
171 throw new IllegalArgumentException("node is bound to other graph");
172
173 return node;
174 }

◆ edges()

Edges aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.edges ( )

Get Edges instance to obtain iterator.

Provided for convenience.

Use as for (edge : graph.edges()) { ... }

Definition at line 203 of file AbstractGraph.java.

203{ return new Edges(this); }

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ ensureEdge()

Edge aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.ensureEdge ( Node  source,
Node  destination 
)

Same as getEdge but throw if there is no such edge.

Exceptions
RuntimeExceptionif there is no such edge
IllegalArgumentException
Returns
edge

Definition at line 115 of file AbstractGraph.java.

115 {
116 Edge edge=getEdge(source,destination);
117 if (edge==null)
118 throw new RuntimeException("no such edge "+source.index()+
119 (isDirected() ? "->" : "--")+
120 destination.index());
121 return edge;
122 }
abstract boolean isDirected()
Is graph directed?
abstract Edge getEdge(Node source, Node destination)
Get edge from source to destination.

◆ getDecorator()

GraphvizDecorator aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getDecorator ( )

get decoration or null

Implements aud.util.GraphvizDecorable.

Definition at line 215 of file AbstractGraph.java.

215 {
216 return null;
217 }

Referenced by aud.util.GraphDemo.main().

+ Here is the caller graph for this function:

◆ getDegree()

int aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getDegree ( Node  node)

Get total degree.

Definition at line 151 of file AbstractGraph.java.

151 {
152 if (isDirected())
153 return getInDegree(node)+getOutDegree(node);
154 else
155 return getInDegree(node);
156 }
int getOutDegree(Node node)
get number of edges emanating from node
int getInDegree(Node node)
get number of edges incident to node

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ getEdge()

abstract Edge aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getEdge ( Node  source,
Node  destination 
)
abstract

Get edge from source to destination.

Order of arguments is not relevant for undirected graphs.

Exceptions
IllegalArgumentException
Returns
edge or null if there is no such edge

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ getEdgeIterator()

abstract Iterator< Edge > aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getEdgeIterator ( )
abstract

Get iterator over all edges.

Edges are traversed in arbitrary order.

Referenced by aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.Edges.iterator().

+ Here is the caller graph for this function:

◆ getInDegree()

int aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getInDegree ( Node  node)

get number of edges incident to node

Definition at line 139 of file AbstractGraph.java.

139{ return getInEdges(node).size(); }
abstract Vector< Edge > getInEdges(Node node)
Get incident edges of node.

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ getInEdges()

abstract Vector< Edge > aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getInEdges ( Node  node)
abstract

Get incident edges of node.

For undirected graphs same as getOutEdges.

Exceptions
IllegalArgumentException
Returns
vector of edges

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ getNumNodes()

abstract int aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getNumNodes ( )
abstract

get number of nodes

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ getOutDegree()

int aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getOutDegree ( Node  node)

get number of edges emanating from node

Definition at line 142 of file AbstractGraph.java.

142{ return getOutEdges(node).size(); }
abstract Vector< Edge > getOutEdges(Node node)
Get incident edges of node.

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ getOutEdges()

abstract Vector< Edge > aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getOutEdges ( Node  node)
abstract

Get incident edges of node.

For undirected graphs same as getInEdges.

Exceptions
IllegalArgumentException
Returns
vector of edges

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ getSomeNode()

abstract Node aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.getSomeNode ( )
abstract

Get some node.

This may be any node of the graph.

Returns
node
Exceptions
NoSuchElementException(for empty graph)

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ isDirected()

abstract boolean aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.isDirected ( )
abstract

Is graph directed?

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ removeEdge()

abstract void aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.removeEdge ( Edge  edge)
abstract

Remove edge.

Exceptions
IllegalArgumentException

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ removeNode()

abstract void aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.removeNode ( Node  node)
abstract

Remove node and all its incident and excident edges.

Exceptions
IllegalArgumentException

Referenced by aud.test.GraphTest.testDirectedGraph(), and aud.test.GraphTest.testUndirectedGraph().

+ Here is the caller graph for this function:

◆ toDot()

String aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.toDot ( )

Get dot representation.

Implements aud.util.Graphvizable.

Definition at line 219 of file AbstractGraph.java.

219 {
220 String edgeOp=(isDirected() ? " -> " : " -- ");
221 String dot=(isDirected() ? "digraph " : "graph ")+
222 "AbstractGraph {\n";
223
224 GraphvizDecorator decorator=getDecorator();
225 if (decorator!=null) {
226 String d=decorator.getGlobalStyle();
227 if (d!=null) dot+=" "+d+";\n";
228 d=decorator.getGraphDecoration(this);
229 if (d!=null) dot+=" graph ["+d+"];\n";
230 }
231
232 for (Node node : this) {
233 decorator=node.getDecorator();
234 dot+=" \""+node.index()+"\" [label=\""+node.getLabel()+"\",";
235 dot+=(decorator!=null) ? decorator.getFullNodeDecoration(node) : "shape=circle";
236
237 double[] p=node.getPosition();
238 if (p!=null) {
239 dot+=",pos=\""+p[0]+","+p[1]+"\",pin=true,";
240 }
241
242 dot+="];\n";
243 }
244
245 dot+="\n\n";
246
247 for (Edge edge : this.edges()) {
248 decorator=edge.getDecorator();
249 dot+=" \""+edge.source().index()+"\""+edgeOp+
250 "\""+edge.destination().index()+"\" ";
251 String label=edge.getLabel();
252 dot+=(label!=null) ? "[label=\""+label+"\"," : "[";
253 dot+=edge.hasWeight() ? "weight="+edge.getWeight()+"," : "";
254 dot+=edge.hasWeight() ? "len="+edge.getWeight()+"," : "";
255 dot+=(decorator!=null) ? decorator.getFullEdgeDecoration(edge) : "";
256 dot+="];\n";
257 }
258
259 dot+="\n}\n";
260 //System.out.println(dot);
261 return dot;
262 }
GraphvizDecorator getDecorator()
get decoration or null
Edges edges()
Get Edges instance to obtain iterator.

References aud.util.GraphvizDecorator.getFullEdgeDecoration(), aud.util.GraphvizDecorator.getFullNodeDecoration(), aud.util.GraphvizDecorator.getGlobalStyle(), and aud.util.GraphvizDecorator.getGraphDecoration().

Referenced by aud.example.graph.GraphParser.main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toString()

String aud.graph.AbstractGraph< Node extends AbstractNode, Edge extends AbstractEdge >.toString ( )

Definition at line 205 of file AbstractGraph.java.

205 {
206 String rv="nodes:\n";
207 for (Node node : this)
208 rv+=" "+node.toString()+"\n";
209 rv+="edges:\n";
210 for (Edge edge : this.edges())
211 rv+=" "+edge.toString()+"\n";
212 return rv;
213 }

The documentation for this class was generated from the following file: