|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbcds.phison.SdiGraph<V,E>
public class SdiGraph<V,E>
Implements a directed graph in which parallel edges are not allowed (though loops are). The initial S stands for "simple" precisely because parallel edges are not supported.
Both the vertices and the edges may be of any class, provided the following conditions are met:
newEdge(String)
.
The derived edge class contains two references to the edge ends (source and destination vertices) and overrides the toString method so that it returns a string of the form a-b, as in 23-79, where the two integers are the values returned by the toString method of class V.
SdiGraph does not store any vertex or edge attribute within the graph itself. Usually, the caller will maintain such attributes as he/she sees fit, in maps for example. A minimalistic and ficticious example of use of this class is:
class Test { public static void main(String...args) { SdiGraph<String, Link> g = new SdiGraph<String, Link>(String.class, Link.class); String v0 = "0"; String v1 = "1"; String v2; g.addVertex(v0); g.addVertex(v1); g.addVertex(v2=g.newVertex("2")); Map<Link, Integer> cost = new Map<Link, Integer>(); cost.put(g.addEdge(v0, v1), 123); cost.put(g.addEdge(v1, v2), 200); cost.put(g.addEdge(v2, v0), 42); for (String v : g.vertexSet()) { int v_weight = 0; for (Link e : g.edgesOf(v)) { v_weight += cost.get(e); } System.out.printf("Weight of vertex %s=%d\n", v, v_weight); } } public static class Link {} }Note how in this example the class Link is empty.
On referencing vertices and edges.
When searching for vertices and edges, SdiGraph uses
object identity exclusively to determine equality. Thus, in the
previous example we cannot write g.addEdge("0", "1") to
add a new edge. Although here the vertex class is String, and
it might be tempting to use "0" and v0
interchangeably. However, SdiGraph uses internally
IdentityHashMap
, both for vertices and edges.
Therefore, as the object references for "0" and v0 are
different, one cannot be used in place of the other.
On graph input/output. If a graph is never to be read from
or stored in a file, a vertex class such as String might
suffice. However, if file input/output is required, the classes
in the bcds.phison.io package expect a vertex class that
implements GraphIONode
.
Constructor Summary | |
---|---|
SdiGraph(java.lang.Class<V> vertex_class,
java.lang.Class<E> edge_class)
Creates a new instance in which vertices and edges are of the given types. |
Method Summary | |
---|---|
E |
addEdge(V src,
V dest)
Creates a new edge between the given source and destination if one does not exist yet, and returns the new edge instance. |
boolean |
addEdge(V src,
V dest,
E e)
Registers an existing edge instance as connecting two vertices, which are not connected before the call. |
boolean |
addVertex(V v)
Add a vertex to this graph. |
E |
canonicEdge(E e)
Returns the canonicalized form of an edge. |
java.util.Set<E> |
canonicEdges()
Returns the edges in this graph, in canonical form. |
SdiGraph<V,E> |
createSubgraph(java.util.Set<V> vertex_subset,
java.util.Set<E> edge_subset)
Creates a new graph in which the vertices and edges are shared with the calling instance. |
java.util.Set<E> |
edgeSet()
Returns an unmodifiable view of the set of vertices in this graph. |
java.util.Set<E> |
edgesOf(V v)
Returns a the set of edges, both incoming and outgoing, of the vertex v . |
E |
getEdge(V src,
V dest)
Returns the edge object that links vertices src and
dest , if one exists, or null otherwise. |
java.lang.Class<E> |
getEdgeClass()
Returns the class which serves as the parent of all edges created by newEdge(String) . |
V |
getEdgeSource(E e)
Returns the vertex for which e is an outgoing edge. |
V |
getEdgeTarget(E e)
Returns the vertex for which e is an incoming edge. |
java.lang.Class<V> |
getVertexClass()
Returns the class used to instantiate vertices in newVertex(String) . |
java.util.Set<E> |
incomingEdgesOf(V v)
Returns an unmodifiable view of the set of incoming edges of the vertex v . |
int |
inDegreeOf(V v)
Returns the number of incoming edges of vertex v . |
E |
newEdge(java.lang.String id)
Creates a new edge, without adding it to the graph. |
V |
newVertex(java.lang.String id)
Creates a new vertex, without adding it to the graph. |
int |
outDegreeOf(V v)
Returns the number of outgoing edges of vertex v . |
java.util.Set<E> |
outgoingEdgesOf(V v)
Returns an unmodifiable view of the set of outgoing edges of the vertex v . |
void |
removeAllEdges(java.util.Collection<E> c)
Removes all the edges in the collection c by repeatedly
calling removeEdge(E) . |
void |
removeAllVertices(java.util.Collection<V> c)
Removes all the edges in the collection c by repeatedly
calling removeVertex(V) . |
boolean |
removeEdge(E e)
Detaches the given edge from this graph. |
boolean |
removeVertex(V v)
Removes the given vertex and all its associated edges (both incoming and outgoing). |
E |
reverseEdge(E e)
Returns the edge that runs in the reverse direction to e , or null if such an edge does not exist. |
java.lang.String |
toString()
Returns a simple representation of this graph. |
java.util.Set<V> |
vertexSet()
Returns an unmodifiable view of the set of vertices in this graph. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public SdiGraph(java.lang.Class<V> vertex_class, java.lang.Class<E> edge_class)
java.lang.IllegalArgumentException
- if vertex_class does not
have a public constructor that accepts a String, or if
edge_classdoes not have a public default constructor.Method Detail |
---|
public boolean addVertex(V v)
newVertex(java.lang.String)
, but that is not a
requirement.
addVertex
in interface SimGraph<V,E>
public E addEdge(V src, V dest)
addEdge
in interface SimGraph<V,E>
java.lang.IllegalArgumentException
- if src or dest
are not in this graph.
java.lang.RuntimeException
- if edge instantiation fails in any way.public boolean addEdge(V src, V dest, E e)
e
is null, a new edge object is instantiated.
If edge e
it not null, it must
have been instantiated by calling newEdge(java.lang.String)
on this
graph or one of its subgraphs.
Internal information kept in private members on the edge object are updated (the source and destination vertices).
This method may also throw the exceptions listed in
#addEdge(V, V)
.
addEdge
in interface SimGraph<V,E>
java.lang.IllegalArgumentException
- if e is not null but
has not been created by this graph's newEdge.
java.lang.IllegalArgumentException
- if e is already in use
as linking another pair of vertices.public boolean removeEdge(E e)
e
is not in this graph, and true if the
removal is successful. The edge object is left unchanged, in particular,
its internal pointers to its vertices remain intact after this call.
removeEdge
in interface SimGraph<V,E>
public boolean removeVertex(V v)
removeVertex
in interface SimGraph<V,E>
public E getEdge(V src, V dest)
src
and
dest
, if one exists, or null otherwise.
null is returned if either or both vertices do not
belong to this graph,
getEdge
in interface SimGraph<V,E>
public V getEdgeSource(E e)
e
is an outgoing edge.
Note that if e
is not a valid edge for this graph,
the result is undefined. For example, if g and
h are two unrelated graphs,
g.getEdgeSource(h.getEdge(vx, vy)) might succeed but the
returned vertex is not valid for the graph g.
getEdgeSource
in interface SimGraph<V,E>
java.lang.RuntimeException
- if reading the source vertex of the
given edge fails, which usually happens if e
has not been instantiated by calling newEdge(java.lang.String)
.public V getEdgeTarget(E e)
e
is an incoming edge.
The comments for getEdgeSource(E)
applies to this
method as well.
See getEdgeSource(E)
for the list of exceptions thrown.
getEdgeTarget
in interface SimGraph<V,E>
public E reverseEdge(E e)
e
, or null if such an edge does not exist.
This method is implemented as
getEdge(getEdgeTarget(e), getEdgeSource(e)).
See the called methods for the list of exceptions thrown.
reverseEdge
in interface SimGraph<V,E>
public java.util.Set<V> vertexSet()
vertexSet
in interface SimGraph<V,E>
public java.util.Set<E> edgeSet()
edgeSet
in interface SimGraph<V,E>
public java.util.Set<E> outgoingEdgesOf(V v)
v
.
outgoingEdgesOf
in interface SimGraph<V,E>
java.lang.NullPointerException
- if vertex v does not
exist in the graph, or is null.public java.util.Set<E> incomingEdgesOf(V v)
v
.
incomingEdgesOf
in interface SimGraph<V,E>
java.lang.NullPointerException
- if vertex v does not
exist in the graph, or if null.public void removeAllEdges(java.util.Collection<E> c)
c
by repeatedly
calling removeEdge(E)
.
removeAllEdges
in interface SimGraph<V,E>
public void removeAllVertices(java.util.Collection<V> c)
c
by repeatedly
calling removeVertex(V)
.
removeAllVertices
in interface SimGraph<V,E>
public java.lang.Class<V> getVertexClass()
newVertex(String)
. This is the class received in
the constructor.
getVertexClass
in interface SimGraph<V,E>
public java.lang.Class<E> getEdgeClass()
newEdge(String)
. This is the class received
in the constructor.
getEdgeClass
in interface SimGraph<V,E>
public java.util.Set<E> edgesOf(V v)
v
. Beware that each call generates a new
set instance.
edgesOf
in interface SimGraph<V,E>
java.lang.NullPointerException
- if vertex v does not
exist in the graph, or is null.public int outDegreeOf(V v)
v
.
outDegreeOf
in interface SimGraph<V,E>
java.lang.NullPointerException
- if vertex v does not
exist in the graph, or is null.public int inDegreeOf(V v)
v
.
inDegreeOf
in interface SimGraph<V,E>
java.lang.NullPointerException
- if vertex v does not
exist in the graph, or is null.public SdiGraph<V,E> createSubgraph(java.util.Set<V> vertex_subset, java.util.Set<E> edge_subset)
Note that the subgraph and its parent share types (edge and vertex class and constructors) but changes in one (addition of vertices, reassignment of edges, etc) do not affect the other.
createSubgraph
in interface SimGraph<V,E>
vertex_subset
- The set of vertices to include in the subgraph.
If null, all vertices are included.edge_subset
- The set of edges to include in the subgraph.
If null, all edges are included. For an edge to be
effectively included in the subgraph, both its end vertices
must also be there.public V newVertex(java.lang.String id)
id
is passed to the vertex constructor
as its sole parameter.
newVertex
in interface SimGraph<V,E>
java.lang.RuntimeException
- if instantiation fails in any way.public E newEdge(java.lang.String id)
id
is ignored, that is, it is not passed
to the edge constructor.
newEdge
in interface SimGraph<V,E>
java.lang.RuntimeException
- if instantiation fails in any way.public E canonicEdge(E e)
For example, given the edge 10-3, this implementation will return the edge 3-10, that is, the source will be the numerically smaller of the end points (as integers). If integer conversion cannot be applied, the comparison is performed as strings.
canonicEdge
in interface SimGraph<V,E>
public java.util.Set<E> canonicEdges()
canonicEdges
in interface SimGraph<V,E>
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |