在 JSON 中表示图表
Representing a graph in JSON
受到 this 问题的启发,我试图在 JSON 中表示一个 DAG。我的案例包括包含一些数据的边和节点(而不是本例中的字符串)。我在想这样的规范:
{
"graph": {
"a": ["b", "c"],
"b": ["c"]
"c"
},
"nodes": {
"a": {
"name": "Adam"
},
"b": {
"name": "Bob"
},
"c": {
"name": "Caillou"
}
},
"edges": {
// how to do the same for edges?
// ie: how to query edges ?
}
}
我的一个想法是让边的键成为它连接的两个顶点 ID 的串联。例如,ab
、ac
和 bc
是该图中的三个边。我想知道是否有更标准的方法来做到这一点。
编辑:这就是我现在的想法
{
"graph": {
"a": {
"data": {
// a's vertex data
},
"neighbors": {
"b": {
"data": {
// data in edge ab
}
},
"c": {
"data": {
// data in edge ac
}
}
}
},
"b": {
"data": {
// b's vertex data
},
"neighbors": {
"c": {
"data": {
// data in edge bc
}
}
}
},
"c": {
"data": {
// c's vertex data
}
}
}
}
由于 DAG 的边保存数据,它们最好有自己的标识符,就像节点一样。也就是说,json表示应该由三个部分组成:
- 节点记录:将每个节点标识符映射到节点的数据。
- 边记录:将每个边标识符映射到边的数据。
邻接表:将每个节点标识符映射到一个边标识符数组,每个边都对应一个节点的边。
DAG = {
"adjacency": {
"a": ["1", "2"],
"b": ["3"]
},
"nodes": {
"a": {
// data
},
"b": {
// data
},
"c": {
// data
}
},
"edges": {
"1": {
"from": "a", "to": "b",
"data": {
// data
}
},
"2": {
"from": "a", "to": "b",
"data": {
// data
}
},
"3": {
"from": "b", "to": "c",
"data": {
// data
}
}
}
}
原来有一些标准正试图为这类事情出现。我最近不得不为我自己的项目查找这些。您可能对 http://jsongraphformat.info/ for example, or one of the peer projects it references on its website. Goals include trying to represent in JSON anything you can represent in the DOT language (https://en.wikipedia.org/wiki/DOT_(graph_description_language)).
感兴趣
json-ld就是为此而生的。它有一个半陡峭的学习曲线,但它是一种在 json 中表示图形数据的可靠方法。
受到 this 问题的启发,我试图在 JSON 中表示一个 DAG。我的案例包括包含一些数据的边和节点(而不是本例中的字符串)。我在想这样的规范:
{
"graph": {
"a": ["b", "c"],
"b": ["c"]
"c"
},
"nodes": {
"a": {
"name": "Adam"
},
"b": {
"name": "Bob"
},
"c": {
"name": "Caillou"
}
},
"edges": {
// how to do the same for edges?
// ie: how to query edges ?
}
}
我的一个想法是让边的键成为它连接的两个顶点 ID 的串联。例如,ab
、ac
和 bc
是该图中的三个边。我想知道是否有更标准的方法来做到这一点。
编辑:这就是我现在的想法
{
"graph": {
"a": {
"data": {
// a's vertex data
},
"neighbors": {
"b": {
"data": {
// data in edge ab
}
},
"c": {
"data": {
// data in edge ac
}
}
}
},
"b": {
"data": {
// b's vertex data
},
"neighbors": {
"c": {
"data": {
// data in edge bc
}
}
}
},
"c": {
"data": {
// c's vertex data
}
}
}
}
由于 DAG 的边保存数据,它们最好有自己的标识符,就像节点一样。也就是说,json表示应该由三个部分组成:
- 节点记录:将每个节点标识符映射到节点的数据。
- 边记录:将每个边标识符映射到边的数据。
邻接表:将每个节点标识符映射到一个边标识符数组,每个边都对应一个节点的边。
DAG = { "adjacency": { "a": ["1", "2"], "b": ["3"] }, "nodes": { "a": { // data }, "b": { // data }, "c": { // data } }, "edges": { "1": { "from": "a", "to": "b", "data": { // data } }, "2": { "from": "a", "to": "b", "data": { // data } }, "3": { "from": "b", "to": "c", "data": { // data } } } }
原来有一些标准正试图为这类事情出现。我最近不得不为我自己的项目查找这些。您可能对 http://jsongraphformat.info/ for example, or one of the peer projects it references on its website. Goals include trying to represent in JSON anything you can represent in the DOT language (https://en.wikipedia.org/wiki/DOT_(graph_description_language)).
感兴趣json-ld就是为此而生的。它有一个半陡峭的学习曲线,但它是一种在 json 中表示图形数据的可靠方法。