Princeton Algorithm Part 1 Week 1 - Percolation & PercolationStats

这是 coursera Algorithm Part 1 第一周作业, 得分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());
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 就在两天前,我随意的翻看这腾讯新闻,看到了一则悲惨的文章吧!有些感触,可能大家也有吧。 一名重庆男子跳桥轻生,第一...
    暖遇阅读 1,387评论 2 3
  • 我的国庆怎样规划? 华山归来后读书。可几天过去了,书还没有 读呢。 那么,明天就拿起来看吧。 这几天都做啥了? 华...
    者行孙阅读 384评论 0 1
  • 数据完整性是为了保证输入到数据中的数据是正确的,它防止了用户可能的输入错误。(很重要) 数据完整性 实体(行)完整...
    很很狠的狠角色阅读 277评论 5 0
  • 心中神佛不在留,万里群山雾缠绵。只是再无人心路,独坐深山空禅眠。 君正天下王道路,翻唱诗人空留痕。雪飞万里蚕空尽,...
    富贵掌中留阅读 417评论 5 2

友情链接更多精彩内容