Princeton Algorithm Part 1 Week 1 - Percolation & PercolationStats

這是 coursera Algorithm Part 1 第一周作業(yè), 得分98/100, 測試6錯誤,可惜沒找到原因。

This is the Programming Assignment 1 for Princeton Algorithm part1 course @ coursera.org. (algs4)

98/100, with unsolved error on test 6 (I got no idea what's that error mean)

Test 6 error: Open predetermined sites with long percolating path;

test6.png

Percolation Class:


package ProgrammingAssignment;

import edu.princeton.cs.algs4.WeightedQuickUnionUF;

public class Percolation {
    final private int n; // n-by-n grid;
    private boolean[] flag; // array of opened sites
    private int count; // number of opened site
    private WeightedQuickUnionUF uf; // Using WeightedQuickUnion data type
    private WeightedQuickUnionUF bw; //backwash 

    public Percolation(int n) {
    // create n-by-n grid, with all sites blocked
    if (n <= 0)
        throw new IllegalArgumentException(
            "Fail to create an n-by-n brid, " + "n should be a number larger than 0");
    this.n = n;
    uf = new WeightedQuickUnionUF(n * n);
    bw = new WeightedQuickUnionUF(n * n);
    flag = new boolean[n * n];
    for (int i = 0; i < n * n; i++) {
        flag[i] = false;
    }
    for (int i = 1; i < n; i++) {
        uf.union(0, i);
        bw.union(0, i);
    }
    for (int i = n*(n-1); i < n*n-1; i++) {
        uf.union(i, n*n-1);
    }
    }

    public void open(int row, int col) {
    // open site (row, col) if it is not open already
    if (!isOpen(row, col)) {
        int index = index(row, col);

        flag[index] = true;

        if (row - 1 > 0 && isOpen(row - 1, col)){
        uf.union(index, index(row - 1, col));
        bw.union(index, index(row - 1, col));
        }


        if (row + 1 <= n && isOpen(row + 1, col)){
        uf.union(index, index(row + 1, col));
        bw.union(index, index(row + 1, col));
        }

        if (col - 1 > 0 && isOpen(row, col - 1)){
        uf.union(index, index(row, col - 1));
        bw.union(index, index(row, col - 1));
        }

        if (col + 1 <= n && isOpen(row, col + 1)){
        uf.union(index, index(row, col + 1));
        bw.union(index, index(row, col + 1));
        }
        count++;
    }
    }

    public boolean isOpen(int row, int col) {
    // is site (row, col) open
    int index = index(row, col);
    return flag[index];
    }

    private int index(int row, int col) {
    // return the index number with knowed row and col
    if (row <= 0 || row > n)
        throw new IllegalArgumentException("rows index out of bounds");
    if (col <= 0 || col > n)
        throw new IllegalArgumentException("cols index out of bounds");
    return (row - 1) * n + col - 1;
    }

    public boolean isFull(int row, int col) {//using bw istead of uf
    // test if site (row, col) full
    int index = index(row, col);
    return bw.connected(0, index) && isOpen(row, col);
    }

    public boolean percolates() {
    // test if the system percolates
    return (uf.connected(0, n * n - 1) && count!=0);
    }

    public int numberOfOpenSites() {
    // number of open sites
    return count;
    }
}

PercolationStats Class:

import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;

public class PercolationStats {
    private double[] result; // result array
    private double mean; // mean
    private int trials; // number of trials
    private double stddev;
    private double confidenceLo;
    private double confidenceHi;

    public PercolationStats(int n, int trials) {
    // perform trials independent experiments on an n-by-n grid
    if (n < 1)
        throw new IllegalArgumentException(
            "Fail to create an n-by-n brid, " 
        + "n should be a number larger than 0");
    if (trials < 1)
        throw new IllegalArgumentException("Trails too small");
    result = new double[trials];
    this.trials = trials;
    for (int i = 0; i < trials; i++) {
        Percolation perco = new Percolation(n);
        while (!perco.percolates()) {
        perco.open(StdRandom.uniform(n)+1, StdRandom.uniform(n)+1);
        }
        result[i] = (1.0) * perco.numberOfOpenSites() / (n * n);
    }
    mean = StdStats.mean(result);
    stddev = StdStats.stddev(result);
    confidenceLo = mean - (1.96 * stddev() / Math.sqrt(trials));
    confidenceHi = mean + (1.96 * stddev() / Math.sqrt(trials));
    }

    public double mean() {
    // sample mean of percolation threshold
    return mean;
    }

    public double stddev() {
    // sample standard deviation of percolation threshold
    return stddev;
    }

    public double confidenceLo() {
    // low endpoint of 95% confidence interval
    return confidenceLo;
    }

    public double confidenceHi() {
    // high endpoint of 95% confidence interval
    return confidenceHi;
    }

    public static void main(String[] args) {
    // test client
    PercolationStats pstats = new PercolationStats(Integer.parseInt(args[0]), Integer.parseInt(args[1]));

    System.out.printf("mean                     = %f\n", pstats.mean());
    System.out.printf("stddev                   = %f\n", pstats.stddev());
    System.out.printf("95%s confidence interval  = [%f, %f]\n", "%", pstats.confidenceLo(), pstats.confidenceHi());
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 就在兩天前,我隨意的翻看這騰訊新聞,看到了一則悲慘的文章吧!有些感觸,可能大家也有吧。 一名重慶男子跳橋輕生,第一...
    暖遇閱讀 1,388評論 2 3
  • 我的國慶怎樣規(guī)劃? 華山歸來后讀書??蓭滋爝^去了,書還沒有 讀呢。 那么,明天就拿起來看吧。 這幾天都做啥了? 華...
    者行孫閱讀 384評論 0 1
  • 數(shù)據(jù)完整性是為了保證輸入到數(shù)據(jù)中的數(shù)據(jù)是正確的,它防止了用戶可能的輸入錯誤。(很重要) 數(shù)據(jù)完整性 實體(行)完整...
    很很狠的狠角色閱讀 278評論 5 0
  • 心中神佛不在留,萬里群山霧纏綿。只是再無人心路,獨坐深山空禪眠。 君正天下王道路,翻唱詩人空留痕。雪飛萬里蠶空盡,...
    富貴掌中留閱讀 417評論 5 2

友情鏈接更多精彩內(nèi)容