Algorithms Edition 4 chapter 1 percolation 算法第一章课后作业
来源:互联网 发布:华天软件科技有限公司 编辑:程序博客网 时间:2024/06/06 19:52
最近在看爱课程上面princeton的算法课程,因为不熟悉流程,走了不少弯路,现在分享一下自己的学习过程,方便大家学习。第一次写博客,如果有错误的地方,希望大家指出。
STEP1:安装DrJava
点击上面的连接进入网站下载algs4.exe,下载后双击文件,一间配置所需环境。双击下载的文件会跳出DOS窗口,程序会自动配置所需环境,这个过程需要几分钟,取决于你的网速。配置完成后DOS界面会是这样的样子。
tips:如果windows系统跳出安全警告,允许或者忽略就行
配置完成后,桌面上会多出两个图标: Prompt windows 和 DrJava 。删除 Prompt windows ,这是一个辅助的方式,操作比较麻烦,建议不用。保留DrJava就行。
STEP2:通过DrJava建立你的第一个java程序
然后输入我们的第一个 HelloWorld.java 的代码
public class HelloWorld{ public static void main(String[] args){ System.out.println("Hello World"); }}
在保存我们的HelloWorld.java到C:\Users\username\algs4\hello; username是你电脑上的用户名,比如我电脑上的用户名是stn,那么把username替换为stn,此外需要在algs4文件中建立一个名为hello的文件夹,如果不想这么麻烦,那么直接把你的程序保存在C:\Users\username\algs4中,不多这样不便于管理你的程序,推荐用前面一种路径,即每建立一个新的java文件在algs文件中建立一个相同名字的文件夹,方便自己查询。
不同于VS这样的编译器,DrJava需要我们手动输入代码来运行我们的程序,即 java classname,我们建立的类为HelloWorld,name在输入框中输入
java HelloWorld
.
.
单击回车即得到我们预期效果。
如果我们要用到课程中所用的API,那么需要在类之外,程序的开头引用响应的类:
import edu.princeton.cs.algs4.*;这是一种比较偷懒的方式,引用algs4中的全部函数,提交的作业的时候会有一定warning,不过没什么大的问题,如果不想要warning,可以不用*,而用响应的库名来代替。
STEP3:percolation
说了这么多终于要进入正题了。
先说一下题干:详细的要求请移步 Programming Assignment 1: Percolation
part1:审题
之前没有发现有上面的网站,拿起讲义中的一点内容就开始写程序,简直摸不着头脑。开始敲程序之前,一定要好好的看看讲师的要求。
1.需要引用的库
import edu.princeton.cs.algs4.StdRandom;import edu.princeton.cs.algs4.StdStats;import edu.princeton.cs.algs4.WeightedQuickUnionUF;
2.需要建立的类以及方法
public class Percolation { public Percolation(int n) // create n-by-n grid, with all sites blocked public void open(int row, int col) // open site (row, col) if it is not open already public boolean isOpen(int row, int col) // is site (row, col) open? public boolean isFull(int row, int col) // is site (row, col) full? public int numberOfOpenSites() // number of open sites public boolean percolates() // does the system percolate? public static void main(String[] args) // test client (optional)}
public class PercolationStats { public PercolationStats(int n, int trials) // perform trials independent 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() // low endpoint of 95% confidence interval public double confidenceHi() // high endpoint of 95% confidence interval public static void main(String[] args) // test client (described below)}类的名称以及方法的名字一定要和所给的一样,否则提交作业会有问题。
如果自己要建立上面所提到之外的方法,建议申明为private类型
3.一些具体的要求
1>针对percolation
- 表格行列的表达应该按照人的思维来定位比如左上角方格位置应该表示为(1,1)而不是(0,0),换句话说,行列的值应该在1~n之间,包括端点
- 在
open()
,isOpen()
,isFull()函数中,如果参数不在1~n之内抛出 IllegalArgumentException
- 构造函数中,如果n和trials小于0,抛出 IllegalArgumentException
- main() 函数应该有两个命令行参数n 和 T ,分别对应构造函数里面的n和trials
- 用StdRandom产生随机数
- 用StdStats计算平均值和样本标准差
part 2:题意解读
先贴代码,睡了起来继续写
percolation的代码
import edu.princeton.cs.algs4.StdIn;import edu.princeton.cs.algs4.StdOut;import edu.princeton.cs.algs4.WeightedQuickUnionUF;// there should be a whitespace// finished in 2017/11/2 by Xingyu Lai public class Percolation { private final boolean[] status;// record the status of every item: true for open,false for blocked private final int rows,cols; private nt count=0; private final int longth;// convert rows*cols to longth private final WeightedQuickUnionUF uf,uf1;// union items public Percolation(int n) { if(n<= 0) throw new IllegalArgumentException("n must be bigger than 0 !"); longth=n*n; rows=n; cols=n; status=new boolean[longth];// allocate memory for status uf=new WeightedQuickUnionUF(longth+2); uf1=new WeightedQuickUnionUF(longth+1); // reater a tree of n+2, position at longth and longth+1 are for vitual top and bottom respectivly for(int i=0;i<longth-1;i++) status[i]=false;// every item is blocked default } public void open(int row,int col) { // open the item at(row,col) if not & union it to others which are open and closed to it if (row<1||row>rows) throw new IllegalArgumentException("index rows must between 1 and" + " " + rows); if (col<1||col>cols) throw new IllegalArgumentException("index rows must between 1 and" + " " + cols); int pos=revert(row,col);// covert 2D position to 1D position if (status[pos]) return;// if the item is open,then quit count++; status[pos]=true;// open the item // union items around the current item if (row==1) { uf.union(pos,longth); uf1.union(pos,longth); } //if the item is in the first row ,then union it to the visual top if (row==rows) uf.union(pos,longth+1);// if the item is in the bottom,then union it to the visual bottom // covert the items' positon that are over and below the current item to 1D position int up=revert(row-1,col); int down=revert(row+1,col); if (row>1) { if(status[up]){ uf.union(up,pos); uf1.union(up,pos); } } // if the item'row is bigger than 1 and the item over it is open,then union thenm together if (row<rows) { if(status[down]) {uf.union(down,pos); uf1.union(down,pos); } } // if the item'row is less than rows and the item below it is open,then union thenm together int left=revert(row,col-1); int right=revert(row,col+1); if (col>1) { if(status[left]) { uf.union(left,pos); uf1.union(pos,left); } } // if the item'col is bigger than 1 and the left item is open,then union thenm together if (col<rows) { if (status[right]) { uf.union(right,pos); uf1.union(pos,right); } } // if the item'col is less than cols and the right item is open,then union thenm together } public boolean percolates() { boolean a=uf.connected(longth,longth+1); return a; //if the visual top and bottom is connected ,then the table is percolated } public boolean isOpen(int row,int col) { //validate item at(row,col) open or not if(row<1||row>rows) throw new IllegalArgumentException("index rows must between 1 and" + " " + rows); if(col<1||col>cols) throw new IllegalArgumentException("index rows must between 1 and" + " " + cols); int pos=(row-1)*rows+col-1; return status[pos]; } public boolean isFull(int row,int col) { if(row<1||row>rows) throw new IllegalArgumentException("index rows must between 1 and" + " " + rows); if(col<1||col>cols) throw new IllegalArgumentException("index rows must between 1 and" + " " + cols); int pos=(row-1)*rows+col-1; return uf1.connected(pos,longth); } public int numberOfOpenSites() { return count; } private int revert(int row,int col){ int a=(row-1)*rows+col-1; return a; } public static void main(String[] args) { int n=StdIn.readInt(); StdOut.println("please input a integer bigger than 1 to establish a tree"); Percolation per=new Percolation(n);// read the first arg to establish a tree int row,col; StdOut.println("please input integer to union tree"); StdOut.println("enter Crtl + D to quit..."); while (!StdIn.isEmpty()) { // //read two numbers one by one for row and col row=StdIn.readInt(); col=StdIn.readInt(); per.open(row,col); } if (per.percolates()) StdOut.println("It is percolated!"); else StdOut.println("It is not percolated!"); }}
percolationStats的代码
import edu.princeton.cs.algs4.StdOut;import edu.princeton.cs.algs4.StdStats;import edu.princeton.cs.algs4.StdRandom;// finished in 2017/11/2 by Xingyu Lai public class PercolationStats{ private final double CONFIDENCE_95=1.96; private int times; // times for experiment times,count for open sites every experiment private double[] p; // record the status for every site,true for open, false for blocked public PercolationStats(int n,int trials) { if (n<= 0 || trials<= 0) throw new IllegalArgumentException("n and trials should be bigger than 0 !"); p=new double[trials]; // allocate memory for p times=trials; int row,col,count; for (int i= 0;i < trials;i++) { count=0; // clear the value of count for a new test Percolation per=new Percolation(n); while (!per.percolates()) { // quit until table is percolated do { // select a block site randomly row=StdRandom.uniform(n)+1; col=StdRandom.uniform(n)+1; }while (per.isOpen(row,col)); per.open(row,col); count++; // cout plus one when a new site opens } p[i]=count/(double)(n*n); // calculate rate of open sites every experiment } } public double mean() { // calculate the average rate of the all experiments double sum=0; sum=StdStats.mean(p); return sum; } public double stddev(){ double sum=0; sum=StdStats.stddev(p); return sum; } public double confidenceLo() { double a=mean()-CONFIDENCE_95*stddev()/Math.sqrt((double) times); return a; } public double confidenceHi() { double a=mean()+CONFIDENCE_95*stddev()/Math.sqrt((double) times); return a; } public static void main(String[] args) { int n=Integer.parseInt(args[0]); // the first args for n which decides the size of table int T=Integer.parseInt(args[1]); // the second args for T which decides the experiment times PercolationStats per=new PercolationStats(n,T); StdOut.println("mean =" + per.mean()); StdOut.println("stddev =" + per.stddev()); StdOut.println("95%% confidence interval = [" + per.confidenceLo()+","+per.confidenceLo()+"]"); }}
阅读全文
0 0
- Algorithms Edition 4 chapter 1 percolation 算法第一章课后作业
- HTML第一章课后作业4
- Coursera普林斯顿大学算法课-编程作业1: Percolation
- Coursera Algorithms Programming Assignment 1: Percolation
- C#课后作业第一章2
- 使用C# 第一章课后作业
- 《算法》第四版《Algorithms》4th Edition 学习环境搭建
- Coursera Algorithms Percolation解题记录
- Algorithms, Part I WEEK1 Percolation
- [算法作业] Chapter 8 NP-complete problems 课后习题部分证明
- coursera算法课 Programming Assignment 1:Percolation
- 《C程序设计第三版》课后作业【第一章】
- Linux 第一章、第二章 课后作业
- 数据结构与算法 Data Structures and Algorithms(作业1)
- 算法绪论第一章作业
- 算法课后作业第二题
- Percolation 渗透算法
- Coursera.Algorithms.Part.I.Week.1.Percolation
- spring 中.properties加载顺序
- Fresco的配置及使用
- ubuntu14.04 LTS版本下安装配置gem5环境
- 野指针
- The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved
- Algorithms Edition 4 chapter 1 percolation 算法第一章课后作业
- 英版树莓派更改键盘布局
- Django 时间与时区设置问题
- 初识JavaScript依赖注入
- 树莓派更换软件源
- 20171102
- Python高阶函数及匿名函数
- 英版树莓派中文环境和中文输入法的配置
- 矩阵快速幂