How to serialize a graph structure?

前端 未结 6 911
一整个雨季
一整个雨季 2021-02-02 08:11

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

But many problems are b

6条回答
  •  终归单人心
    2021-02-02 08:55

    How do you represent your graph in memory?
    Basically you have two (good) options:

    • an adjacency list representation
    • an adjacency matrix representation

    in which the adjacency list representation is best used for a sparse graph, and a matrix representation for the dense graphs.

    If you used suchs representations then you could serialize those representations instead.

    If it has to be human readable 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:

       1  2  3
    1 #t #f #f
    2 #f #f #t
    3 #f #t #f
    

    (this is a non-optimized, non weighted representation, but can be used for directed graphs)

提交回复
热议问题