首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JGraphT:计算图的交点

JGraphT:计算图的交点
EN

Stack Overflow用户
提问于 2015-08-31 09:09:54
回答 1查看 581关注 0票数 2

使用JGraphT能计算出两个Graph对象的交集(差分/增量)吗?

还有其他java图形库可以帮助我解决问题吗?

EN

回答 1

Stack Overflow用户

发布于 2015-08-31 18:48:14

在JUNG中,最简单的解决方法是构建顶点和边缘集的交集(或其他任何您喜欢的集合操作),然后根据结果构建另一个图:

(使用番石榴集类)

代码语言:javascript
复制
Graph<V, E> newGraph = ...; // create the appropriate graph type
for (V v : Sets.intersection(g1.getVertices(), g2.getVertices()) {
  newGraph.addVertex(v);
}

for (E e : Sets.intersection(g1.getEdges(), g2.getEdges()) {
  // Assume that each each e connects the same set of vertices in both
  // g1 and g2.  Otherwise you need to check that as well.
  V v1 = g1.getEndpoints(e).getFirst();
  V v2 = g1.getEndpoints(e).getSecond();
  // This contains check is necessary because the default implementations
  // of addEdge() will add v1 and v2 if they are not already present in
  // the graph, which you don't want to do if they're not in the intersection.
  if (newGraph.containsVertex(v1) && newGraph.containsVertex(v2)) {
    newGraph.addEdge(e, v1, v2);
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32307665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档