[英]JGraphT: calculate Graph intersection
使用JGraphT
,是否可以计算两个Graph
对象的交点 (差/增量)?
还有其他Java图形库可以帮助我解决问题吗?
在JUNG中,解决此问题的最直接方法是构建顶点和边集的交集(或您喜欢的任何其他集操作),然后从结果中构建另一个图:
(使用Guava Sets类)
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.