Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Graph representation

This thread is locked; no one can reply to it. rss feed Print
Graph representation
Ariesnl
Member #2,902
November 2002
avatar

I was reviewing some graph based algorithms and tutorials on the web.
And one thing I noticed.. all talk about adjecency lists or matrices.
Why not nodes and pointers ? (this is what I always use, since it actually creates a graph in memory!) is it inefficient ?

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

axilmar
Member #1,204
April 2001

Arrays have better cpu cache locality.

Elias
Member #358
May 2000

I feel for some algorithms one is better than the other. In general if you have a small number of nodes and need a result for every node I found adjacency matrices very fast and elegant. I think in one of my games I have a number of cities and roads and needed the shortest distance of any city to any other city.

If you have a larger number of nodes and/or only need something for a single node then adjacency matrices might make less sense. E.g. get the distance from point A to point B when you have a million points - a good algorithm probably only will touch the points on the path between A and B. So assume the result is a path that is 100 points long then standard A* likely will only create a temporary graph with 200 or 300 or so nodes, not the entire million. If you use an adjacency matrix you start by creating a matrix with 1000000x1000000 entries and probably run out of memory immediately... :P

--
"Either help out or stop whining" - Evert

Go to: