I want to perform some graph clustering and since I am pretty much bound to Java, decided to give the java package Jung a try. As a simple graph I create two clusters of each 5vertices which are interconnected. I connect both clusters using one edge. I would expect after the graph clustering to retrieve 2 clusters of both size 5 but I get different results. This is the code:
import edu.uci.ics.jung.algorithms.cluster.VoltageClusterer;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseGraph;
import java.io.IOException;
import java.util.Collection;
import java.util.Set;
public class CreateGraph {
public static void main(String[] args) throws IOException {
// Graph<V, E> where V is the type of the vertices
// and E is the type of the edges
Graph<Integer, String> g = new SparseGraph<Integer, String>();
for (int i = 0; i < 5; i++) {
g.addVertex((Integer) i);
}
for (int i = 0; i < 5; i++) {
for (int ii = 0; ii < 5; ii++) {
if (i != ii) {
g.addEdge("EdgeA-" + i + ii, i, ii);
}
}
}
// cluster 2
for (int i = 5; i < 10; i++) {
g.addVertex((Integer) i);
}
for (int i = 5; i < 10; i++) {
for (int ii = 5; ii < 10; ii++) {
if (i != ii) {
g.addEdge("EdgeB-" + i + ii, i, ii);
}
}
}
System.out.println(g.toString());
g.addEdge("Edge-connector", 1, 5);
System.out.println("Creating voltageclusterer");
VoltageClusterer<Integer, String> vc = new VoltageClusterer<Integer, String>(g, 2);
Collection<Set<Integer>> clusters = vc.cluster(2);
for (Set<Integer> s : clusters) {
System.out.println("set is " + s.size());
for (Integer ss : s) {
System.out.println("Element " + ss);
}
}
}
}
and the output: +
set is 1
- Element 8
set is 9
- Element 0
- Element 1
- Element 2
- Element 3
- Element 4
- Element 5
- Element 6
- Element 7
- Element 9
Anyone any idea? (suggestions with regard to other approaches are also welcome, as long as they are in Java).