コード例 #1
0
  public void parseInput(String file, int threads, int limit)
      throws FileNotFoundException, InterruptedException {
    // long startParseTime = System.currentTimeMillis();
    m_jgAdapter = new JGraphModelAdapter<Position, DefaultEdge>(graph);
    jgraph = new JGraph(m_jgAdapter);
    this.threads = threads;

    Scanner input = new Scanner(new File(file));
    try {
      for (int r = 0; input.hasNextLine() && r < limit; r++) {
        Scanner line = new Scanner(input.nextLine());
        try {

          ArrayList<Position> row = new ArrayList<Position>();
          grid.add(row);

          System.out.println("Row " + r);

          for (int c = 0; line.hasNextInt() && c < limit; c++) {
            Position position = new Position(r, c, line.nextInt());
            row.add(position);
            graph.addVertex(position);
            positionVertexAt(position, position.column * 5, position.row * 5);
          }
        } finally {
          line.close();
        }
      }
    } finally {
      input.close();
    }

    graphGrid(grid);

    // ArrayList<ArrayList<Position>> grid2 = transpose(grid);
    // outputGrid(grid2);

  }
コード例 #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);
          }
        }
      }
    }
  }