I was initially doing it in two ways. One way is by storing visited nodes in a list and traversing the list to determine if a node has been visited before. Another was using a boolean array which keeps track of both visited and unvisited nodes. It really interested me, what is the best way?
Share
One approach, which can be useful for micro-optimization (better cache behavior, avoids lookup), is storing a flag on each node object. The obvious drawback is that the graph algorithms are not re-entrant. Want to do a different graph traversal to make a decision during traversal? Well, you can’t. You also have to remember to clear all these flags afterwards.
The other category is maintaining a separate data structure on a per-traversal basis. Both of the approaches you give fall under this, although the list approach is very inefficient — O(n) time for every lookup. The boolean array (possibly compressed into a bitset; this option is extremely space efficient) is simple and efficient in both time and space, but requires that the nodes have consecutive indices/ids. This is not always given, and has consequences for other parts of the graph handling.
If you don’t have that, and instead refer to graph objects with pointers, you can use a mapping based on that. In higher-level languages, such as Python, this is very simple (and due to highly tuned hash table/set data structures, rather efficient too).
The obvious advantage is that graph traversals are re-entrant. While this may not sound like much, I can attest that situations where this is useful do arise at times.