-
Notifications
You must be signed in to change notification settings - Fork 3
Vertex Edge Proposals
We currently allow direct modification of both Edge endpoint vertices and Vertex edgelists. This is an opportunity for graphs to become incoherent, as references are stored in both directions (Vertex <--> Edge). I propose that the constructors for Vertex and Edge take references to their enclosing entity(ies) as parameters, and add themselves automatically to the enclosing entity upon creation. If an edge is later re-attached to a different vertex, the edgelist for that vertex is automatically updated. Detaching an Edge at both ends will update the corresponding edge lists and leave it free-floating for garbage collection. There would be no other way to modify references in the enclosing entity than by going through the public methods of the enclosed entity (and perhaps doing so should be avoided after creation). Edgelists are then treated as an entirely internal detail.
Likewise, a Vertex would belong to a specific Graph at instantiation. Since the edgelists are now in the Vertex itself, each Vertex has a privileged relationship with a particular "main graph" (as opposed to overlay graph) and its internal edges are considered to be "in" that main graph. This is the graph that is passed to the Vertex constructor.
All the logic to do this can be in the Vertex and Edge abstract base classes (and Graph) which can be placed in a package apart from the concrete classes.
As a result, the (rather messy) idiom:
Graph graph = new Graph();
Vertex v1 = new SomeVertex(label, x, y, name);
v1 = graph.add(v1);
Vertex v2 = new SomeVertex(label, x, y, name);
v2 = graph.add(v2);
Edge e = new SomeEdge(v1, v2);
graph.addEdge(v1, v2);
becomes:
Graph graph = new Graph();
Vertex v1 = new SomeVertex(graph, x, y, name);
Vertex v2 = new SomeVertex(graph, x, y, name);
Edge e = new SomeEdge(v1, v2);
This is already implemented in the branch and seems to work fine.