2 ms·
Graph data structures are almost always better represented as some sort of array, with indices for links, than as loose objects on the heap connected with point
by ncmncm 5y ago
Graph data structures are almost always better represented as some sort of array, with indices for links, than as loose objects on the heap connected with pointers. (This is true in any language except Lisp.) Often the links should themselves be entries in another array, and not just indices, because in the real world you probably want to be able to annotate the links, too. Sometimes only the links.
Heap-allocated graph nodes are really only for CS classes.
At the opposite extremum, use an NxN bit map, with a 1 at a[n,m] to indicate a link between n and m. Or a regular NxN array, with a value V indicating a weight between n and m. Again, no pointers.