32

I want to represent a DAG as JSON text and wondering if anyone has tried this and any issues they dealt with in regards to validating if the JSON is actually a DAG.

SamB
  • 8,218
  • 5
  • 44
  • 52
828
  • 1,060
  • 1
  • 12
  • 21

3 Answers3

40

Label each node and make an edge list. That is, for each node store the nodes that it has edges to, for example:

{
  "a": [ "b", "c", "d" ],
  "b": [ "d" ],
  "c": [ "d" ],
  "d": [ ]
}

You can store many kinds of graphs this way, not just DAGs, so you will need to post-process it to make sure that it has no loops. Just pick a node, DFS, if you see any node more than once it is not a DAG. Then remove all of the nodes you just saw and repeat with any remaining nodes. Do this until you find a loop or you've removed all of the nodes, in the latter case the graph is a DAG.

Note that this does not store parent nodes, since that is redundant information. You can generate those after loading the graph if you need that data.

Running Wild
  • 2,835
  • 15
  • 15
  • 1
    Is there any downside to storing it inversely (having the array values be the "from" nodes instead of "to") ? I would think this would be better if you are trying to do a topological sort. – James Wierzba Apr 04 '16 at 09:47
4

JSON has no native facility to represent DAGs unless you make your own convention to represent linked data. JSON-LD (a W3C proposal) is a JSON extension that is trying to do exactly that. The proposal can be found here: http://json-ld.org/spec/latest/json-ld/.

dave
  • 4,160
  • 2
  • 24
  • 24
1

Strictly speaking you cannot do it with JSON directly. You'd have to come up with your own way of representing objects that can be identified by reference elsewhere in the data structure, and then you'd have to post-process the result of deserializing the JSON string.

You can't do it with JSON for the simple reason that the JSON expression is the object graph, and there's simply no provisions for expressing the notion that the value of a property should be the value of another property elsewhere in the data structure. To put it another way, no object in the graph can have more than one parent, which implies that every object is the value of exactly one property of one other object.

Pointy
  • 371,531
  • 55
  • 528
  • 584
  • 2
    In a DAG, a node can have two parents. Think of the representation of an expression in a programming language (say, JavaScript :-) with two references to a single variable. Traditionally, those two points in the expression would refer to the same node. There are no cycles in a DAG (by definition) because (usually; not necessarily I guess) all the links "point down" the graph, so you never "go back up". It's like a tree, except where like nodes are merged. – Pointy Mar 27 '12 at 21:40
  • Ha ha well that comment was in response to a question about this having to do with cycles, so I'll leave it there. – Pointy Mar 27 '12 at 21:40
  • Yes, basic CS understanding failure on my part ;-) Thanks for explaining. –  Mar 27 '12 at 21:41