elm-community / graph

Functional Graph Library in Elm.

Home Page:http://package.elm-lang.org/packages/elm-community/graph/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GraphViz export doesn't preserve the structure of the graph

jhrcek opened this issue · comments

At the moment the Graph.GraphViz renders String in graphviz format by using the node labels as "identifiers" of the nodes. This is OK when graphs have non-empty String labels AND all labels are different. But it causes problems when I have multiple nodes with same names or with empty labels. An example graph that this library renders incorrectly is graph with 2 nodes with empty label "" connected by an edge.

This gets rendered by this library as single node with a loop

digraph G {
  "" -> ""

  ""
  ""
}

screenshot from 2018-01-27 09-55-28

But I want it to be rendered so that the 2 nodes remain separate, which could be achieved in graphviz like this:

digraph G {
  1 -> 2

  1 [label=""]
  2 [label=""]
}

screenshot from 2018-01-27 09-55-50

@sgraf812 would you be ok with changing the internal implementation of graphviz String rendering so that the output graph uses node IDs like in the 2nd example above and would set node labels using the syntax
NodeId [label="..."]? If yes, I can prepare a PR implementing this.

Apart from preserving the structure of the graph (based on connectivity of nodes based on their IDs as opposed to relying on labels which are not guarranteed to be unique/nonempty) this would have the additional benefit of being less redundant (e.g. labels with long text wouldn't be repeated multiple times in the graphviz output).

Seems like the proper solution. Honestly, that module was a contribution, so this is more or less NIH.

Since I accepted another export format just last week, I should spend the time to make sure these follow the same/similar API.

Released as 5.0.0.