C++ 中的散列指针值
在尝试进行 DFS 时,保存所有已访问节点列表的最佳数据结构是什么?如果每个节点都有唯一的 ID,一种方法是维护这些唯一 ID 的哈希值。如果它们没有唯一的 ID,散列节点是否可行?
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 do not have a unique id, is hashing nodes viable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将所有访问过的节点放入哈希表中,而是将它们放入堆栈中。如果将访问过的节点放入堆栈中,则可以更轻松地回溯并遵循搜索的其他分支。
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.