如何使用 jgrapht 创建子图。

huangapple go评论61阅读模式
英文:

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&lt;Integer, HashSet&lt;Integer&gt;&gt; adjacencyList = new HashMap&lt;Integer, HashSet&lt;Integer&gt;&gt;();
\\fill out adjacency list

\\create your graph
Graph&lt;Integer, DefaultEdge&gt; myGraph =  new SimpleGraph&lt;&gt;(DefaultEdge.class);
 
int numNodes = ...;
for(int i = 0 ; i &lt;numNodes; i++) 
  myGraph.addVertex(i);
	

for(int i = 0 ; i&lt; numNodes; i++) {	
	if(adjacencyList.get(i) != null) {
		for(Integer j : adjacencyList.get(i)) {
			myGraph.addEdge(i, j);
		}
	}
}
Set&lt;Integer&gt; subNodes = new HashSet&lt;Integer&gt;(); 
\\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&lt;Integer, DefaultEdge&gt; g = new SimpleGraph&lt;&gt;(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(&quot;Graph: &quot;+g);

        //Subgraph
        Set&lt;Integer&gt; vertexSubset = new HashSet&lt;&gt;(Arrays.asList(1,2));
        Graph&lt;Integer, DefaultEdge&gt; subgraph = new AsSubgraph&lt;&gt;(g, vertexSubset);
        System.out.println(&quot;Subgraph: &quot;+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/

huangapple
  • 本文由 发表于 2020年9月24日 01:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64032963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定