简体   繁体   English

在图中编码连接的节点

[英]coding connected nodes in graph

I am trying to make a graph in java that would have different nodes. 我正在尝试在Java中创建一个具有不同节点的图形。 some nodes would be connected to others and some wont. 一些节点将连接到其他节点,而另一些则不会。 If they are connected then some boolean value for that node will be true and another variable will hold the value of the node it is connected to. 如果它们已连接,则该节点的某个布尔值将为true,而另一个变量将保存与其连接的节点的值。

...any suggestions on what you guys think is the best way to approach this? ...关于你们认为是解决此问题的最佳方法的任何建议?

The two most common ways to represent a graph are adjacency matrix and adjacency lists. 表示图形的两种最常见方法是邻接矩阵和邻接表。 Let n be the number of nodes. 令n为节点数。

Adjacency matrix A is an nxn matrix of boolean values, such that A(i, j) = 1 if nodes i and j are connected, and 0 if they are not. 邻接矩阵A是布尔值的nxn矩阵,因此,如果节点i和j连接,则A(i,j)= 1,否则,则为0。

In adjacency lists representation for each node you maintain a list of the nodes it is connected to (adjacent to). 在每个节点的邻接列表表示中,您将维护一个与其连接(相邻)的节点的列表。

The question now is what you want to do with the graph. 现在的问题是您要如何处理图形。 If it is something simple, it may make sense to roll your own. 如果这很简单,则可以自己滚动。 If not, you may want to poke around the web for a java library to handle graphs. 如果没有,您可能想在网络上戳一个Java库来处理图形。 JGraphT has been mentioned. 提到了JGraphT

If you want to use adjacency matrix, then you can easily represent it in Java as a 2D array of bool's or int's. 如果要使用邻接矩阵,则可以轻松地在Java中将其表示为bool或int的2D数组。 You would need to give each node an index. 您将需要给每个节点一个索引。 The easiest way to do that is to keep your Node objects in an array, always in the same order. 最简单的方法是将Node对象始终以相同的顺序放置在数组中。 So you would really have two data structures: an array of Nodes, which are objects that represent whatever your nodes actually are, and the adjacency matrix, which refers to the Nodes by their indices. 因此,您实际上将拥有两个数据结构:Nodes数组(它们是表示您的节点实际上是什么的对象)和邻接矩阵,该矩阵通过其索引来引用Nodes。

Once you populate the matrix, if you can easily find a node that is connected to most other nodes by adding up the values (0s and 1s) in all the columns (or rows), and finding the maximum. 填充矩阵后,如果可以通过将所有列(或行)中的值(0和1)相加并找到最大值,轻松找到与大多数其他节点连接的节点。 Hope this helps. 希望这可以帮助。

There are two standard models for storing graphs. 有两种用于存储图形的标准模型。 The one Tom describes is the Adjacency List . 汤姆描述的一个是邻接表 The other would be a stand-alone Adjacency Matrix . 另一个将是独立的邻接矩阵

If you care about efficiency, study the sparseness of your situation (the more edges, the more performance-friendly is the matrix version). 如果您关心效率,请研究情况的稀疏性(矩阵越多,边缘的性能越友好)。 If perf isn't an issue, use what you'd rather program with... 如果性能不是问题,请使用您喜欢的程序...

Create a Node class and give it an instance variable of type Node . 创建一个Node类,并为其提供Node类型的实例变量。 Initialize it to null - if said node is connected to another node, then this instance variable will refer to it; 将其初始化为null-如果所述节点连接到另一个节点,则此实例变量将引用它;否则,它将引用它。 otherwise, it will remain null. 否则,它将保持为空。

If the node may be connected to numerous nodes (which is quite common for graphs), then use an ArrayList to store all of the nodes to which said node is connected. 如果该节点可能连接到多个节点(这在图形中是很常见的),则使用ArrayList存储该节点连接到的所有节点。

Possibly a duplicate of ' Good Java graph algorithm library? 可能是' 良好的Java图形算法库的副本 '. '。 The short answer would be to look at JGraphT . 简短的答案是看JGraphT

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

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