Ejemplo n.º 1
0
 /**
  * checks whether element is full
  *
  * @param i - row
  * @param j - column
  * @return true is is full
  */
 public boolean isFull(final int i, final int j) {
   checkBoundary(i, j);
   if (isOpen(i, j)) {
     return model.connected(model.find(getAbsoluteCoordinate(i, j)), topVirtualElement);
   }
   return false;
 }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    // write your code here
    System.out.println("hello world!\n");
    WeightedQuickUnionUF qu = new WeightedQuickUnionUF(10);
    qu.union(0, 1);
    qu.union(1, 2);
    qu.union(5, 6);
    qu.union(7, 9);

    Assert.check(qu.connected(0, 2));
  }
 /**
  * Reads in a sequence of pairs of integers (between 0 and N-1) from standard input, where each
  * integer represents some object; if the objects are in different components, merge the two
  * components and print the pair to standard output.
  */
 public static void main(String[] args) {
   int N = StdIn.readInt();
   WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);
   while (!StdIn.isEmpty()) {
     int p = StdIn.readInt();
     int q = StdIn.readInt();
     if (uf.connected(p, q)) continue;
     uf.union(p, q);
     StdOut.println(p + " " + q);
   }
   StdOut.println(uf.count() + " components");
 }
Ejemplo n.º 4
0
 /** is site (row i, column j) full? */
 public boolean isFull(int i, int j) {
   if (i < 1 || i > this.size || j < 1 || j > this.size) {
     throw new java.lang.IndexOutOfBoundsException();
   }
   int p = xyTo1D(i, j);
   return wqu.connected(this.virtualTop, p);
 }
Ejemplo n.º 5
0
  private void connectNeighbour(int x, int y, int x1, int y1) {
    if (y1 == -1 || y1 == N) {
      return;
    }

    if (x1 == -1) {
      gridFull.union(convertCoordinates(x, y), topVirtual);
      gridPercolates.union(convertCoordinates(x, y), topVirtual);
      return;
    } else if (x1 == N) {
      gridPercolates.union(convertCoordinates(x, y), bottomVirtual);
      return;
    }

    if (isOpened(x1, y1)) {
      gridFull.union(convertCoordinates(x, y), convertCoordinates(x1, y1));
      gridPercolates.union(convertCoordinates(x, y), convertCoordinates(x1, y1));
    }
  }
Ejemplo n.º 6
0
 // open site (row i, column j) if it is not open already
 public void open(int i, int j) {
   check(i, j);
   if (!isOpen(i, j)) {
     op[num * (i - 1) + j - 1] = true;
     if (i == 1) {
       unionBoth(j - 1, num * num);
     }
     if (i == num) uf1.union(num * (i - 1) + j - 1, num * num + 1);
     conNeighbor(i, j);
   }
 }
Ejemplo n.º 7
0
  public void open(int i, int j) {
    i = verifyIndex(i);
    j = verifyIndex(j);

    sites[i][j] = true;
    int thisId = nId(i, j);

    if (i > 0 && sites[i - 1][j]) {
      uf.union(thisId, nId(i - 1, j));
    }

    if (i < dim - 1 && sites[i + 1][j]) {
      uf.union(thisId, nId(i + 1, j));
    }

    if (j > 0 && sites[i][j - 1]) {
      uf.union(thisId, nId(i, j - 1));
    }

    if (j < dim - 1 && sites[i][j + 1]) {
      uf.union(thisId, nId(i, j + 1));
    }

    if (i == 0) {
      uf.union(thisId, top);
    }

    if (i == dim - 1) {
      uf.union(thisId, bottom);
    }
  }
Ejemplo n.º 8
0
  public static void main(String[] args) {
    WeightedQuickUnionUF uf = new WeightedQuickUnionUF(10);
    MinPQ<String> minPQ = new MinPQ<>(17, new Prob1().new myComparator());
    String prob =
        "F-A    16    G-A    13    B-A     3     C-B     6    H-B     5    G-B     2    C-D    12    C-I     9    C-H     1    D-I    14    J-D     8    D-E     7    J-E    17    F-G    15    G-H     4    I-H    11    I-J    10";
    String[] temp = prob.trim().split("\\s+");
    for (int i = 0; i < temp.length; i += 2) {
      String record = temp[i] + "," + temp[i + 1];
      minPQ.insert(record);
    }

    int sizeOfMST = 0;
    while (!minPQ.isEmpty() && sizeOfMST <= 9) {
      String minEdgeStr = minPQ.delMin();
      String[] vertexStr = minEdgeStr.split(",")[0].split("-");
      int v = vertexStr[0].charAt(0) - 'A';
      int w = vertexStr[1].charAt(0) - 'A';
      if (!uf.connected(v, w)) {
        uf.union(v, w);
        System.out.println(minEdgeStr);
        sizeOfMST++;
      }
    }
  }
Ejemplo n.º 9
0
  public void open(int i, int j) { // open site (row i, column j) if it is not open already
    // check index validity
    if (i < 1 || i > N || j < 1 || j > N) {
      throw new java.lang.IndexOutOfBoundsException();
    }
    // convert to actual index
    int ni = i - 1, nj = j - 1;
    int site_index = ni * N + nj;
    // if not open yet
    if (!grid[site_index]) {
      grid[site_index] = true;

      // check surrounding sites
      int surr_i, surr_j;
      for (int k = 0; k < 4; k++) {
        surr_i = ni + surrX[k];
        surr_j = nj + surrY[k];
        // if surrounding site exist and is open already
        // connect to the open part
        int surr_site = surr_i * N + surr_j;
        if (surr_i >= 0 && surr_i < N && surr_j >= 0 && surr_j < N && grid[surr_site]) {
          perc.union(site_index, surr_site);
          percExtra.union(site_index, surr_site);
        }
      }

      // check top and bottom virtual roots
      if (i == 1) { // connect the sites in the 1st row with the top
        perc.union(site_index, N * N);
        percExtra.union(site_index, N * N);
      }
      if (i == N) { // connect the sites in the last row with the bottom
        perc.union(site_index, N * N + 1);
      }
    }
  }
Ejemplo n.º 10
0
  /** open site (row i, column j) if it is not open already */
  public void open(int i, int j) {
    if (i < 1 || i > this.size || j < 1 || j > this.size) {
      throw new java.lang.IndexOutOfBoundsException();
    }
    if (this.openMatrix[i][j]) {
      return;
    }

    this.openMatrix[i][j] = true;
    int p = xyTo1D(i, j);
    if (i == 1) {
      checkBottom(p, this.virtualTop);
    }
    if (i == this.size) {
      int root = wqu.find(p);
      this.connectedToBottom[root] = true;
    }
    if (i - 1 >= 1) {
      if (isOpen(i - 1, j)) {
        int q = xyTo1D(i - 1, j);
        //                this.wqu.union(p, q);
        checkBottom(p, q);
      }
    }
    if (i + 1 <= this.size) {
      if (isOpen(i + 1, j)) {
        int q = xyTo1D(i + 1, j);
        checkBottom(p, q);
      }
    }
    if (j - 1 >= 1) {
      if (isOpen(i, j - 1)) {
        int q = xyTo1D(i, j - 1);
        checkBottom(p, q);
      }
    }
    if (j + 1 <= this.size) {
      if (isOpen(i, j + 1)) {
        int q = xyTo1D(i, j + 1);
        checkBottom(p, q);
      }
    }
  }
Ejemplo n.º 11
0
  // open site (row i, column j) if it is not open already
  public void open(int i, int j) {
    checkBounds(i, j);

    grid[i - 1][j - 1] = 1; // mark node as open

    if (i == 1) wquf.union(xyTo1D(i, j), top); // connect to virtual top

    if (i == size) wquf.union(xyTo1D(i, j), bottom); // connect to virtual bottom

    if ((i > 1) && isOpen(i - 1, j)) // connect to open (row-1, col)
    wquf.union(xyTo1D(i - 1, j), xyTo1D(i, j));

    if ((i < size) && isOpen(i + 1, j))
      wquf.union(xyTo1D(i + 1, j), xyTo1D(i, j)); // connect to open (row+1, col)

    if ((j > 1) && isOpen(i, j - 1))
      wquf.union(xyTo1D(i, j - 1), xyTo1D(i, j)); // connect to open (row, col-1)

    if ((j < size) && isOpen(i, j + 1))
      wquf.union(xyTo1D(i, j + 1), xyTo1D(i, j)); // connect to open (row, col+1)
  }
Ejemplo n.º 12
0
 // does the system percolate?
 public boolean percolates() {
   return gridPercolates.connected(topVirtual, bottomVirtual);
 }
Ejemplo n.º 13
0
 // is site (row i, column j) full?
 public boolean isFull(int i, int j) {
   checkBounds(i, j);
   return gridFull.connected(topVirtual, convertCoordinates(i - 1, j - 1));
 }
Ejemplo n.º 14
0
 public boolean percolates() { // does the system percolate?
   return perc.connected(N * N, N * N + 1);
 }
Ejemplo n.º 15
0
 public boolean isFull(int i, int j) { // is site (row i, column j) full?
   return isOpen(i, j) && percExtra.connected((i - 1) * N + (j - 1), N * N);
 }
Ejemplo n.º 16
0
 private void unionBoth(int p, int q) {
   uf1.union(p, q);
   uf2.union(p, q);
 }
Ejemplo n.º 17
0
 /**
  * checks whether bottom neighbour is existed and connects it to the current element
  *
  * @param i - row
  * @param j - column
  */
 private void connectBottomNeighbour(final int i, final int j) {
   int current = getAbsoluteCoordinate(i, j);
   if (i < end - 1 && isOpen(i + 1, j)) {
     model.union(current, model.find(checkNeighbour(i + 1, j)));
   }
 }
Ejemplo n.º 18
0
 /**
  * checks whether top neighbour is existed and connects it to the current element
  *
  * @param i - row
  * @param j - column
  */
 private void connectTopNeighbour(final int i, final int j) {
   int current = getAbsoluteCoordinate(i, j);
   if (i > begin && isOpen(i - 1, j)) {
     model.union(current, model.find(checkNeighbour(i - 1, j)));
   }
 }
Ejemplo n.º 19
0
 /**
  * connects current element to the virtual bottom element
  *
  * @param i - row
  * @param j - column
  */
 private void connectVirtualBottom(final int i, final int j) {
   int current = getAbsoluteCoordinate(i, j);
   if (i == end - 1) {
     model.union(current, bottomVirtualElement);
   }
 }
Ejemplo n.º 20
0
 // does the system percolate?
 public boolean percolates() {
   return uf1.connected(num * num, num * num + 1);
 }
Ejemplo n.º 21
0
 // is site (row i, column j) full?
 public boolean isFull(int i, int j) {
   check(i, j);
   return uf2.connected(num * (i - 1) + j - 1, num * num);
 }
Ejemplo n.º 22
0
  public boolean isFull(int i, int j) {
    i = verifyIndex(i);
    j = verifyIndex(j);

    return uf.connected(top, nId(i, j));
  }
Ejemplo n.º 23
0
 // is site (row i, column j) full?
 public boolean isFull(int i, int j) {
   checkBounds(i, j);
   return wquf.connected(top, xyTo1D(i, j));
 }
Ejemplo n.º 24
0
 /**
  * checks whether grid percolates
  *
  * @return true if percolates
  */
 public boolean percolates() {
   return model.connected(topVirtualElement, bottomVirtualElement);
 }
Ejemplo n.º 25
0
 /**
  * connects current element to the virtual top element
  *
  * @param i - row
  * @param j - column
  */
 private void connectVirtualTop(final int i, final int j) {
   int current = getAbsoluteCoordinate(i, j);
   if (i == begin) {
     model.union(current, topVirtualElement);
   }
 }
Ejemplo n.º 26
0
 public boolean percolates() {
   return uf.connected(top, bottom);
 }