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

后端 未结 7 1728
别跟我提以往
别跟我提以往 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

    Okay, so if edges don't have weights, the matrix can be a binary array, and using binary operators can make things go really, really fast in that case.

    If the graph is sparse, the object/pointer method seems a lot more efficient. Holding the object/pointers in a data structure specifically to coax them into a single chunk of memory might also be a good plan, or any other method of getting them to stay together.

    The adjacency list - simply a list of connected nodes - seems by far the most memory efficient, but probably also the slowest.

    Reversing a directed graph is easy with the matrix representation, and easy with the adjacency list, but not so great with the object/pointer representation.

提交回复
热议问题