开发者

Hashing pointer values in C++

开发者 https://www.devze.com 2023-03-01 02:11 出处:网络
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开发

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...

  1. If you are ever copying the nodes, then they will get a new address.

  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消