Above my head, I hope I have the right facts.
Conceptually, the graph tries to imagine how a set of nodes (or vertices) is connected (connected) to each other (through edges). However, in a real physical device (memory) we have a continuous array of memory cells.
So, to represent the graph, we can choose to use a matrix. In this case, we use the vertex index as a row and column, and the record has a value of 1 if the vertices are adjacent to each other, 0 otherwise.
Alternatively, you can also represent the graph by selecting an object to represent node / vertex, which points to a list of all nodes adjacent to it.
The matrix representation gives an advantage when the graph is dense, which means that most nodes / vertices are connected to each other. This is due to the fact that in such cases, using the matrix entry, this eliminates the need to allocate an additional pointer (which requires word size memory) for each connection.
For a sparse graph, the approach to the list is better because you do not need to consider 0 entries when there is no connection between the vertices.
Hope this helps.
source share