Three ways to store a graph in memory, advantages and disadvantages

后端 未结 7 1723
别跟我提以往
别跟我提以往 2020-12-04 07:09

There are three ways to store a graph in memory:

  1. Nodes as objects and edges as pointers
  2. A matrix containing all edge weights between numbered node x a
7条回答
  •  一整个雨季
    2020-12-04 07:32

    A couple more things to consider:

    1. The matrix model lends itself more easily to graphs with weighted edges, by storing the weights in the matrix. The object/pointer model would need to store edge weights in a parallel array, which requires synchronization with the pointer array.

    2. The object/pointer model works better with directed graphs than undirected graphs because the pointers would need to be maintained in pairs, which can become unsynchronized.

提交回复
热议问题