Пример #1
0
  /** . */
  public void testDirectedGraph() {
    ListenableDirectedGraph<String, DefaultEdge> g =
        new ListenableDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
    g.addVertex(V1);
    g.addVertex(V2);
    g.addVertex(V3);

    g.addEdge(V1, V2);

    ConnectivityInspector<String, DefaultEdge> inspector =
        new ConnectivityInspector<String, DefaultEdge>(g);
    g.addGraphListener(inspector);

    assertEquals(false, inspector.isGraphConnected());

    g.addEdge(V1, V3);

    assertEquals(true, inspector.isGraphConnected());
  }
Пример #2
0
  private void findEdges(int startRow, int endRow) {
    int gridSize = grid.size();
    int sizeOfArrays = grid.get(0).size();
    for (int i = startRow; i < endRow; i++) {

      for (int j = 0; j < sizeOfArrays; j++) {

        Position currentPosition = grid.get(i).get(j);
        int current = currentPosition.value;
        int min = current;
        Position minP = currentPosition;

        if (i - 1 >= 0) {
          Position north = grid.get(i - 1).get(j);
          if (north.value < min) {
            min = north.value;
            minP = north;
          }
        }

        if (i - 1 >= 0 && j - 1 >= 0) {
          Position northWest = grid.get(i - 1).get(j - 1);
          if (northWest.value < min) {
            min = northWest.value;
            minP = northWest;
          }
        }

        if (j - 1 >= 0) {
          Position west = grid.get(i).get(j - 1);
          if (west.value < min) {
            min = west.value;
            minP = west;
          }
        }
        if (i + 1 < gridSize && j - 1 >= 0) {
          Position southWest = grid.get(i + 1).get(j - 1);
          if (southWest.value < min) {
            min = southWest.value;
            minP = southWest;
          }
        }
        if (i + 1 < gridSize) {
          Position south = grid.get(i + 1).get(j);
          if (south.value < min) {
            min = south.value;
            minP = south;
          }
        }
        if (i + 1 < gridSize && j + 1 < sizeOfArrays) {
          Position southEast = grid.get(i + 1).get(j + 1);
          if (southEast.value < min) {
            min = southEast.value;
            minP = southEast;
          }
        }
        if (j + 1 < sizeOfArrays) {
          Position east = grid.get(i).get(j + 1);

          if (east.value < min) {
            min = east.value;
            minP = east;
          }
        }

        if (i - 1 >= 0 && j + 1 < sizeOfArrays) {
          Position northEast = grid.get(i - 1).get(j + 1);
          if (northEast.value < min) {
            min = northEast.value;
            minP = northEast;
          }
        }

        if (currentPosition.equals(minP)) {
        } else {
          synchronized (graph) {
            graph.addEdge(currentPosition, minP);
          }
        }
      }
    }
  }