Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store a Directed Acyclic Graph (DAG) as JSON?

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.

like image 316
828 Avatar asked Mar 27 '12 21:03

828


People also ask

How do you store directed acyclic graphs?

A simple way is to save first all nodes assigning to each of them a node ID, then save all arcs using the node ID of starting and ending node. This will handle all cases (including non-connected graphs, multiply connected graphs, loops... etc.) Show activity on this post.

What is DAG in JSON?

Specification: DAG-JSON. Status: Descriptive - Final. DAG-JSON supports the full IPLD Data Model. DAG-JSON uses the [JavaScript Object Notation (JSON)] data format, defined by RFC 8259.

Is JSON a graph?

JSON Graph is a convention for modeling graph information as a JSON object. Applications that use Falcor represent all their domain data as a single JSON Graph object. JSON Graph is valid JSON and can be parsed by any JSON parser.


3 Answers

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.

like image 82
Running Wild Avatar answered Nov 09 '22 17:11

Running Wild


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/.

like image 33
dave Avatar answered Nov 09 '22 16:11

dave


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.

like image 35
Pointy Avatar answered Nov 09 '22 16:11

Pointy