Example #1
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));
  }
Example #2
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));
    }
  }
 // 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);
   }
 }
 /**
  * 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");
 }
Example #5
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);
    }
  }
  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);
      }
    }
  }
Example #7
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++;
      }
    }
  }
Example #8
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)
  }
 /**
  * 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)));
   }
 }
Example #10
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)));
   }
 }
Example #11
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);
   }
 }
Example #12
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);
   }
 }
 private void unionBoth(int p, int q) {
   uf1.union(p, q);
   uf2.union(p, q);
 }