问题描述可以详见:http://coursera.cs.princeton.edu/algs4/assignments/percolation.html

关于QuickFindUF的javadoc:http://algs4.cs.princeton.edu/15uf/QuickFindUF.java.html

关于WeightedQuickUnionUF的javadoc:http://algs4.cs.princeton.edu/15uf/WeightedQuickUnionUF.java.html

附言:(引用于http://blog.csdn.net/revilwang/article/details/10823467

关于这个模型,其实存在一个问题,在算法课程的论坛上,讨论的热度很高。问题是这样的:

由于引入虚拟的顶层区域和虚拟的底层区域,那么当模型渗透的时候,可能会出现下图的情况

如右边图所示,由于所有的底层区域都和虚拟底层区域相连,所以一旦当区域渗透,则和其他的底层开启区域相连的区域也显示为区域满状态。而实际的情况应该是按照左图所示。这个问题称为 backwash,个人把这个翻译成“回流”。引入虚拟底层区域,很难避免这个问题。讨论的结果,有两种方式可以改进:

1. 不使用虚拟底层区域,只保留顶层,判断是否渗透的时候用虚拟顶层和一个for循环来判断。

2. 保留虚拟底层区域,另外加一个不使用虚拟底层的模型,将两个模型结合在一起来判断是否渗透,通过浪费一些内存来保证效率。

backwash的情况导致Percolation.java在测试时public void isfull(int i, int j) 方法出现错误,这一点从上面两张图也可以明显的看出来。(而解决的办法通过上面两种方法实现)

下面两段代码是从http://www.cnblogs.com/tiny656/p/3820653.html 复制来的,因为自己写的没有考虑到backwash的情况,所以有一些错误,就不挂上来误人子弟了。

Percolation.java

public class Percolation {

    private boolean[] matrix;
private int row, col;
private WeightedQuickUnionUF wquUF;
private WeightedQuickUnionUF wquUFTop;
private boolean alreadyPercolates; public Percolation(int N) {
if (N < 1) throw new IllegalArgumentException("Illeagal Argument");
wquUF = new WeightedQuickUnionUF(N*N+2);
wquUFTop = new WeightedQuickUnionUF(N*N+1);
alreadyPercolates = false;
row = N;
col = N;
matrix = new boolean[N*N+1];
} private void validate(int i, int j) {
if (i < 1 || i > row)
throw new IndexOutOfBoundsException("row index i out of bounds");
if (j < 1 || j > col)
throw new IndexOutOfBoundsException("col index j out of bounds");
} public void open(int i, int j) {
validate(i, j);
int curIdx = (i-1)*col + j;
matrix[curIdx] = true;
if (i == 1) {
wquUF.union(curIdx, 0);
wquUFTop.union(curIdx, 0);
}
if (i == row) {
wquUF.union(curIdx, row*col+1);
} int[] dx = {1, -1, 0, 0};
int[] dy = {0, 0, 1, -1};
for (int dir = 0; dir < 4; dir++) {
int posX = i + dx[dir];
int posY = j + dy[dir];
if (posX <= row && posX >= 1
&& posY <= row && posY >= 1
&& isOpen(posX, posY)) {
wquUF.union(curIdx, (posX-1)*col+posY);
wquUFTop.union(curIdx, (posX-1)*col+posY);
}
}
} public boolean isOpen(int i, int j) {
validate(i, j);
return matrix[(i-1)*col + j];
} public boolean isFull(int i, int j) {
validate(i, j);
int curIdx = (i-1)*col+j;
if (wquUFTop.find(curIdx) == wquUFTop.find(0)) return true;
return false;
} public boolean percolates() {
if (alreadyPercolates) return true;
if (wquUF.find(0) == wquUF.find(row*col+1)) {
alreadyPercolates = true;
return true;
}
return false;
} public static void main(String[] args) {
Percolation perc = new Percolation(2);
perc.open(1, 1);
perc.open(1, 2);
perc.open(2, 1);
System.out.println(perc.percolates());
} }

PercolationStats.java

public class PercolationStats {
private double[] x;
private int expTime;
public PercolationStats(int N, int T) { // perform T independent experiments on an N-by-N grid if (N <= 0 || T <= 0)
throw new IllegalArgumentException("Illeagal Argument");
x = new double[T+1];
expTime = T;
for (int i = 1; i <= T; i++) {
Percolation perc = new Percolation(N);
while (true) {
int posX, posY;
do {
posX = StdRandom.uniform(N) + 1;
posY = StdRandom.uniform(N) + 1;
} while(perc.isOpen(posX, posY));
perc.open(posX, posY);
x[i] += 1;
if (perc.percolates())
break;
}
x[i] = x[i]/(double) (N * N);
}
}
public double mean() { // sample mean of percolation threshold double u = 0.0;
for (int i = 1; i <= expTime; i++) { u += x[i];
}
return u / (double)expTime;
}
public double stddev() { // sample standard deviation of percolation threshold double q = 0.0;
double u = mean();
for (int i = 1; i <= expTime; i++) { q += (x[i]-u)*(x[i]-u);
}
return Math.sqrt(q / (double)(expTime - 1));
}
public double confidenceLo() { // low endpoint of 95% confidence interval double mu = mean();
double sigma = stddev();
return mu - 1.96*sigma / Math.sqrt(expTime);
}
public double confidenceHi() { // high endpoint of 95% confidence interval double mu = mean();
double sigma = stddev();
return mu + 1.96*sigma / Math.sqrt(expTime);
} public static void main(String[] args) { // test client (described below) int N = Integer.parseInt(args[0]);
int T = Integer.parseInt(args[1]);
PercolationStats percStats = new PercolationStats(N, T);
StdOut.printf("mean = %f\n", percStats.mean());
StdOut.printf("stddev = %f\n", percStats.stddev());
StdOut.printf("95%% confidence interval = %f, %f\n",
percStats.confidenceLo(), percStats.confidenceHi()); }
}

Programming Assignment 1: Percolation的更多相关文章

  1. AlgorithmsI Programming Assignment 1: Percolation

    3种版本的答案,第一种使用virtual top and bottom site, 但有backwash的问题,解决这个问题有两种方法: 1. 使用2个WQUUF, 但会增加memory. One f ...

  2. Coursera Algorithms Programming Assignment 1: Percolation(100分)

    题目来源http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 作业分为两部分:建立模型和仿真实验. 最关键的部分就是建 ...

  3. 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 3.Programming Assignment : Planar data classification with a hidden layer

    Planar data classification with a hidden layer Welcome to the second programming exercise of the dee ...

  4. Algorithms: Design and Analysis, Part 1 - Programming Assignment #1

    自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming ass ...

  5. Algorithms : Programming Assignment 3: Pattern Recognition

    Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...

  6. Programming Assignment 2: Randomized Queues and Deques

    实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...

  7. 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 2、编程作业常见问题与答案(Programming Assignment FAQ)

    Please note that when you are working on the programming exercise you will find comments that say &q ...

  8. Programming Assignment 5: Kd-Trees

    用2d-tree数据结构实现在2维矩形区域内的高效的range search 和 nearest neighbor search.2d-tree有许多的应用,在天体分类.计算机动画.神经网络加速.数据 ...

  9. Programming Assignment 4: 8 Puzzle

    The Problem. 求解8数码问题.用最少的移动次数能使8数码还原. Best-first search.使用A*算法来解决,我们定义一个Seach Node,它是当前搜索局面的一种状态,记录了 ...

随机推荐

  1. 今天工作遇到要发短信(ios)的功能,于是随手记录了一下

    ios中发送短信有两种 1.程序外调用系统短信 2.程序内调用系统发短信 第一种比较简单,直接调用url就可以了 oc下的代码为 [[UIApplication sharedApplication] ...

  2. 3669 [Noi2014]魔法森林(LCT,最小生成树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3669 [题意] 给定一个无向图,求1-n的路径中最小的max{ai}+max{bi} ...

  3. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  4. web服务器分析与设计(五)--一些总结

    随着年龄与经验的增加,对于软件方面的分析与设计也会有一些新的认识.下面做个近期的总结: 1,关于到底用不用作设计的问题: 在最近两个公司,原有人马是不会作设计(我自己的感觉),也察觉不到作设计的任何冲 ...

  5. Java设计模式系列之动态代理模式(转载)

    代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示例代码 public interface Sub ...

  6. Java设计模式系列之中介者模式

    中介者模式(Mediator)的定义 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式(Mediator)的适 ...

  7. 问题-某个程序改了ICO图标后编译后还是显示老图标?

    问题现象:某个程序改了ICO图标后编译后还是显示老图标? 问题原原:可能是因为系统的缓存问题. 问题处理:把程序的EXE放在别的路径下打开就可以了. 问题相关人员:QQ253120114(朋友)  Q ...

  8. DelphiXE Android的所有权限按照分类总结说明

    相关资料:http://www.delphitop.com/html/Android/2778.html   网络相关的: android.permission.INTERNET 允许程序打开网络套接 ...

  9. [iOS 多线程 & 网络 - 2.7] - NSURLCache

    A.基本知识 1.为什么需要缓存?   有时候一个url会请求多次,得到的内容确实一样的   2.缓存的概念     3.缓存数据的过程     当服务器返回数据时,需要做以下步骤(1)使用服务器的数 ...

  10. [C语言 - 6] static & extern

    A. extern函数 一个c文件生成一个obj文件   外部函数:允许其他文件访问.调用的函数(默认函数为外部函数),不允许存在同名的外部函数   my.c //define a extern fu ...