Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing a graph in JSON

Inspired by this question, I'm trying to represent a DAG in JSON. My case includes edges and nodes that contain some data (rather than just strings as in this example). I was thinking of a spec like this:

{
    "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 ?
    }
}

One idea I had was to make the keys of the edges be the concatenation of the two vertex ids that it connects. For example, ab, ac, and bc are the three edges in this graph. I want to know if there's a more standard way of doing this.

EDIT: This is what I'm thinking of now

{
    "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
            }
        }
    }
}
like image 737
Carpetfizz Avatar asked Mar 27 '17 16:03

Carpetfizz


People also ask

What is graph JSON?

What is JSON 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.

What is JSON representation?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).


1 Answers

Since the DAG's edges hold data, they better have their own identifiers, just like the nodes. That is, the json representation should be composed of three components:

  1. Node records: mapping each node identifier to the node's data.
  2. Edge records: mapping each edge identifier to the edge's data.
  3. Adjacency lists: mapping each node identifier to an array of edge identifiers, each corresponds to an edge going out of the node.

    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
          }
        }
      }
    }
    
like image 134
snakile Avatar answered Sep 19 '22 10:09

snakile