存储无向图最有效的方法是什么?
正如标题所说.. 在java中存储连通图最有效的方法是什么?
例如,假设我有多个位置以各种方式相互连接,我必须遍历图表以查看它是否已连接。任何帮助/评论都会有所帮助,谢谢!
As the title says.. What is the most efficient way to store a connected graph in java?
For example lets say I have multiple locations connecting to each other in various ways and I have to traverse through the graph to see if it's connected.. any help/comment would be helpful thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种常用的表示形式是由图中所有节点索引的矩阵(二维数组),其中如果存在来自节点
i 的有向边,则
到M[i,j] == true
j
。该主题的一个变体是存储两个节点之间的边的长度/权重(其中缺失的边可以用值 -1 表示)。One often used representation is a matrix (two dimensional array) indexed by all the nodes in the graph, where
M[i,j] == true
if there is a directed edge from nodei
toj
. A variation on the theme is to store the length / weight of the edge between the two nodes (where a missing edge may be represented by the value -1).使用不对称的邻接矩阵,从而表示方向而不是简单的邻接。
use an adjacency matrix without the symmetry, thereby representing direction instead of simple adjacency.
使用发生率或邻接 矩阵就可以了。
如果您使用邻接矩阵,则稀疏矩阵可能会更有效,如果您有很多节点。 Colt 提供稀疏矩阵实现。信息取自这篇文章。
另外,我之前已经成功使用过 JUNG ,所以你可能想看看引擎盖下的情况他们如何实现有向图。
Using an incidence or adjacency matrix will do the trick.
If you use an adjacency matrix, a sparse matrix might be efficient to use, if you have a lot of nodes. Colt provides a sparse matrix implementation. Info taken from this SO post.
Also, I've used JUNG sucessfully before, so you might want to take a peek under the hood to see how they've implemented directed graphs.
为了提高查找效率 - 使用布尔矩阵(如果有连接节点的弧则为 true,如果没有则为 false)。为了提高内存效率,将对象属性之一定义为其指向的对象的(动态)列表。请注意,只有当图中有很多节点时,后者才会有帮助。
For lookup efficiency - use a boolean matrix (true if there is an arc connecting the nodes, false if there isn't). For memory efficiency define one of your object properties as a (dynamic) list of objects its pointing to. Note the latter will only help if you have a LOT of nodes in your graph.
我错过了什么吗?数据已经是一个图,只需将其序列化即可。对于这个问题,书面谈论矩阵是完全不成熟的。
Am I missing something? The data is already a graph, just serialize it.For the question as written talk of matrices is completely premature.