There are three ways to store a graph in memory:
- Nodes as objects and edges as pointers
- A matrix containing all edge weights between numbered node x and node y
- A list of edges between numbered nodes
I know how to write all three, but I’m not sure I’ve thought of all of the advantages and disadvantages of each.
What are the advantages and disadvantages of each of these ways of storing a graph in memory?
One way to analyze these is in terms of memory and time complexity (which depends on how you want to access the graph).
Storing nodes as objects with pointers to one another
Storing a matrix of edge weights
Depending on what algorithm you run on the graph and how many nodes there are, you’ll have to choose a suitable representation.