JUNG - 如何获取图中的确切顶点?

发布于 2024-08-16 00:38:36 字数 327 浏览 4 评论 0原文

我必须创建一个具有自定义节点类型以及节点和节点的图。连接是从txt文件中一一读取的。

文件格式是这样的:startNode attibutes endNode。

每次我读一行,我都会创建 2 个节点对象:startNode 和 startNode。结束节点。并在它们之间添加边。

但是,startNode 可能存在于多行中。

例如 V1 ... V2 ; V1 ... V3

因此,在添加边之前,我必须检查我的图是否包含该节点..并且我应该使用图中的顶点而不是新创建的节点..

jung 是否有任何内置方法来解决这个问题? 或者有什么建议吗?

I have to create a graph with its self-defined node type and the nodes & connections are read from a txt file one by one.

The file format is like this: startNode attibutes endNode.

Every time I read one line, I created 2 node objects: startNode & endNode. and add edge between them..

However, the startNode may exist in several lines..

e.g. V1 ... V2 ; V1 ... V3

Therefore, I have to check whether my graph has contained the node before I add edges..and I should use the vertex in graphs instead of the node newly created..

Does jung have any built-in method to solve this problem?
Or any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

氛圍 2024-08-23 00:38:36

简短的答案是:根据合同,JUNG 的图形实现会为您处理这个问题,只要您的自定义节点/边缘对象的 equals()hashCode() 做正确的事。

如果您尝试向图中添加顶点并且该顶点已存在于图中,则 addVertex() 方法将返回 false(表示“未完成任何操作”),就像类似的 add( Set 中的 ) 方法。

另请注意,addEdge() 方法将为您将连接的顶点添加到图中(如果它们尚不存在)。

The short answer is: by contract, JUNG's graph implementations take care of this for you, as long as your custom node/edge objects' implementations of equals() and hashCode() do the Right Thing.

If you try to add a vertex to a graph and it's already present in the graph, the addVertex() method will return false (meaning 'nothing done') just as with the analogous add() method in Set.

Also note that the addEdge() methods will add the connected vertices to the graph for you if they're not already present.

忱杏 2024-08-23 00:38:36

JUNG 认为顶点(和边)是不同的,只要它们引用了不同的对象。如果您创建两个具有相同属性的顶点对象,它们将被视为不同的顶点,您将能够将它们都插入到图中。 JUNG 没有可以重写的 equals 方法(以检查顶点对象的属性)来执行检查两个顶点对象是否相同。因此,您需要手动维护图中的顶点(和边)列表,以避免添加图中已有的顶点。但是,您可以使用 HashMap 轻松做到这一点(如果您的图表不是太大)。

JUNG considers the vertices (and edges) different as long as they are referenced to different objects. If you create two vertex objects with same properties, they will be considered as different vertices and you will be able to insert both of them into the graph. JUNG doesn't have an equals method that you can override (to check the vertex object's properties) to perform a check whether two vertex objects are the same or not. Therefore you need to manually maintain the list of vertices (and edges) in your graph to avoid adding a vertex you already have in your graph. However you can easily do that with a HashMap (if your graph is not too big).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文