From c3517ad61a0c7053cef79b0e90341be4acbb9330 Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Sun, 20 Jul 2025 17:40:41 -0500 Subject: [PATCH 1/6] hw2: Complete Percolation constructor --- hw2/src/Percolation.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hw2/src/Percolation.java b/hw2/src/Percolation.java index f4638c8..f48ee82 100644 --- a/hw2/src/Percolation.java +++ b/hw2/src/Percolation.java @@ -3,9 +3,14 @@ public class Percolation { // TODO: Add any necessary instance variables. + int N; + boolean[][] grid; + WeightedQuickUnionUF uf; public Percolation(int N) { - // TODO: Fill in this constructor. + this.N = N; + grid = new boolean[N][N]; + uf = new WeightedQuickUnionUF(N); } public void open(int row, int col) { @@ -13,8 +18,7 @@ public void open(int row, int col) { } public boolean isOpen(int row, int col) { - // TODO: Fill in this method. - return false; + return grid[row][col]; } public boolean isFull(int row, int col) { From 25de9a70f4226ba4aefd121b493177a3769a24d1 Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Sun, 20 Jul 2025 17:48:24 -0500 Subject: [PATCH 2/6] hw2: Complete numberOfOpenSites method --- hw2/src/Percolation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hw2/src/Percolation.java b/hw2/src/Percolation.java index f48ee82..4e8a277 100644 --- a/hw2/src/Percolation.java +++ b/hw2/src/Percolation.java @@ -5,6 +5,7 @@ public class Percolation { // TODO: Add any necessary instance variables. int N; boolean[][] grid; + int numberOfOpenSites = 0; WeightedQuickUnionUF uf; public Percolation(int N) { @@ -27,8 +28,7 @@ public boolean isFull(int row, int col) { } public int numberOfOpenSites() { - // TODO: Fill in this method. - return 0; + return numberOfOpenSites; } public boolean percolates() { From 25f5cf10f6d2f02dfcdfd56f1189ef6852394504 Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 21 Jul 2025 16:27:57 -0500 Subject: [PATCH 3/6] hw2: Implement validateIndices and rcToIndex methods --- hw2/src/Percolation.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/hw2/src/Percolation.java b/hw2/src/Percolation.java index 4e8a277..e7e8573 100644 --- a/hw2/src/Percolation.java +++ b/hw2/src/Percolation.java @@ -19,11 +19,14 @@ public void open(int row, int col) { } public boolean isOpen(int row, int col) { + validateIndices(row, col); return grid[row][col]; } public boolean isFull(int row, int col) { // TODO: Fill in this method. + validateIndices(row, col); + return false; } @@ -38,5 +41,13 @@ public boolean percolates() { // TODO: Add any useful helper methods (we highly recommend this!). // TODO: Remove all TODO comments before submitting. + private void validateIndices(int row, int col) { + if (row < 0 || row >= N || col < 0 || col >= N) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + } + private int rcToIndex(int row, int col) { + return row * N + col; + } } From 8a4490bc7313d7ae822b51b29fb2b7de692cbc16 Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 21 Jul 2025 16:30:50 -0500 Subject: [PATCH 4/6] hw2: Add IllegalArgumentException to constructor --- hw2/src/Percolation.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hw2/src/Percolation.java b/hw2/src/Percolation.java index e7e8573..54f82f8 100644 --- a/hw2/src/Percolation.java +++ b/hw2/src/Percolation.java @@ -2,13 +2,15 @@ public class Percolation { - // TODO: Add any necessary instance variables. int N; boolean[][] grid; int numberOfOpenSites = 0; WeightedQuickUnionUF uf; public Percolation(int N) { + if (N <= 0) { + throw new IllegalArgumentException("Class should be constructed with a value greater than 0"); + } this.N = N; grid = new boolean[N][N]; uf = new WeightedQuickUnionUF(N); From 46e5ab28f3f4093b7db711f3c8b942c407dfd18d Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 21 Jul 2025 16:31:13 -0500 Subject: [PATCH 5/6] hw2: Complete open method --- hw2/src/Percolation.java | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/hw2/src/Percolation.java b/hw2/src/Percolation.java index 54f82f8..550d51c 100644 --- a/hw2/src/Percolation.java +++ b/hw2/src/Percolation.java @@ -13,11 +13,31 @@ public Percolation(int N) { } this.N = N; grid = new boolean[N][N]; - uf = new WeightedQuickUnionUF(N); + uf = new WeightedQuickUnionUF(N * N); } public void open(int row, int col) { - // TODO: Fill in this method. + validateIndices(row, col); + grid[row][col] = true; + numberOfOpenSites++; + + int p = rcToIndex(row, col); + if (row > 0 && isOpen(row - 1, col)) { + int q = rcToIndex(row - 1, col); // top + uf.union(p, q); + } + if (row < N - 1 && isOpen(row + 1, col)) { + int q = rcToIndex(row + 1, col); // bottom + uf.union(p, q); + } + if (col > 0 && isOpen(row, col - 1)) { + int q = rcToIndex(row, col - 1); // left + uf.union(p, q); + } + if (col < N - 1 && isOpen(row, col + 1)) { + int q = rcToIndex(row, col + 1); // right + uf.union(p, q); + } } public boolean isOpen(int row, int col) { From 68e7d5e27eda787b65bffebec831bc0f89bc193b Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 21 Jul 2025 17:49:30 -0500 Subject: [PATCH 6/6] hw2: Complete isFull method accounting for backwash --- hw2/src/Percolation.java | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/hw2/src/Percolation.java b/hw2/src/Percolation.java index 550d51c..58796c4 100644 --- a/hw2/src/Percolation.java +++ b/hw2/src/Percolation.java @@ -6,6 +6,9 @@ public class Percolation { boolean[][] grid; int numberOfOpenSites = 0; WeightedQuickUnionUF uf; + WeightedQuickUnionUF ufFull; // Used to prevent backwash in isFull + int virtualTop; + int virtualBottom; public Percolation(int N) { if (N <= 0) { @@ -13,7 +16,10 @@ public Percolation(int N) { } this.N = N; grid = new boolean[N][N]; - uf = new WeightedQuickUnionUF(N * N); + uf = new WeightedQuickUnionUF(N * N + 2); + ufFull = new WeightedQuickUnionUF(N * N + 1); + virtualTop = N * N; + virtualBottom = N * N + 1; } public void open(int row, int col) { @@ -22,21 +28,32 @@ public void open(int row, int col) { numberOfOpenSites++; int p = rcToIndex(row, col); + + if (row == 0) { + uf.union(p, virtualTop); + ufFull.union(p, virtualTop); + } + if (row == N - 1) uf.union(p, virtualBottom); + if (row > 0 && isOpen(row - 1, col)) { int q = rcToIndex(row - 1, col); // top uf.union(p, q); + ufFull.union(p, q); } if (row < N - 1 && isOpen(row + 1, col)) { int q = rcToIndex(row + 1, col); // bottom uf.union(p, q); + ufFull.union(p, q); } if (col > 0 && isOpen(row, col - 1)) { int q = rcToIndex(row, col - 1); // left uf.union(p, q); + ufFull.union(p, q); } if (col < N - 1 && isOpen(row, col + 1)) { int q = rcToIndex(row, col + 1); // right uf.union(p, q); + ufFull.union(p, q); } } @@ -46,10 +63,9 @@ public boolean isOpen(int row, int col) { } public boolean isFull(int row, int col) { - // TODO: Fill in this method. validateIndices(row, col); - - return false; + int p = rcToIndex(row, col); + return ufFull.connected(virtualTop, p); } public int numberOfOpenSites() { @@ -57,12 +73,8 @@ public int numberOfOpenSites() { } public boolean percolates() { - // TODO: Fill in this method. - return false; + return uf.connected(virtualTop, virtualBottom); } - - // TODO: Add any useful helper methods (we highly recommend this!). - // TODO: Remove all TODO comments before submitting. private void validateIndices(int row, int col) { if (row < 0 || row >= N || col < 0 || col >= N) { throw new IndexOutOfBoundsException("Index out of bounds");