WebDrake / Dgraph

Graph library written in D.
GNU General Public License v3.0
45 stars 6 forks source link

Vertex/edge labels #3

Open gsmnv opened 11 years ago

gsmnv commented 11 years ago

Is it possible to label edges/vertices with arbitary data type? It'll be great to be able do so. E.g i want to represent path with graph and that graph should have vertices labeled with string type(like street name or whatever) and edges with int type(travel time). Ultimately it should be possible to instantinate these two types of labels(for edges and for vertices) with arbitrary type.

Imaginary code follows for better understandin what i mean.


// Vertex label type.
class Foo { ... };
// Edge label type.
enum Bar { ... };

auto graph = new Graph!(Foo, Bar)(...);
WebDrake commented 11 years ago

Not yet -- it will be implemented at some point but the core functionality is priority for now. The idea of Dgraph is that for any actual graph calculations, the vertices/edges should be labelled 0, 1, 2, ... but that there should be wrappers that can be used for data input/output. You can implement this manually doing something like:

size_t[LabelType] idMap;
LabelType[] idMapInverse;

... and then to add an edge (i, j) to the graph you could do something like:

if ((i in idMap) is null)
{
    idMap[i] = idMapInverse.length;
    idMapInverse ~= i;
    assert(idMap.length == idMapInverse.length);
}

if ((j in idMap) is null)
{
    idMap[j] = idMapInverse.length;
    idMapInverse ~= j;
    assert(idMap.length == idMapInverse.length);
}

if (idMap.length > graph.vertexCount)
{
    // ... add appropriate number of vertices,
    // I really must implement vertexCount as a
    // writeable property ...
}

graph.addEdge(idMap[i], idMap[j]);

You can now do any and all calculations you need on the graph, and then filter vertex IDs back through idMapInverse when you need to write out any results.

gsmnv commented 11 years ago

Okay, thanks for an explanation.

WebDrake commented 11 years ago

Leave the issue open. It's a perfectly valid feature request :-)