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 testGreedy() {
   Graph<String, String> graph = LinkedUndirectedGraph.create();
   graph.createNode("A");
   graph.createNode("B");
   graph.createNode("C");
   graph.createNode("D");
   graph.connect("A", "--", "C");
   graph.connect("B", "--", "C");
   graph.connect("B", "--", "D");
   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("A");
   assertThat(coloring.getPartitionSuperNode("C")).isEqualTo("C");
 }
Example #4
0
  // Unit testing Graph API
  public static void main(String args[]) {
    Graph<Integer> graph = new Graph<Integer>();
    Scanner sc = new Scanner(System.in);
    ArrayList<Integer> vertex = new ArrayList<Integer>();
    // Vertices numbered 1..2..size
    for (int i = 0; i < 10; i++) vertex.add(i);

    graph.addAll(vertex);

    graph.connect(3, 4);
    graph.connect(3, 5);
    graph.connect(2, 3);
    graph.connect(1, 2);
    graph.connect(1, 4);
    graph.connect(5, 6);
    graph.connect(5, 9);
    graph.source = 1;
    if (graph.BFS(5)) {
      System.out.println("Element Found and It's Conected Components are");
      for (Integer v : graph.fetchBag(5)) System.out.print(v + "->");
      System.out.println();
    }
    for (Integer i : graph.fetchBag(1)) System.out.print(i + "->");
  }
 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");
   }
 }
Example #6
0
  public static void main(String args[]) throws Exception {
    StringTokenizer tokenizer = new StringTokenizer(in.readLine());
    int n = Integer.parseInt(tokenizer.nextToken()), m = Integer.parseInt(tokenizer.nextToken());

    Graph graph = new Graph(n, m);
    for (long i = 0; i < m; i++) {
      tokenizer = new StringTokenizer(in.readLine());
      int from = Integer.parseInt(tokenizer.nextToken()),
          to = Integer.parseInt(tokenizer.nextToken());

      graph.connect(from, to);
    }

    output(graph, n);
    out.println();
    out.flush();
  }
 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);
   }
 }