Hashing pointer values in C++
In trying to do DFS, what is the best data structure to hold the list of all already visited nodes? If each node has a unique id, one way would be to maintain a hash of these unique ids. If they d开发者_JAVA技巧o not have a unique id, is hashing nodes viable?
Instead of putting all the nodes you've visited in a hash table, put them in a stack. If you put visited nodes in a stack, you make it much easier to backtrack and follow other branches of the search.
Let's think of reasons why the addresses wouldn't be a unique identifier...
If you are ever copying the nodes, then they will get a new address.
If you ever free the nodes and allocate new ones, then the newly allocated ones could re-use some previous address.
If you can satisfactorily say that the above doesn't apply (my guess is they won't) then sure, go right ahead.
精彩评论