简体   繁体   English

包含图节点作为键的无序图

[英]Unordered map containing a graph node as the key

I am working on implementing dijsktra's algorithm for traversing a graph and I have hit a small roadblock. 我正在努力实现dijsktra的遍历图形的算法,但遇到了一个小障碍。

class Node{

private:
int id = -1;
unordered_map<Node*, int> edges;

};

As you can see each node in the graph contains an int for an id as well as an unordered map containing a node* as the key value and the edge length as the underlying data. 如您所见,图中的每个节点都包含一个id的int以及一个无序映射,该映射包含一个node *作为键值,边缘长度作为基础数据。 The issue i'm having is I do not know what the best way to hash these id's. 我遇到的问题是我不知道哈希这些ID的最佳方法是什么。

Id's will be read in from a file and numbered in order. ID将从文件中读取并按顺序编号。 What is the best way to hash these id's without having many collisions? 在没有很多冲突的情况下哈希这些ID的最佳方法是什么?

EDIT: I currently am just hashing in such a way that returns the id mod edges.size() but I don't believe this to be the best way. 编辑:我目前只是以返回id mod edge.size()的方式进行哈希处理,但我不认为这是最好的方法。

Don't even bother with the % edges.size() . 甚至不必理会% edges.size() The unordered_map itself will already perform a % this -> _Table.size() . unordered_map本身已执行% this -> _Table.size()

Note that for sparsely connected graphs (say < 8 edges/node) a simple std::vector<std::pair<Node*,int>> is probably more efficient. 请注意,对于稀疏连接的图(例如,<8个边/节点),简单的std::vector<std::pair<Node*,int>>可能更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM