AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
AbstractGraph.java
Go to the documentation of this file.
1package aud.graph;
2
3import aud.Vector;
7
8import java.util.Iterator;
9import java.util.NoSuchElementException;
10
22public abstract class
24 implements Iterable<Node>,
26
27 Node nodeGenerator_ = null;
28 Edge edgeGenerator_ = null;
29
46 public AbstractGraph(Node nodeGenerator,Edge edgeGenerator) {
47 nodeGenerator_=nodeGenerator;
48 edgeGenerator_=edgeGenerator;
49 }
50
52 public abstract boolean isDirected();
53
57 public abstract Node addNode();
58
79 public abstract Edge addEdge(Node source,Node destination);
80
85 public abstract void removeNode(Node node);
89 public abstract void removeEdge(Edge edge);
90
92 public abstract int getNumNodes();
93
99 public abstract Node getSomeNode();
100
108 public abstract Edge getEdge(Node source,Node destination);
109
115 public Edge ensureEdge(Node source,Node destination) {
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 }
123
130 public abstract Vector<Edge> getInEdges(Node node);
136 public abstract Vector<Edge> getOutEdges(Node node);
137
139 public int getInDegree(Node node) { return getInEdges(node).size(); }
140
142 public int getOutDegree(Node node) { return getOutEdges(node).size(); }
151 public int getDegree(Node node) {
152 if (isDirected())
153 return getInDegree(node)+getOutDegree(node);
154 else
155 return getInDegree(node);
156 }
157
161 public abstract Iterator<Edge> getEdgeIterator();
162
167 protected Node check(Node node) {
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 }
179 protected Edge check(Edge edge) {
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 }
187
191 public class Edges implements Iterable<Edge> {
193 Edges(AbstractGraph<Node,Edge> graph) { graph_=graph; }
194 @Override public Iterator<Edge> iterator() {
195 return (Iterator<Edge>) graph_.getEdgeIterator();
196 }
197 }
198
203 public Edges edges() { return new Edges(this); }
204
205 @Override public String toString() {
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 }
214
215 @Override public GraphvizDecorator getDecorator() {
216 return null;
217 }
218
219 @Override public String toDot() {
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 }
263}
Implementation of an array-based vector.
Definition: Vector.java:44
Defines iterator over all edges of anlink AbstractGraph}.
Interface to a graph.
GraphvizDecorator getDecorator()
get decoration or null
int getOutDegree(Node node)
get number of edges emanating from node
int getDegree(Node node)
Get total degree.
abstract Iterator< Edge > getEdgeIterator()
Get iterator over all edges.
abstract Edge addEdge(Node source, Node destination)
Create and add new edge from source to destination.
abstract int getNumNodes()
get number of nodes
abstract boolean isDirected()
Is graph directed?
Edge check(Edge edge)
helper: check if edge is valid
AbstractGraph(Node nodeGenerator, Edge edgeGenerator)
Constructor.
abstract Vector< Edge > getInEdges(Node node)
Get incident edges of node.
int getInDegree(Node node)
get number of edges incident to node
abstract Node addNode()
create and add new node
String toDot()
Get dot representation.
abstract void removeNode(Node node)
Remove node and all its incident and excident edges.
Edge ensureEdge(Node source, Node destination)
Same as getEdge but throw if there is no such edge.
abstract Vector< Edge > getOutEdges(Node node)
Get incident edges of node.
abstract Node getSomeNode()
Get some node.
abstract void removeEdge(Edge edge)
Remove edge.
abstract Edge getEdge(Node source, Node destination)
Get edge from source to destination.
Node check(Node node)
helper: check if node is valid
Edges edges()
Get Edges instance to obtain iterator.
Decorator for items of Graphvizable objects.
String getFullNodeDecoration(GraphvizDecorable object)
concatenates getAllNodesDecoration and getNodeDecoration
String getFullEdgeDecoration(GraphvizDecorable object)
concatenates getAllEdgesDecoration and getEdgeDecoration
String getGlobalStyle()
get global style (returns same for all nodes/edges)
String getGraphDecoration(GraphvizDecorable object)
get graph decoration (returns same for all nodes/edges)
Interface for decorating items of Graphvizable objects.
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1