题注

这本来是我新浪博客中的一个文章,不过感觉技术博客还是在CSDN中发表比较合适,以后CSDN会是自己的领地了!把以前的一些有用的,阅读量比较多的博客搬过来,造福更多人吧~

转眼又到寒假了,话说我这简单的技术小博客一直没有更新呢~ 寒假有充足的时间给自己充电,整个寒假净忙着弄英语、看论文、写ReportReview,当然还有我自己最喜欢的:上Coursera的课程了~这个寒假总共注册了三门课程:Stanford《Introduction toDatabase》;Princeton《Algorithms partI》;以及一门和以后研究方向有关的《Health info inCloud》。对于Introduction toDatabase这门课来说,Stanford的作业量那真是超大…都贴常来我博客就爆了…不过我还是争取在闲余时间把那门课的InteractiveExercise的答案贴出来,供大家参考。Princeton的算法课程完全用Java写,这是我的最爱嘛~贴上来代码和大家分享一下,我觉得对于我和对于有兴趣阅读这一系列博客的人来说都会有帮助。

对于《Algorithms》这门课,如果想做一做这里面的Assignment,首先必须要到他的官方网站下面下载一些jar文件,链接如下:http://algs4.cs.princeton.edu/home/。应用它提供的很多库函数以及实例代码,可以帮助我们简化编程题目的难度。

题目

Programming Assignment 1: Percolation

Write a program to estimate the value of the percolationthreshold via Monte Carlo simulation.

Percolation. Given a composite systems comprised ofrandomly distributed insulating and metallic materials: whatfraction of the materials need to be metallic so that the compositesystem is an electrical conductor? Given a porous landscape withwater on the surface (or oil below), under what conditions will thewater be able to drain through to the bottom (or the oil to gushthrough to the surface)? Scientists have defined an abstractprocess known aspercolation to model such situations.

The model. We model a percolation system using anN-by-N grid ofsites. Each site iseitheropen or blocked. A full site isan open site that can be connected to an open site in the top rowvia a chain of neighboring (left, right, up, down) open sites. Wesay the systempercolates if there is a full site in thebottom row. In other words, a system percolates if we fill all opensites connected to the top row and that process fills some opensite on the bottom row. (For the insulating/metallic materialsexample, the open sites correspond to metallic materials, so that asystem that percolates has a metallic path from top to bottom, withfull sites conducting. For the porous substance example, the opensites correspond to empty space through which water might flow, sothat a system that percolates lets water fill open sites, flowingfrom top to bottom.)

The problem. In a famous scientific problem, researchersare interested in the following question: if sites areindependently set to be open with probabilityp (andtherefore blocked with probability 1 −p), what is theprobability that the system percolates? Whenp equals 0,the system does not percolate; whenp equals 1, the systempercolates. The plots below show the site vacancy probabilityp versus the percolation probability for 20-by-20 randomgrid (left) and 100-by-100 random grid (right).

            

When N is sufficiently large, there is athreshold value p* such that when p <p* a random N-by-N grid almost neverpercolates, and whenp >p*, a randomN-by-N grid almost always percolates. Nomathematical solution for determining the percolation thresholdp* has yet been derived. Your task is to write a computerprogram to estimatep*.

Percolation data type. To model a percolation system,create a data typePercolation with the following API:

public class Percolation {   public Percolation(int N)              // create N-by-N grid, with all sites blocked   public void open(int i, int j)         // open site (row i, column j) if it is not already   public boolean isOpen(int i, int j)    // is site (row i, column j) open?   public boolean isFull(int i, int j)    // is site (row i, column j) full?   public boolean percolates()            // does the system percolate?}

By convention, the indices i and j are integersbetween 1 and N, where (1, 1) is the upper-left site:Throw a java.lang.IndexOutOfBoundsException if eitheri orj is outside this range. The constructorshould take time proportional to N^2; all methods should takeconstant time plus a constant number of calls to the union-findmethodsunion(), find(), connected(),andcount().

Monte Carlo simulation. To estimate the percolationthreshold, consider the following computational experiment:

  • Initialize all sites to be blocked.
  • Repeat the following until the system percolates:
    • Choose a site (row i, column j) uniformly atrandom among all blocked sites.
    • Open the site (row i, column j).
  • The fraction of sites that are opened when the systempercolates provides an estimate of the percolation threshold.

For example, if sites are opened in a 20-by-20 lattice accordingto the snapshots below, then our estimate of the percolationthreshold is 204/400 = 0.51 because the system percolates when the204th site is opened.

     
50 open sites

100 open sites

150 open sites

204 open sites

By repeating this computation experiment T times andaveraging the results, we obtain a more accurate estimate of thepercolation threshold. Letxt be the fractionof open sites in computational experimentt. The samplemean μ provides an estimate of the percolation threshold; thesample standard deviation σ measures the sharpness of thethreshold.

Assuming T is sufficiently large (say, at least 30), thefollowing provides a 95% confidence interval for the percolationthreshold:

To perform a series of computational experiments, create a datatype PercolationStats with the following API.

public class PercolationStats {   public PercolationStats(int N, int T)    // perform T independent computational experiments on an N-by-N grid   public double mean()                     // sample mean of percolation threshold   public double stddev()                   // sample standard deviation of percolation threshold   public double confidenceLo()             // returns lower bound of the 95% confidence interval   public double confidenceHi()             // returns upper bound of the 95% confidence interval   public static void main(String[] args)   // test client, described below}

The constructor should throw ajava.lang.IllegalArgumentException if eitherN ≤0 orT ≤ 0.

Also, include a main() method that takes twocommand-line arguments N andT, performsT independent computational experiments (discussed above)on anN-by-N grid, and prints out the mean,standard deviation, and the95% confidence interval forthe percolation threshold. Use standard random from ourstandard libraries to generate random numbers; usestandardstatistics to compute the sample mean and standarddeviation.

% java PercolationStats 200 100mean                    = 0.5929934999999997stddev                  = 0.0087699042155256795% confidence interval = 0.5912745987737567, 0.5947124012262428% java PercolationStats 200 100mean                    = 0.592877stddev                  = 0.00999052371707379995% confidence interval = 0.5909188573514536, 0.5948351426485464% java PercolationStats 2 10000mean                    = 0.666925stddev                  = 0.1177653652103355895% confidence interval = 0.6646167988418774, 0.6692332011581226% java PercolationStats 2 100000mean                    = 0.6669475stddev                  = 0.11775205263262094
95% confidence interval = 0.666217665216461, 0.6676773347835391

分析

这道题看起来难度不大,但是实际上做的时候还是需要想一想的。首先的问题就是,这是一个蓄水问题,而不是图连接问题,如何将蓄水问题转换为图连接问题呢?我的方法是这样的:
(1)Open一个Site
(2)分别检查Open的这个Site的上下左右是不是也有Open的Site
        如果有,那么将这两个Open的Site链接起来
   这样一来,题目就简单多了。

第二个问题是,如何用提供的API,模拟一个N*N的蓄水池呢?方法肯定是坐标转换,我的方法是将每个i,j的Site表示为一维数组中的i*N+j,那么这个N*N的蓄水池起始点为1*N+1,终止点为N*N+N。先把0*N+1到0*N+N以及(N+1)*N+1到(N*1)*N+N链接起来,这样的好处就是:我们检查是否percolates的条件就是看0*N+1,(N+1)*N+1这两个点是否Connected就行了。如果不这么做,我们需要循环检查所有0*N+1到0*N+N以及(N+1)*N+1到(N*1)*N+N是否Connected。

第三个问题,也是我一直纠结的问题,就是所谓Backwash问题。什么叫做Backwash问题呢?下面一个图可以很好地解释这个情况:

也就是说,当我们的蓄水池已经percolate了以后,那么所有和最底边联通的Site也会和有水的地方connected。在课程上面,还没有一种不增加存储复杂度的前提下,在上面的模型中解决这一问题,唯一的解决方法是由Callum Noble提出的,原文如下:
A simple solution would be to maintain 2 data structures - onewhich has a hidden top and bottom and one which has a hidden toponly. In the interest of promoting discussion I'll not say exactlywhat these could be would be used for and how this solves thebackwash problem.

OK,这就是所有的问题了。

代码

1. Percolation.java

public class Percolation {private WeightedQuickUnionUF uf;private WeightedQuickUnionUF uf_backwash;private int N;private boolean[] arrayOpen;// create N-by-N grid, with all sites blockedpublic Percolation(int N){this.N = N;uf = new WeightedQuickUnionUF((N+1)*(N)+N+1);uf_backwash = new WeightedQuickUnionUF(N*N+N+1);arrayOpen = new boolean[(N+1)*(N)+N+1];for (int i=1; i<=N; i++){uf.union(0*N+1, 0*N+i);uf_backwash.union(0*N+1, 0*N+i);arrayOpen[0*N+i] = true;uf.union((N+1)*N+1, (N+1)*N+i);arrayOpen[(N+1)*N+i] = true;}}// open site (row i, column j) if it is not alreadypublic void open(int i, int j){if (i < 1 || i > N){throw new IndexOutOfBoundsException("row index " + i + " out of bounds");}if (j < 1 || j > N){throw new IndexOutOfBoundsException("row index " + j + " out of bounds");}if (arrayOpen[i*N+j]){return;}arrayOpen[i*N+j] = true;if (arrayOpen[(i-1)*N+j]){uf.union(i*N+j, (i-1)*N+j);uf_backwash.union(i*N+j, (i-1)*N+j);}if (arrayOpen[(i+1)*N+j]){uf.union(i*N+j, (i+1)*N+j);if (i!=N){uf_backwash.union(i*N+j, (i+1)*N+j);}}if (j!=1 && arrayOpen[i*N+j-1]){uf.union(i*N+j, i*N+j-1);uf_backwash.union(i*N+j, i*N+j-1);}if (j!=N && arrayOpen[i*N+j+1]){uf.union(i*N+j, i*N+j+1);uf_backwash.union(i*N+j, i*N+j+1);}}// is site (row i, column j) open?public boolean isOpen(int i, int j){if (i <1 || i > N){throw new IndexOutOfBoundsException("row index " + i + " out of bounds");}if (j < 1 || j > N){throw new IndexOutOfBoundsException("row index " + j + " out of bounds");}return arrayOpen[i*N+j];}// is site (row i, column j) full?public boolean isFull(int i, int j){if (i <1 || i > N){throw new IndexOutOfBoundsException("row index " + i + " out of bounds");}if (j < 1 || j > N){throw new IndexOutOfBoundsException("row index " + j + " out of bounds");}return uf_backwash.connected(i*N+j, 0*N+1) && arrayOpen[i*N+j];}// does the system percolate?public boolean percolates(){return uf.connected(0*N+1, (N+1)*N+1);}
}

2. PercolationStats.java

public class PercolationStats {private double recT[];private double res_mean;private double res_stddev;private int N;// perform T independent computational experiments on an N-by-N gridpublic PercolationStats(int N, int T){recT = new double[T];this.N = N;int times = 0;if (N <= 0){throw new IllegalArgumentException();}if (T <= 0){throw new IllegalArgumentException();}while (times < T){Percolation percolation = new Percolation(N);boolean[][] arrOpen = new boolean[N+1][N+1];int count = 0;while(true){count++;while(true){int x = StdRandom.uniform(N) + 1;int y = StdRandom.uniform(N) + 1;if (arrOpen[x][y]){continue;}else{percolation.open(x, y);arrOpen[x][y] = true;break;}}if (percolation.percolates()){recT[times] = (double)count / ((double)N * (double)N);break;}}times++;}this.res_mean = StdStats.mean(recT);this.res_stddev = StdStats.stddev(recT);}// sample mean of percolation thresholdpublic double mean(){return this.res_mean;}// sample standard deviation of percolation thresholdpublic double stddev(){return this.res_stddev;}// returns lower bound of the 95% confidence intervalpublic double confidenceLo(){return this.res_mean - 1.96*this.res_stddev / Math.sqrt(N);}// returns upper bound of the 95% confidence intervalpublic double confidenceHi(){return this.res_mean + 1.96*this.res_stddev / Math.sqrt(N);}// test client, described belowpublic static void main(String[] args){int N = StdIn.readInt();int T = StdIn.readInt();PercolationStats percolationStats = new PercolationStats(N, T);StdOut.println("mean = " + percolationStats.mean());StdOut.println("stddev = " + percolationStats.stddev());StdOut.println("95% confidence interval " + percolationStats.confidenceLo() + ", " + percolationStats.confidenceHi());}
}

Week1 Assignment - Princeton-Algorithms-PartI相关推荐

  1. Princeton Algorithms, Boggle

    Princeton Algorithm Assignment Boggle 普林斯顿大学算法课 Boggle 实现一个 Boggle 游戏,由自己决定采用什么数据结构和搜索方法. 基本就是字典树(Tr ...

  2. Week 5.1 | 左倾红黑树LLRB | Princeton Algorithms

    文章目录 一.2-3 search trees 1. 定义 2. 操作 3. 复杂度 二.左倾红黑树LLRB 1. 定义 2. 实现 (1)查找 (2)红黑树节点表示 (3)左旋 (4)右旋 (5)翻 ...

  3. Week 3.2 | Quik sort | Princeton Algorithms

    文章目录 一.双路快排 1. idea 2. 实现 3. 复杂度分析 4. 一些改进方法 二.双路快排的应用:找到第k小的元素(selection) 三.三路快排 1. idea 2. 实现 3. 复 ...

  4. 软件技术实习_当您还在学校时,如何获得一流的技术实习机会和技术工作

    软件技术实习 by Michael Deng 邓小平 当您还在学校时,如何获得一流的技术实习机会和技术工作 (How to land a top-notch tech internship - and ...

  5. 香港大学的计算机专业,香港大学计算机专业研究方向介绍(英文)

    香港大学计算机专业研究方向介绍!下面毕达教育就从香港大学官网www.cs.hku.hk整理了香港大学计算机专业研究方向,希望能对申请香港大学计算机专业的同学有所参考和帮助!!! Research Ar ...

  6. 八数码问题-8puzzle

    完整代码及测试数据 完整代码及测试数据 问题简介 八数码:是指在3x3的矩阵中,其中有8个格子放置成1-8,剩下一个格子是空格.能够移动和空格相邻的格子到空格,直到这个矩阵满足每一行依次从左到右读取是 ...

  7. java资源吧_哔哩哔哩有好的Java学习资源吗?

    你提到了 Java,我就说两个我学过的.并非常推荐的 Java 资源吧. 这两个资源在 Bilibili 都有视频,但是编程并不是你看过视频就能掌握的,还是需要自己写代码,所以推荐去课程平台上注册这两 ...

  8. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  9. Algorithm I assignment Collinear

    这本来应该是第三周的作业,但是由于其他作业逼近deadline,暂时推后了一周完成. 这周的assignment大大提高了我对这门课的看法,不得不说,Algorithms这门课的assignment部 ...

最新文章

  1. 编程能力差!90%输在这点上!谷歌AI专家:其实都是瞎努力!
  2. 工程项目如何实现采购效益最大化?
  3. 互联网协议 — TCP — 流量控制(网络质量保障)
  4. 第七天2017/04/14(引用与const、C++基本知识)
  5. Jz2440 环境安装
  6. js 位运算符 ~, ,| ,^
  7. 网络编程之 传输层的协议TCP与UDP
  8. 常见位操作及运算应用举例:1,C语言中位运算符异或“∧”的作用​​​​​​​2,异或运算的作用3,(与运算)、|(或运算)、^(异或运算)
  9. 开源图数据库项目 DGraph 的前世今生 | 技术头条
  10. SilverLight学习笔记--Silverlight中操作DOM元素
  11. DEA模型及matlab应用2:超效率SE-DEA模型
  12. 开根号的笔算算法图解_excel中开根号的计算方法步骤详解
  13. 电视机关机特效——android
  14. Android系统模拟位置的使用方法
  15. 淘宝天猫评论爬取,简单的办法完成滑动验证
  16. WinRAR 4.01 key!注册文件 注册码
  17. vue视频通话(Agora声网)
  18. FPGA:逻辑函数的卡诺图化简法
  19. np.meshgrid, ravel(), np.c_, plt.contourf()函数的用法,以及决策边界的画法。
  20. MyZip Pro for Mac(专业解压缩工具)

热门文章

  1. java —— 回文串字符
  2. 横幅广告已经完蛋了?
  3. 我的Serverless实战——能掰扯面试官的SSVM超详细解析!
  4. 绘画初学者怎么去学习漫画?有什么要注意的?
  5. Java小白读完这篇文章秒变大神,P8大师的赞叹不已
  6. python表情包语言_我是斗图王之python爬取表情包
  7. yandex地图js学习
  8. 《手把手教你学嵌入式无人机》——2.航模遥控器通信协议
  9. 进击的Transformer --- 一文介绍Efficient Transformers
  10. SqlServer订单表和订单明细表典型案例