如何在JUNG中合并两张图? [英] How to unite two graphs in JUNG?

查看:129
本文介绍了如何在JUNG中合并两张图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个由图的边和节点的联合组成的新图(不重复),以此来合并我想要的图。在JUNG中是否有可用的实现?或者我自己有这样做吗?

解决方案

没有实现对于JUNG来说,它的约六行代码假设图形,顶点和边是相同的类型:

 
/ /给定图g1,g2
图g = new [适当的图实现]
for(V v:Collections.union(g1.getVertices(),g2.getVertices())){
g.addVertex(v); (e,g1.getEdges()){
g.addEdge(e,g1.getEndpoints(e));
}
(e,g2.getEdges()){
g.addEdge(e,g2.getEndpoints(e));
}





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

如果图形是定向的,您需要更改(e,g1.getSource(e),g1.getDest(e));

g.addEdge >



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

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?

解决方案

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).

这篇关于如何在JUNG中合并两张图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆