普林斯顿算法(第一周作业Percolation 100分)
什么是backwash?
如上图,使用一个WeightedQuickUnionUF对象,带有virtual top site和virtual bottom site。当渗透时,bottom row上可能存在连通virtual bottom site,但是没有连通virtual top site的site。比如上图红色圈内的site,调用isFull()时返回true,因为会通过红色箭头连通到virtual top site,这就产生了backwash问题。
可以使用两个WeightedQuickUnionUF对象避免backwash问题,其中一个只带有virtual top site。
存在的问题:使用两个WeightedQuickUnionUF对象,存在内存占用问题。
********************************************************************************
* MEMORY
********************************************************************************Analyzing memory of Percolation
*-----------------------------------------------------------
Running 4 total tests.Test 1a-1d: check that total memory <= 17 n^2 + 128 n + 1024 bytesn bytes
--------------------------------------------
=> passed 64 69944
=> passed 256 1114424
=> passed 512 4456760
=> passed 1024 17826104
==> 4/4 tests passedEstimated student memory = 17.00 n^2 + 0.00 n + 312.00 (R^2 = 1.000)Test 2 (bonus): check that total memory <= 11 n^2 + 128 n + 1024 bytes- failed memory test for n = 64
==> FAILEDTotal: 4/4 tests passed!
Percolation.java
import edu.princeton.cs.algs4.WeightedQuickUnionUF;/*** $ClassName Percolation* $Description TODO* $Author Freedom Wen* $Date 2022/8/7 22:47*/
public class Percolation {private final int n; // n阶private int openSiteNum; // 已经open的数量private final boolean[] site;private final WeightedQuickUnionUF wquWithTopAndBottom; // 带两个虚拟结点的modelprivate final WeightedQuickUnionUF wquWithTop; // 带两个虚拟结点的modelprivate final int virtualTop; // 虚拟头部结点private final int virtualBottom; // 虚拟底部结点// creates n-by-n grid, with all sites initially blockedpublic Percolation(int n) {if (n < 1) {throw new IllegalArgumentException("param is not valid!");}this.n = n;openSiteNum = 0;virtualTop = 0; //virtualBottom = n * n + 1;site = new boolean[n * n + 2];site[virtualTop] = true;site[virtualBottom] = true;for (int i = 1; i < n*n; i++) {site[i] = false;}wquWithTopAndBottom = new WeightedQuickUnionUF(n * n + 2); // with top and bottomwquWithTop = new WeightedQuickUnionUF(n * n + 1); // only with top}private void checkParams(int row, int col) {if (row < 1 || row > n || col < 1 || col > n) {throw new IllegalArgumentException("params are not valid!");}}private int getSitePos(int row, int col) {return (row - 1) * n + col;}// opens the site (row, col) if it is not open alreadypublic void open(int row, int col) {checkParams(row, col);if (isOpen(row, col)) {return;}int sitePos = getSitePos(row, col);site[sitePos] = true;openSiteNum++; // 每打开一个结点,openSiteNum+1// 连通周围已经打开的结点connectSite(row, col, sitePos);}private void connectSite(int row, int col, int sitePos) {// 当row==1时,连接结点到virtualTopif (row == 1) {wquWithTopAndBottom.union(sitePos, virtualTop);wquWithTop.union(sitePos, virtualTop);}// 当row==n时,连接结点到virtualBottomif (row == n) {wquWithTopAndBottom.union(sitePos, virtualBottom);}if (row > 1 && isOpen(row - 1, col)) {wquWithTopAndBottom.union(sitePos, getSitePos(row - 1, col));wquWithTop.union(sitePos, getSitePos(row - 1, col));}if (row < n && isOpen(row + 1, col)) {wquWithTopAndBottom.union(sitePos, getSitePos(row + 1, col));wquWithTop.union(sitePos, getSitePos(row + 1, col));}if (col > 1 && isOpen(row, col - 1)) {wquWithTopAndBottom.union(sitePos, getSitePos(row, col - 1));wquWithTop.union(sitePos, getSitePos(row, col - 1));}if (col < n && isOpen(row, col + 1)) {wquWithTopAndBottom.union(sitePos, getSitePos(row, col + 1));wquWithTop.union(sitePos, getSitePos(row, col + 1));}}// is the site (row, col) open?public boolean isOpen(int row, int col) {checkParams(row, col);int sitePos = getSitePos(row, col);return site[sitePos];}// is the site (row, col) full?public boolean isFull(int row, int col) {checkParams(row, col);return wquWithTop.find(getSitePos(row, col)) == wquWithTop.find(virtualTop);}// returns the number of open sitespublic int numberOfOpenSites() {return openSiteNum;}// does the system percolate?public boolean percolates() {return wquWithTopAndBottom.find(virtualTop) == wquWithTopAndBottom.find(virtualBottom);}// test client (optional)// public static void main(String[] args) {// }
}
PercolationStats.java
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;/*** $ClassName PercolationStats* $Description TODO* $Author Freedom Wen* $Date 2022/8/7 22:47*/
public class PercolationStats {private static final double CONFIDENCE_95 = 1.96;private final double[] threshold;private final int trials;// perform independent trials on an n-by-n gridpublic PercolationStats(int n, int trials) {if (n <= 0 || trials <= 0) {throw new IllegalArgumentException("illegal args exception!");}this.trials = trials;threshold = new double[trials];for (int i = 0; i < trials; i++) {Percolation percolation = new Percolation(n);while (!percolation.percolates()) {int row = StdRandom.uniform(1, n + 1);int col = StdRandom.uniform(1, n + 1);percolation.open(row, col);}int openSites = percolation.numberOfOpenSites();threshold[i] = (double) openSites / (n * n);}}// sample mean of percolation thresholdpublic double mean() {return StdStats.mean(threshold);}// sample standard deviation of percolation thresholdpublic double stddev() {return StdStats.stddev(threshold);}// low endpoint of 95% confidence intervalpublic double confidenceLo() {return mean() - CONFIDENCE_95 * stddev() / Math.sqrt(trials);}// high endpoint of 95% confidence intervalpublic double confidenceHi() {return mean() + CONFIDENCE_95 * stddev() / Math.sqrt(trials);}// test client (see below)public static void main(String[] args) {PercolationStats percolationStats = new PercolationStats(Integer.parseInt(args[0]), Integer.parseInt(args[1]));StdOut.println("mean = " + percolationStats.mean());StdOut.println("stddev = " + percolationStats.stddev());StdOut.println("95% confidence interval = [" + percolationStats.confidenceLo() + ","+ percolationStats.confidenceHi() + "]");}
}
普林斯顿算法(第一周作业Percolation 100分)相关推荐
- 「数据结构」普林斯顿算法课第一周作业
「数据结构」普林斯顿算法课第一周作业 Algorithm I, Princeton 编程作业: Percolation 思路 第一部分代码展示 第二部分代码展示 编程作业: Percolation P ...
- 【中文】【吴恩达课后编程作业】Course 5 - 序列模型 - 第一周作业
[中文][吴恩达课后编程作业]Course 5 - 序列模型 - 第一周作业 - 搭建循环神经网络及其应用 上一篇:[课程5 - 第一周测验]※※※※※ [回到目录]※※※※※下一篇:[课程5 - 第 ...
- 【中文】【吴恩达课后编程作业】Course 2 - 改善深层神经网络 - 第一周作业(123)
[中文][吴恩达课后编程作业]Course 2 - 改善深层神经网络 - 第一周作业(1&2&3) - 初始化.正则化.梯度校验 上一篇:[课程2 - 第一周测验]※※※※※ [回到目 ...
- 输出结果为16的python表达式_第一周作业(rayco)
rayco 第一周作业 第一次-课后习题 a = 10 b = 3 c = a/b-a print(c, type(c)) c = a/b*a print(c, type(c)) c = 0.1*a/ ...
- 吴恩达 02.改善深层神经网络:超参数调试、正则化以及优化 第一周作业
Initialization Welcome to the first assignment of "Improving Deep Neural Networks". Traini ...
- 2017-2018-2 《密码与安全新技术》第一周作业
2017-2018-2 <密码与安全新技术>第一周作业 课程:<密码与安全新技术> 班级:2017级92班 学号:20179225 上课教师:谢四江 上课日期:2018年3月1 ...
- 学号20189220余超 2018-2019-2 《密码与安全新技术专题》第一周作业
学号20189220 2018-2019-2 <密码与安全新技术专题>第一周作业 课程:<密码与安全新技术专题> 班级: 1892 姓名: 余超 学号:20189220 上课教 ...
- 第一周作业(零基础)
第一周作业 一.选择题 下列变量名中不合法的是?(C) A. abc B. Npc C. 1name D ab_cd 下列选项中不属于关键字的是?(B) A. and B. print C. True ...
- OUC2022秋季软件工程第一周作业
注:本博客为OUC2022秋季软件工程第一周作业 文章目录 注:本博客为OUC2022秋季软件工程第一周作业 软件工程第18小组 成员: 一.个人简介 罗浩宇 二.四个问题 问题① 问题② 问题③ 问 ...
最新文章
- 转载:HBuilder常用快捷键
- python语言的考试_【Python学习路线】Python语言基础自测考试 - 中级难度
- Java程序员必须了解的JVM性能调优知识,全都在这里了
- python gzip压缩后传给前端_Python 3,从/向gzip文件读取/写入压缩的json对象
- 3._FILE_和_LINE_
- 吴恩达机器学习资源汇总帖
- UBUNTU下彻底删除MYSQL
- [Silverlight入门系列]使用MVVM模式(3):Model的INotifyPropertyChanged接口实现
- 洛谷 P1018乘积最大
- 一、MySQL查询学习笔记(基础查询、条件查询、排序查询、常见函数、分组查询 详解)
- YII中的CComponent,CEvent与Behavior及CActiveRecordBehavior
- 7类数据分析常见的统计陷阱,快来排雷!
- html代码编辑器jason,JSON 编辑器实现代码
- 计算机四级网络工程师(备考过程,避开误区,高效备考!)
- Newton插值法 | 差商 + Newton插值公式 + 插值余项
- 江西应用技术职业学院计算机协会,江西应用技术职业学院47个学生社团陆续召开动员大会...
- 中文文字检测及识别(ORC)
- 数据库之 MySQL—— 50个查询系列
- 关于USART波特率、TIM的外设预分频值
- ROS开发之如何使用RPLidar A1二维激光雷达?