5

I am using Erlang's digraph module for storing Directed Acyclic Graphs (DAGs). So for argument's sake here is a super simple graph (using Elixir):

gr = :digraph.new()
:digraph.add_vertex(gr, "A")
:digraph.add_vertex(gr, "B")
:digraph.add_vertex(gr, "C")
:digraph.add_edge(gr, "A", "B")
:digraph.add_edge(gr, "A", "C")

which looks like this:

enter image description here

We can see it's all worked:

iex(7)> :digraph.vertices(gr)
["A", "C", "B"]
iex(8)> :digraph.edges(gr)
[[:"$e" | 0], [:"$e" | 1]]
iex(9)> :digraph.out_neighbours(gr, "A")
["C", "B"]
iex(10)> :digraph.out_neighbours(gr, "B")
[]
iex(11)> :digraph.out_neighbours(gr, "C")
[]
iex(12)> :digraph_utils.is_acyclic(gr)
true

Now I'm going to be adding and removing more vertices and edges, but I would like to transmit these graphs to applications outside of the Elixir/Erlang ecosystem such as Cytoscape.js. Is there a standardized way to serialize digraphs into some industry standard readable format (json or xml for example), such as JGF, Netlix's Falcor JSON Graph format, or other?

I could write my own serializer but I'd prefer something pre-existing. I can't find anything that does this in digraph or digraph_utils.

Thomas Browne
  • 23,824
  • 32
  • 78
  • 121
  • For those who got here wanting to serialize/de-serialize `digraph`s to save process state, pass graphs between processes etc. within BEAM-based languages: [How to pass a digraph to a different process and node?](https://stackoverflow.com/questions/15552796/how-to-pass-a-digraph-to-a-different-process-and-node) – toraritte Apr 05 '20 at 01:28

1 Answers1

5

Searching there are various solutions for exporting digraph data to various popular formats, but nothing canonical Two of the most popular of these formats are DOT and GraphML.

Some Elixir and Erlang libraries for exporting digraphs to various formats:

Some Elixir and Erlang examples of updating the front-end in soft realtime using js libs such as vis.js and web sockets:

starbelly
  • 254
  • 1
  • 4
  • 1
    Unfortunately many of these are more than 5 years old and, while that may be fine, I'm just a bit worried about packages being maintained. Nevertheless https://github.com/fenollp/erlang-dot looks good especially when combined with some of the solutions outlined here: https://stackoverflow.com/questions/22595493/reading-dot-files-in-javascript-d3. If you want to add that last url to your answer, I will accept it. Many thanks for your hard work, and welcome to Stack Overflow! – Thomas Browne Dec 16 '18 at 11:01
  • actually erlang-dot doesn't seem to complile under Elixir, graphvix seems to be a _competitor_ to digraph and looks very patchy because it uses a genserver to store graphs (DDOS risk), while digraph_viz doesn't give me an export option that I can see. I'm afraid none of these really answers my question that I can see unless you can show me otherwise. – Thomas Browne Dec 16 '18 at 22:57
  • Graphvix is a wrapper for digraph and not a competitor as evident here: https://github.com/mikowitz/graphvix/blob/e437a1dcdbca5ce64a3d7504a1b09e4f90daafeb/lib/graphvix/graph.ex#L74 It also does not make use of a gen_server. However, making use of a gen_server does not entail any security risks. It is quite common for a "lib" to use gen servers to hold state. Meanwhile, digraph_vis indeed has functions for exporting as evident here: https://github.com/jabarszcz/digraph_viz/blob/a961d940eb3f0b516d46777c29e03a918027d5dd/src/digraph_viz.erl#L29 – starbelly Dec 16 '18 at 23:21
  • perfect thank you. Please show some code in the answer and I will accept it. Note that I need a serialization, that will not necessarily be written to disk but may need to be transmitted over the wire via zeromq or websockets etc. – Thomas Browne Dec 17 '18 at 00:42