|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjdsl.core.ref.AbstractPositionalContainer
jdsl.graph.ref.AbstractGraph
jdsl.graph.ref.IncidenceListGraph
An implementation of the Graph interface, including self-loops, parallel edges, and mixed directed and undirected edges. For specifications of the methods, see the documentation of the interfaces. The remainder of this description is about the implementation.
The following is a high-level description of the design; low-level details appear further below. The graph is implemented via a list of vertices and a list of edges. The nodes of these "global" lists are the vertices and edges of the graph, respectively. In addition to the global lists of vertices and edges, each vertex has a "local" list of its incident edges. Thus, each edge participates in two local lists (one for each endpoint) plus the global list. Each vertex participates in only the global list. Although sequential data structures are used to implement the graph, the graph conceptually consists of unordered sets; no guarantee is made about where in the various lists a given accessor appears.
This design forces most of the methods' time complexities. Insertion and deletion of vertices and edges are constant-time because the various doubly-linked lists can be modified in constant time. Adjacency queries are typically O(degrees of vertices involved). Iterations take time linear in the number of things iterated over.
The remainder of this description is about low-level details of the design, and efficiency tradeoffs of alternate designs.
The global list of edges exists to avoid having the duplicates in edges() that would result from asking all vertices for their incident edges (each edge would appear twice). For most other purposes, the only global list required would be the vertex list. An alternate design would get rid of the global edge list and would do a traversal to get edges(). This would save space at the cost of increasing the complexity of edges() from O(E) to O(V+E).
Both global lists are implemented with JDSL NodeSequences. In contrast, the local incidence lists at each vertex are implemented "by hand," with links in the ILEdges, for space efficiency. Internal methods that refer to those links also take a vertex, so the edge knows which set of links is being referred to (the set for one endpoint or the set for the other endpoint). Each local list includes a dummy edge to handle special cases (empty list, iterating till end of list, inserting at beginning of list).
To avoid the space overhead of having the global lists' positions point to vertex/edge objects, which would need to point back to the positions, the positions *are* the vertices/edges. This is accomplished by inheriting from the position implementation of the NodeSequence (called FNSNode) and using the posInsert* methods to put objects of the subclass into the global lists.
Research issues. The following are changes to the design that could be made if experiments indicated they were worthwhile:
Graph,
Vertex,
Edge,
EdgeDirection| Nested Class Summary |
| Nested classes inherited from class jdsl.graph.ref.AbstractGraph |
AbstractGraph.OO_to_O_MergerIterator |
| Field Summary |
| Fields inherited from interface jdsl.graph.api.EdgeDirection |
IN, OUT, UNDIR |
| Constructor Summary | |
IncidenceListGraph()
Creates a new IncidenceListGraph with default parameters. |
|
| Methods inherited from class jdsl.graph.ref.AbstractGraph |
aCommonVertex, aConnectingEdge, adjacentVertices, adjacentVertices, areAdjacent, areAdjacent, connectingEdges, directedEdges, positions, size, undirectedEdges |
| Methods inherited from class jdsl.core.ref.AbstractPositionalContainer |
isEmpty, swapElements |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface jdsl.graph.api.InspectableGraph |
aCommonVertex, aConnectingEdge, adjacentVertices, adjacentVertices, areAdjacent, areAdjacent, connectingEdges, directedEdges, undirectedEdges |
| Methods inherited from interface jdsl.core.api.InspectablePositionalContainer |
positions |
| Methods inherited from interface jdsl.core.api.InspectableContainer |
isEmpty, size |
| Methods inherited from interface jdsl.core.api.PositionalContainer |
swapElements |
| Constructor Detail |
public IncidenceListGraph()
O(1)
| Method Detail |
public Container newContainer()
newContainer in interface Container
public Object replaceElement(Accessor p,
Object newElement)
replaceElement in interface Containerp - Accessor in this containernewElement - to be stored at a
public boolean contains(Accessor p)
contains in interface InspectableContainerpublic ObjectIterator elements()
elements in interface InspectableContainerpublic int numVertices()
numVertices in interface InspectableGraphpublic int numEdges()
numEdges in interface InspectableGraphpublic VertexIterator vertices()
vertices in interface InspectableGraphpublic Vertex aVertex()
aVertex in interface InspectableGraphpublic EdgeIterator edges()
edges in interface InspectableGraphpublic Edge anEdge()
anEdge in interface InspectableGraph
public int degree(Vertex v)
throws InvalidAccessorException
degree in interface InspectableGraphv - a vertex
v
InvalidAccessorException - if v is
not contained in this graph
public int degree(Vertex v,
int edgetype)
throws InvalidAccessorException
degree in interface InspectableGraphv - a vertexedgetype - A constant from the EdgeDirection interface
v
InvalidAccessorException - if v is
not contained in this graphEdgeDirection
public EdgeIterator incidentEdges(Vertex v)
throws InvalidAccessorException
incidentEdges in interface InspectableGraphv - a vertex
v
InvalidAccessorException - if v is not
contained in this graph
public EdgeIterator incidentEdges(Vertex v,
int edgetype)
throws InvalidAccessorException
incidentEdges in interface InspectableGraphv - a vertexedgetype - A constant from the EdgeDirection interface
v
InvalidAccessorException - if v is not
contained in this graphEdgeDirection
public Edge anIncidentEdge(Vertex v)
throws InvalidAccessorException
anIncidentEdge in interface InspectableGraphv - a vertex
v, or Edge.NONE if
there is no edge incident on v
InvalidAccessorException - if v is not
contained in this graph
public Edge anIncidentEdge(Vertex v,
int edgetype)
throws InvalidAccessorException
anIncidentEdge in interface InspectableGraphv - a vertexedgetype - A constant from the EdgeDirection interface
v,
or Edge.NONE if there is no such edge incident on v
InvalidAccessorException - if v is not
contained in this graphEdgeDirectionpublic Vertex[] endVertices(Edge e)
endVertices in interface InspectableGraphe - an edge
e;
if e is directed, the first element of the array is the origin of e
and the second element is the destination of e
public boolean areIncident(Vertex v,
Edge e)
areIncident in interface InspectableGraphv - a vertexe - an edge
v and e are incident,
i.e., whether v is an endvertex of e
public Vertex opposite(Vertex v,
Edge e)
opposite in interface InspectableGraphv - one endvertex of ee - an edge
e different from vpublic Vertex origin(Edge e)
origin in interface InspectableGraphe - an edge
e, if e is directedpublic Vertex destination(Edge e)
destination in interface InspectableGraphe - an edge
e, if
e is directedpublic Vertex insertVertex(Object elt)
insertVertex in interface Graphelt - the object to be stored in the new vertex
public Object removeVertex(Vertex v)
throws InvalidAccessorException
removeVertex in interface Graphv - the vertex to be deleted
v
InvalidAccessorException - if the vertex does not
belong to this graph
public Edge attachVertex(Vertex v,
Object vertexInfo,
Object edgeInfo)
throws InvalidAccessorException
attachVertex in interface Graphv - a vertexvertexInfo - the object to be stored in vedgeInfo - the object to be stored in the new edge
opposite(v,e)
InvalidAccessorException - if vertex to be attached to
does not belong to this graph
public Edge attachVertexFrom(Vertex v,
Object vertexInfo,
Object edgeInfo)
throws InvalidAccessorException
attachVertexFrom in interface Graphv - a vertexvertexInfo - the object to be stored in vedgeInfo - the object to be stored in the new edge
e; to get the new vertex, use method
opposite(v,e)
InvalidAccessorException - if origin
does not belong to this graph
public Edge attachVertexTo(Vertex v,
Object vertexInfo,
Object edgeInfo)
throws InvalidAccessorException
attachVertexTo in interface Graphv - a vertexvertexInfo - the object to be stored in vedgeInfo - the object to be stored in the new edge
e; to get the new vertex, use method
opposite(v,e)
InvalidAccessorException - if destination
does not belong to this graph
public Edge insertEdge(Vertex v1,
Vertex v2,
Object elt)
throws InvalidAccessorException
insertEdge in interface Graphv1 - the first endvertexv2 - the second endvertexelt - the object to be stored in the new edge
InvalidAccessorException - if either v1 or
v2 does not belong to this graph
public Edge insertDirectedEdge(Vertex v1,
Vertex v2,
Object elt)
throws InvalidAccessorException
insertDirectedEdge in interface Graphv1 - the origin vertexv2 - the destination vertexelt - the object to be stored in the new edge
InvalidAccessorException - if either v1 or
v2 does not belong to this graph
public Object removeEdge(Edge e)
throws InvalidAccessorException
removeEdge in interface Graphe - the edge to be removed
e
InvalidAccessorException - if the edge does not belong
to this graph
public Vertex splitEdge(Edge e,
Object elt)
throws InvalidAccessorException
splitEdge in interface ModifiableGraphe - the edge to be splitelt - the object to be stored in v
w; to get the new edges, use method
incidentEdges(w)
InvalidAccessorException - if the edge does not belong
to this graph
public Edge unsplitEdge(Vertex v,
Object o)
throws InvalidAccessorException,
InvalidVertexException
O(1)
unsplitEdge in interface ModifiableGraphv - the vertex to be removedo - the element to be stored in the new edge
InvalidAccessorException - if the vertex does not
belong to this graph
InvalidVertexException - If the vertex is not of degree 2
or is of degree 2 but the "two" edges are a single self-looppublic boolean isDirected(Edge e)
isDirected in interface InspectableGraphe - an edge
true if e is directed,
false otherwise
public void setDirectionFrom(Edge e,
Vertex v)
setDirectionFrom in interface ModifiableGraphe - an edgev - an endvertex of e
public void setDirectionTo(Edge e,
Vertex v)
setDirectionTo in interface ModifiableGraphe - an edgev - an endvertex of epublic void makeUndirected(Edge e)
makeUndirected in interface ModifiableGraphe - an edgepublic void reverseDirection(Edge e)
reverseDirection in interface ModifiableGraphe - an edgepublic String toString()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||