public void testDeterministic() { // A pentagon. Graph<String, String> graph = LinkedUndirectedGraph.create(); graph.createNode("A"); graph.createNode("B"); graph.createNode("C"); graph.createNode("D"); graph.createNode("E"); graph.connect("A", "-->", "B"); graph.connect("B", "-->", "C"); graph.connect("C", "-->", "D"); graph.connect("D", "-->", "E"); graph.connect("E", "-->", "A"); GraphColoring<String, String> coloring = new GreedyGraphColoring<>(graph, Ordering.<String>natural()); assertThat(coloring.color()).isEqualTo(3); validateColoring(graph); assertThat(coloring.getPartitionSuperNode("A")).isEqualTo("A"); assertThat(coloring.getPartitionSuperNode("C")).isEqualTo("A"); Comparator<String> biasD = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.replace('D', '@').compareTo(o2.replace('D', '@')); } }; coloring = new GreedyGraphColoring<>(graph, biasD); assertThat(coloring.color()).isEqualTo(3); validateColoring(graph); assertThat(coloring.getPartitionSuperNode("A")).isEqualTo("A"); assertThat("A".equals(coloring.getPartitionSuperNode("C"))).isFalse(); }
public void testTwoFullyConnected() { final int count = 100; // A graph with two disconnected disjunct cliques. Graph<String, String> graph = LinkedUndirectedGraph.create(); for (int i = 0; i < count; i++) { graph.createNode("Node Left " + i); graph.createNode("Node Right " + i); for (int j = 0; j < count; j++) { graph.createNode("Node Left " + j); graph.createNode("Node Right " + j); if (i != j) { graph.connect("Node Left " + i, null, "Node Left " + j); graph.connect("Node Right " + i, null, "Node Right " + j); } } } assertThat(new GreedyGraphColoring<>(graph).color()).isEqualTo(count); validateColoring(graph); // Connect the two cliques. for (int i = 0; i < count; i++) { graph.connect("Node Left " + i, null, "Node Right " + i); } // Think of two exactly same graph with the same coloring side by side. // If we circularly shift the colors of one of the graph by 1, we can // connect the isomorphic nodes and still have a valid coloring in the // resulting graph. assertThat(new GreedyGraphColoring<>(graph).color()).isEqualTo(count); validateColoring(graph); }
public void testTwoNodesConnected() { Graph<String, String> graph = LinkedUndirectedGraph.create(); graph.createNode("A"); graph.createNode("B"); graph.connect("A", "--", "B"); GraphColoring<String, String> coloring = new GreedyGraphColoring<>(graph); assertThat(coloring.color()).isEqualTo(2); validateColoring(graph); assertThat(coloring.getPartitionSuperNode("A")).isEqualTo("A"); assertThat(coloring.getPartitionSuperNode("B")).isEqualTo("B"); }
public void testNoEdge() { Graph<String, String> graph = LinkedUndirectedGraph.create(); for (int i = 0; i < 5; i++) { graph.createNode("Node " + i); // All node with same color. GraphColoring<String, String> coloring = new GreedyGraphColoring<>(graph); assertThat(coloring.color()).isEqualTo(1); validateColoring(graph); for (int j = 0; j < i; j++) { assertThat(coloring.getPartitionSuperNode("Node 0")).isEqualTo("Node 0"); } } }
public void testAllConnectedToOneNode() { final int count = 10; Graph<String, String> graph = LinkedUndirectedGraph.create(); graph.createNode("Center"); for (int i = 0; i < count; i++) { graph.createNode("Node " + i); graph.connect("Center", null, "Node " + i); } GraphColoring<String, String> coloring = new GreedyGraphColoring<>(graph); assertThat(coloring.color()).isEqualTo(2); validateColoring(graph); assertThat(coloring.getPartitionSuperNode("Center")).isEqualTo("Center"); for (int i = 0; i < count; i++) { assertThat(coloring.getPartitionSuperNode("Node " + i)).isEqualTo("Node 0"); } }
public void testFullyConnected() { final int count = 100; Graph<String, String> graph = LinkedUndirectedGraph.create(); for (int i = 0; i < count; i++) { graph.createNode("Node " + i); for (int j = 0; j < count; j++) { graph.createNode("Node " + j); if (i != j) { graph.connect("Node " + i, null, "Node " + j); } } } GraphColoring<String, String> coloring = new GreedyGraphColoring<>(graph); assertThat(coloring.color()).isEqualTo(count); validateColoring(graph); for (int i = 0; i < count; i++) { assertThat(coloring.getPartitionSuperNode("Node " + i)).isEqualTo("Node " + i); } }