Esempio 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;
 }
Esempio n. 2
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);
 }
Esempio n. 3
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");
 }
Esempio n. 5
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++;
      }
    }
  }
Esempio n. 6
0
 // does the system percolate?
 public boolean percolates() {
   return gridPercolates.connected(topVirtual, bottomVirtual);
 }
Esempio n. 7
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));
 }
 public boolean percolates() { // does the system percolate?
   return perc.connected(N * N, N * N + 1);
 }
 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);
 }
Esempio n. 10
0
 /**
  * checks whether grid percolates
  *
  * @return true if percolates
  */
 public boolean percolates() {
   return model.connected(topVirtualElement, bottomVirtualElement);
 }
Esempio n. 11
0
 // does the system percolate?
 public boolean percolates() {
   return uf1.connected(num * num, num * num + 1);
 }
Esempio n. 12
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);
 }
Esempio n. 13
0
 public boolean percolates() {
   return uf.connected(top, bottom);
 }
Esempio n. 14
0
  public boolean isFull(int i, int j) {
    i = verifyIndex(i);
    j = verifyIndex(j);

    return uf.connected(top, nId(i, j));
  }
Esempio n. 15
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));
 }