【发布时间】:2020-09-23 17:05:58
【问题描述】:
我在Java 中使用jgrapht 来工作基于网络的算法。我首先阅读了一个邻接列表,然后创建了基于 jgrapht 的图。现在,给定一个名为subNodes 的节点子集,我想生成一个子图。我正在尝试使用Subgraph 类,如this link 所示,但是,我无法让它工作。
import org.jgrapht.*;
import org.jgrapht.graph.*;
......
HashMap<Integer, HashSet<Integer>> adjacencyList = new HashMap<Integer, HashSet<Integer>>();
\\fill out adjacency list
\\create your graph
Graph<Integer, DefaultEdge> myGraph = new SimpleGraph<>(DefaultEdge.class);
int numNodes = ...;
for(int i = 0 ; i <numNodes; i++)
myGraph.addVertex(i);
for(int i = 0 ; i< numNodes; i++) {
if(adjacencyList.get(i) != null) {
for(Integer j : adjacencyList.get(i)) {
myGraph.addEdge(i, j);
}
}
}
Set<Integer> subNodes = new HashSet<Integer>();
\\generate a sub set of vertices to have a subgprah
类似的帖子是here,但这也没有帮助。
【问题讨论】:
-
究竟是什么不起作用?