java - Simple undirected unlabelled graph in JGraphT doesn't work? What is the edgeClass parameter? -
i want make simple undirected unlabelled (edges aren't labelled) graph a<->b
in jgrapht 0.8.3:
import org.jgrapht.graph.defaultedge; import org.jgrapht.graph.simplegraph; class { public static void main(string[] args) { simplegraph<string, string> sg = new simplegraph<string, string>(string.class); sg.addvertex("a"); sg.addvertex("b"); sg.addedge("a", "b"); system.out.println("edges of a: " + sg.edgesof("a")); system.out.println("edges of b: " + sg.edgesof("b")); } }
i output:
$ java -cp 'jgrapht-jdk1.6.jar:.' edges of a: [] edges of b: []
why sets of edges of vertices a
, b
empty? class parameter of simplegraph
for? seems type of edges, since edges here unlabelled, surely doesn't matter? graph classes seem take class of edge (edgeclass
) parameter. can't find in documentation edgeclass
described.
i found if label edge (change addedge
line sg.addedge("a", "b", "an_edge");
) works... don't want label edges...
$ java -cp 'jgrapht-jdk1.6.jar:.' edges of a: [an_edge] edges of b: [an_edge]
you don't have label edges. problem in approach tell graph use string edge type. proper std. way of doing things be:
simplegraph<string, defaultedge> sg = new simplegraph<string, defaultedge>(defaultedge.class); sg.addvertex("a"); sg.addvertex("b"); sg.addvertex("c"); sg.addedge("a", "b"); sg.addedge("b", "c"); system.out.println("graph: " + sg.tostring()); system.out.println("edges of a: " + sg.edgesof("a")); system.out.println("edges of b: " + sg.edgesof("b"));
this give following output:
graph: ([a, b, c], [{a,b}, {b,c}]) edges of a: [(a : b)] edges of b: [(a : b), (b : c)]
so fix problem necessary switch edge class defaultedge shown in code.