Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding cyclic data structures (eg directed graphs) using protocol buffers

I have a graph data structure that I'd like to encode with protocol buffers. There are cyclic connections between the graph vertices. Is there a standard/common way to encode such structures in protobuf? One approach that comes to mind is to add an "id" field to each vertex, and use those ids instead of pointers. E.g.:

message Vertex {
  required int32 id = 1;
  required string label = 2;
  repeated int32 outgoing_edges = 3; // values should be id's of other nodes
}

message Graph {
  repeated Vertex vertices = 1;
}

Then I could write classes that wrap the protobuf-generated classes, and automatically convert these identifiers to real pointers on deserialization (and back to ids on serialization). Is this the best approach? If so, then does anyone know of existing projects that use/document this approach? If not, then what approach would you recommend?

like image 772
Edward Loper Avatar asked Nov 04 '22 09:11

Edward Loper


1 Answers

If you need cross platform support, then using a DTO as you propose in the question, then mapping that to/from a separate graph-based model in your own code is probably your best approach.

As a side note, in protobuf-net (c# / .net) I've added support for this which adds a layer of abstraction silently. Basically, the following works:

[ProtoContract]
class Vertex {
   ...
    [ProtoMember(3, AsReference = true)]
    public List<Vertex> OutgoingEdges {get;set;}
}
like image 199
Marc Gravell Avatar answered Nov 09 '22 07:11

Marc Gravell