如何将有向无环图(DAG)存储为JSON?

时间:2023-02-02 15:02:18

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.

我想将DAG表示为JSON文本,并想知道是否有人尝试过此以及他们在验证JSON是否实际上是DAG时所遇到的任何问题。

3 个解决方案

#1


27  

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.

您可以通过这种方式存储多种图形,而不仅仅是DAG,因此您需要对其进行后期处理以确保它没有循环。只需选择一个节点DFS,如果您多次看到任何节点它不是DAG。然后删除刚刚看到的所有节点,并重复剩余的任何节点。执行此操作直到找到循环或删除所有节点,在后一种情况下,图形是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.

请注意,这不会存储父节点,因为这是冗余信息。如果需要该数据,可以在加载图表后生成这些数据。

#2


3  

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

除非您使用自己的约定来表示链接数据,否则JSON没有表示DAG的本机工具。 JSON-LD(一个W3C提案)是一个JSON扩展,它试图做到这一点。该提案可在此处找到:http://json-ld.org/spec/latest/json-ld/。

#3


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.

严格来说,你无法直接使用JSON。您必须提出自己的方式来表示可以通过数据结构中的其他位置引用来标识的对象,然后您必须对反序列化JSON字符串的结果进行后处理。

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.

由于JSON表达式是对象图的简单原因,您无法使用JSON,并且根本没有规定表达属性值应该是数据结构中其他属性的值的概念。换句话说,图中没有对象可以有多个父对象,这意味着每个对象都是另一个对象的一个​​属性的值。

#1


27  

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.

您可以通过这种方式存储多种图形,而不仅仅是DAG,因此您需要对其进行后期处理以确保它没有循环。只需选择一个节点DFS,如果您多次看到任何节点它不是DAG。然后删除刚刚看到的所有节点,并重复剩余的任何节点。执行此操作直到找到循环或删除所有节点,在后一种情况下,图形是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.

请注意,这不会存储父节点,因为这是冗余信息。如果需要该数据,可以在加载图表后生成这些数据。

#2


3  

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

除非您使用自己的约定来表示链接数据,否则JSON没有表示DAG的本机工具。 JSON-LD(一个W3C提案)是一个JSON扩展,它试图做到这一点。该提案可在此处找到:http://json-ld.org/spec/latest/json-ld/。

#3


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.

严格来说,你无法直接使用JSON。您必须提出自己的方式来表示可以通过数据结构中的其他位置引用来标识的对象,然后您必须对反序列化JSON字符串的结果进行后处理。

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.

由于JSON表达式是对象图的简单原因,您无法使用JSON,并且根本没有规定表达属性值应该是数据结构中其他属性的值的概念。换句话说,图中没有对象可以有多个父对象,这意味着每个对象都是另一个对象的一个​​属性的值。