From 1347b61e24c7f96c051d41874a7325a42941e62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 4 Mar 2013 11:29:32 +0100 Subject: Algorithms-I : 1-Percolation: add PercolationJZU.java --- .../Part-I/1-Percolation/PercolationJZU.java | 103 +++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Algorithms/Part-I/1-Percolation/PercolationJZU.java diff --git a/Algorithms/Part-I/1-Percolation/PercolationJZU.java b/Algorithms/Part-I/1-Percolation/PercolationJZU.java new file mode 100644 index 0000000..8112b9f --- /dev/null +++ b/Algorithms/Part-I/1-Percolation/PercolationJZU.java @@ -0,0 +1,103 @@ +/* vim: set expandtab tabstop=4 shiftwidth=4 : */ + +public class PercolationJZU +{ + private static final int closed = -1; + private int N; + // faste using struct { id, weight } ? + private int[] sites; + private int[] weights; + + public PercolationJZU(int N) + { + this.N = N; + int n = (N*N)+2; + sites = new int[n]; + weights = new int[n]; + sites[0] = 0; + weights[0] = N+1; + sites[n-1] = n-1; + weights[n-1] = N+1; + for(int x=1; x<=n-1; x++) + { + sites[x] = x; + weights[x] = closed; + } + } + + private int ijToX(int i, int j) + { + if(i<1 || j<1 || i>N || j>N) + throw new java.lang.IndexOutOfBoundsException("check your indexes"); + return ((j-1)*N)+i; + } + + private boolean isOpen(int x) + { + return (weights[x]!=closed); + } + + private int root(int x) + { + while (x != sites[x]) + { + sites[x] = sites[sites[x]]; + x = sites[x]; + } + return x; + } + + private void connect(int i, int j) + { + int ri = root(i); + int rj = root(j); + if (ri==rj) return; + if (weights[ri] < weights[rj]) + { + sites[ri] = rj; + weights[rj] += weights[ri]; + } + else + { + sites[rj] = ri; + weights[ri] += weights[rj]; + } + + } + + public void open(int i, int j) + { + int x = ijToX(i,j); + if (isOpen(x)) return; + weights[x] = 1; + if (i>1 && isOpen(x-1)) connect(x,x-1); + if (i