简体   繁体   English

具有恒定时间复杂度的无向图边的数据结构

[英]Datastructure for undirected graph edges with constant time complexity

I have a undirected graph where the nodes are stored in a flat array. 我有一个无向图,其中节点存储在平面数组中。 Now I am looking for a data structure for the edges. 现在,我正在寻找边缘的数据结构。 It should have constant time complexity for getting all edges of a given node. 为了获得给定节点的所有边缘,它应该具有恒定的时间复杂度。 An edge contains two node indices and additional information such as a weight. 一条边包含两个节点索引和其他信息,例如权重。

The only way I see is duplicating the data, one sorted by the left node and another sorted by the right node. 我看到的唯一方法是复制数据,一种按左节点排序,另一种按右节点排序。

vector<vector<int>> left, right;

But I would like to prevent duplicating the edges. 但我想避免重复边缘。

It sounds like you just want an adjacency list representation. 听起来您只是想要一个邻接列表表示形式。

In this representation, each node would store a list of all its connected edges. 在此表示形式中,每个节点将存储其所有连接边的列表。

For an undirected graph, you can have each endpoint both store the edge. 对于无向图,可以让每个端点都存储边。

There isn't really a way to get the connected edges for a node in constant time without some duplication. 确实没有一种方法可以在恒定时间内获得节点的连接边缘而不进行重复。 But you can just store a pointer, reference or unique ID (which can be an index in an edge array, for example) to the actual edge, preventing the need to actually have 2 copies of it floating around. 但是,您可以只存储指向实际边缘的指针,引用或唯一ID(例如,可以是边缘数组中的索引),从而无需实际浮动2个副本。

Make a vector of vectors. 使向量成为向量。

Each node will have a vector of all the nodes it has. 每个节点将具有其所有节点的向量。

You should build this during the graph creation. 您应该在图形创建期间构建它。

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

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