使用JGraphT,能计算出两个Graph对象的交集(差分/增量)吗?
还有其他java图形库可以帮助我解决问题吗?
发布于 2015-08-31 18:48:14
在JUNG中,最简单的解决方法是构建顶点和边缘集的交集(或其他任何您喜欢的集合操作),然后根据结果构建另一个图:
(使用番石榴集类)
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);
}
}https://stackoverflow.com/questions/32307665
复制相似问题