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.