<p>Flat files and relational databases give us a mechanism to serialize structured data. XML is superb for serializing un-structured tree-like data.</p>

<p>But many problems are best represented by graphs. A thermal simulation program will, for instance, work with temperature nodes connected to each others through resistive edges.</p>

<p>So what is the best way to serialize a graph structure? I know XML can, to some extent, do it---in the same way that a relational database can serialize a complex web of objects: it usually works but can easily get ugly.</p>

<p>I know about the dot language used by the graphviz program, but I'm not sure this is the best way to do it. This question is probably the sort of thing academia might be working on and I'd love to have references to any papers discussing this.</p>

[{'answer_id': 51794, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 3, 'selected': False, 'text': "<p>XML is very verbose.  Whenever I do it, I roll my own.  Here's an example of a 3 node directed acyclic graph.  It's pretty compact and does everything I need it to do:</p>\n\n<pre><code>0: foo\n1: bar\n2: bat\n----\n0 1\n0 2\n1 2\n</code></pre>\n"}, {'answer_id': 51804, 'author': 'Erlend Halvorsen', 'author_id': 1920, 'author_profile': 'https://Stackoverflow.com/users/1920', 'pm_score': 0, 'selected': False, 'text': '<p>On a less academic, more practical note, in <a href="http://cubictest.openqa.org" rel="nofollow noreferrer">CubicTest</a> we use <a href="http://xstream.codehaus.org/" rel="nofollow noreferrer">Xstream</a> (Java) to serialize tests to and from xml. Xstream handles graph-structured object relations, so you might learn a thing or two from looking at it\'s source and the resulting xml. You\'re right about the <em>ugly</em> part though, the generated xml files don\'t look pretty.</p>\n'}, {'answer_id': 51814, 'author': 'Nick Fortescue', 'author_id': 5346, 'author_profile': 'https://Stackoverflow.com/users/5346', 'pm_score': 1, 'selected': False, 'text': '<p>One example you might be familiar is Java serialization. This effectively serializes by graph, with each object instance being a node, and each reference being an edge. The algorithm used is recursive, but skipping duplicates. So the pseudo code would be:</p>\n\n<pre><code>serialize(x):\n    done - a set of serialized objects\n    if(serialized(x, done)) then return\n    otherwise:\n         record properties of x\n         record x as serialized in done\n         for each neighbour/child of x: serialize(child)\n</code></pre>\n\n<p>Another way of course is as a list of nodes and edges, which can be done as XML, or in any other preferred serialization format, or as an adjacency matrix.</p>\n'}, {'answer_id': 51816, 'author': 'sven', 'author_id': 46, 'author_profile': 'https://Stackoverflow.com/users/46', 'pm_score': 5, 'selected': True, 'text': '<p>How do you represent your graph in memory?<br>\nBasically you have two (good) options:</p>\n\n<ul>\n<li><a href="http://en.wikipedia.org/wiki/Adjacency_list" rel="noreferrer">an adjacency list representation</a></li>\n<li><a href="http://en.wikipedia.org/wiki/Adjacency_matrix" rel="noreferrer">an adjacency matrix representation</a></li>\n</ul>\n\n<p>in which the adjacency list representation is best used for a sparse graph, and a matrix representation for the dense graphs.</p>\n\n<p>If you used suchs representations then you could serialize those representations instead.</p>\n\n<p>If it has to be <em>human readable</em> you could still opt for creating your own serialization algorithm. For example you could write down the matrix representation like you would do with any "normal" matrix: just print out the columns and rows, and all the data in it like so:</p>\n\n<pre><code>   1  2  3\n1 #t #f #f\n2 #f #f #t\n3 #f #t #f\n</code></pre>\n\n<p>(this is a non-optimized, non weighted representation, but can be used for directed graphs)</p>\n'}, {'answer_id': 51838, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Typically relationships in XML are shown by the parent/child relationship. XML can handle graph data but not in this manner. To handle graphs in XML you should use the <a href="http://www.zvon.org/xxl/xmlSchema2001Reference/Standard/xmlschema-2.html#ID" rel="noreferrer">xs:ID</a> and <a href="http://www.zvon.org/xxl/xmlSchema2001Reference/Standard/xmlschema-2.html#IDREF" rel="noreferrer">xs:IDREF</a> schema types.</p>\n\n<p>In an example, assume that node/@id is an xs:ID type and that link/@ref is an xs:IDREF type. The following XML shows the cycle of three nodes 1 -> 2 -> 3 -> 1.</p>\n\n<pre><code>&lt;data&gt;\n  &lt;node id="1"&gt; \n    &lt;link ref="2"/&gt;\n  &lt;/node&gt;\n  &lt;node id="2"&gt;\n    &lt;link ref="3"/&gt;\n  &lt;/node&gt;\n  &lt;node id="3"&gt;\n    &lt;link ref="1"/&gt;\n  &lt;/node&gt;\n&lt;/data&gt;\n</code></pre>\n\n<p>Many development tools have support for ID and IDREF too. I have used Java\'s JAXB (Java XML Binding. It supports these through the <a href="http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/XmlID.html" rel="noreferrer">@XmlID</a> and the <a href="http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/XmlIDREF.html" rel="noreferrer">@XmlIDREF</a> annotations. You can build your graph using plain Java objects and then use JAXB to handle the actual serialization to XML.</p>\n'}, {'answer_id': 145095, 'author': 'jbl', 'author_id': 2353001, 'author_profile': 'https://Stackoverflow.com/users/2353001', 'pm_score': 1, 'selected': False, 'text': "<p>Adjacency lists and adjacency matrices are the two common ways of representing graphs in memory. The first decision you need to make when deciding between these two is what you want to optimize for. Adjacency lists are very fast if you need to, for example, get the list of a vertex's neighbors. On the other hand, if you are doing a lot of testing for edge existence or have a graph representation of a markov chain, then you'd probably favor an adjacency matrix.</p>\n\n<p>The next question you need to consider is how much you need to fit into memory. In most cases, where the number of edges in the graph is much much smaller than the total number of possible edges, an adjacency list is going to be more efficient, since you only need to store the edges that actually exist. A happy medium is to represent the adjacency matrix in compressed sparse row format in which you keep a vector of the non-zero entries from top left to bottom right, a corresponding vector indicating which columns the non-zero entries can be found in, and a third vector indicating the start of each row in the column-entry vector.</p>\n\n<pre><code>[[0.0, 0.0, 0.3, 0.1]\n [0.1, 0.0, 0.0, 0.0]\n [0.0, 0.0, 0.0, 0.0]\n [0.5, 0.2, 0.0, 0.3]]\n</code></pre>\n\n<p>can be represented as:</p>\n\n<pre><code>vals: [0.3, 0.1, 0.1, 0.5, 0.2, 0.3]\ncols: [2,   3,   0,   0,   1,   4]\nrows: [0,        2, null,  4]\n</code></pre>\n\n<p>Compressed sparse row is effectively an adjacency list (the column indices function the same way), but the format lends itself a bit more cleanly to matrix operations.</p>\n"}]
['https://Stackoverflow.com/questions/51783', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1428/']