英文:
how to create a subgraph with jgrapht
问题
我在Java
中使用jgrapht
来实现基于网络的算法。我首先读取一个邻接表,然后创建基于jgrapht
的图。现在,给定一个名为subNodes
的节点子集,我想要生成一个子图。我尝试使用Subgraph
类,就像在这个链接中所示,但是我无法让它工作。
import org.jgrapht.*;
import org.jgrapht.graph.*;
......
HashMap<Integer, HashSet<Integer>> adjacencyList = new HashMap<Integer, HashSet<Integer>>();
// 填写邻接表
// 创建图
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>();
// 生成一个顶点的子集以构造子图
类似的帖子在这里,但也没有帮助。
英文:
I am using jgrapht
in Java
to work a network-based algorithm. I first read an adjacency list and then create jgrapht
based graph. Now, given a subset of nodes called subNodes
, I'd like to generate a sub graph. I am trying to use Subgraph
class as shown at this link, however, I could not have it worked.
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
A similar post is here, but that also did not help.
答案1
得分: 1
这似乎是在提到一些旧的 javadoc。不确定为什么你特别使用了 1.1.0 版本。以下是使用 jgrapht 1.5 版本的示例:
public class SubGraphExample {
public static void main(String[] args){
Graph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class);
Graphs.addAllVertices(g, Arrays.asList(1,2,3,4));
g.addEdge(1,2);
g.addEdge(2,3);
g.addEdge(3,4);
g.addEdge(4,1);
System.out.println("Graph: "+g);
//Subgraph
Set<Integer> vertexSubset = new HashSet<>(Arrays.asList(1,2));
Graph<Integer, DefaultEdge> subgraph = new AsSubgraph<>(g, vertexSubset);
System.out.println("Subgraph: "+subgraph);
}
}
输出结果:
Graph: ([1, 2, 3, 4], [{1,2}, {2,3}, {3,4}, {4,1}])
Subgraph: ([1, 2], [{1,2}])
你应该经常查看测试目录中包含的示例。AsSubgraph
类附带了 AsSubgraphTest
类,可以在 测试套件 中找到。
最新的 javadoc(在撰写时为 1.5.0 版本)可以在这里找到:https://jgrapht.org/javadoc/
英文:
It seems you are referring to some old javadoc. Not sure why you are specifically using the 1.1.0. Here's an example using the 1.5 version of jgrapht:
public class SubGraphExample {
public static void main(String[] args){
Graph<Integer, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class);
Graphs.addAllVertices(g, Arrays.asList(1,2,3,4));
g.addEdge(1,2);
g.addEdge(2,3);
g.addEdge(3,4);
g.addEdge(4,1);
System.out.println("Graph: "+g);
//Subgraph
Set<Integer> vertexSubset = new HashSet<>(Arrays.asList(1,2));
Graph<Integer, DefaultEdge> subgraph = new AsSubgraph<>(g, vertexSubset);
System.out.println("Subgraph: "+subgraph);
}
}
Output:
Graph: ([1, 2, 3, 4], [{1,2}, {2,3}, {3,4}, {4,1}])
Subgraph: ([1, 2], [{1,2}])
You should always have a look at the examples included in the test directories. The AsSubgraph
class comes with the AsSubgraphTest
class which can be found in the test suite.
The latest javadoc (1.5.0 at the time of writing) can be found here: https://jgrapht.org/javadoc/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论