如何在 JUNG 中合并两个图?

发布于 2024-12-01 04:24:58 字数 67 浏览 3 评论 0原文

我必须要合并的图,即创建一个由图的边和节点的并集组成的新图(不重复)。 JUNG 中有可用的实现吗?还是我自己可以实现?

I have to graphs which I want to unite, that is, create a new graph composed by the union of both graph's edges and nodes (without repetition). Is there an implementation for that avaliable in JUNG or do I have do so on my own?

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

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

发布评论

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

评论(1

卷耳 2024-12-08 04:24:58

JUNG 中没有实现这一点,但假设图、顶点和边具有相同类型,则大约有六行代码:

// given Graph g1, g2
Graph g = new [appropriate Graph implementation]
for (V v : Collections.union(g1.getVertices(), g2.getVertices())) {
  g.addVertex(v);
}
for (E e : g1.getEdges()) {
  g.addEdge(e, g1.getEndpoints(e));
}
for (E e : g2.getEdges()) {
  g.addEdge(e, g2.getEndpoints(e));
}

如果没有孤立的顶点(即,没有入射边); addEdge() 将添加任何事件顶点。

如果图形是有向的,您需要将上面的内容更改为

g.addEdge(e, g1.getSource(e), g1.getDest(e));

重复项将被默默忽略(如果您想知道添加是否有效果,请检查返回值)。

There isn't an implementation for that in JUNG, but it's about six lines of code assuming that the graphs, vertices, and edges are of the same types:

// given Graph g1, g2
Graph g = new [appropriate Graph implementation]
for (V v : Collections.union(g1.getVertices(), g2.getVertices())) {
  g.addVertex(v);
}
for (E e : g1.getEdges()) {
  g.addEdge(e, g1.getEndpoints(e));
}
for (E e : g2.getEdges()) {
  g.addEdge(e, g2.getEndpoints(e));
}

You can skip the vertex adding if there are no isolated vertices (i.e., vertices that have no incident edges); addEdge() will add any incident vertices.

If the graph is directed, you'll want to change the above to

g.addEdge(e, g1.getSource(e), g1.getDest(e));

Duplicates are silently ignored (if you want to know whether an add had an effect, check the return value).

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