算法(Algorithms)第4版 练习 1.5.1
id数组的变化情况:
0 1 2 3 4 5 6 7 8 9
10 components
9 0
0 1 2 3 4 5 6 7 8 0
9 components
3 4
0 1 2 4 5 6 7 8 0
8 components
5 8
0 1 2 4 4 6 7 8 0
7 components
7 2
0 1 2 4 4 8 6 8 0
6 components
2 1
0 1 4 4 8 6 8 0
5 components
5 7
0 1 1 4 4 6 1 0
4 components
0 3
1 1 4 4 1 6 1 1
3 components
4 2
1 1 1 6 1 1
2 components
操作次数分析:
find()函数每次调用访问数组1次。
connected函数每次调用两次find()函数,故访问数组2次。
union函数访问数组的次数为:2 + N + (1,N-1)。其中2为两次调用find()函数,N为N次数组判断,(1,N-1)为可能的数组替换次数。
public static void main(String[] args) {
//initialize N components
int N = StdIn.readInt();
UFQuickFind uf = new UFQuickFind(N);
StdOut.println(uf);
while(!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if(uf.connected(p, q)) {//ignore if connected
StdOut.println(p + " " + q + " is connected");
continue;
}
uf.union(p, q);//connect p and q
StdOut.println(p + " " + q);
StdOut.println(uf);
}
对于这个client,对每个数据对,都调用一次connected函数和union函数。
下边对数组访问次数进行分析:
9 0:2 + 2 + 10 + 1
3 4:2 + 2 + 10 + 1
5 8:2 + 2 + 10 + 1
7 2:2 + 2 + 10 + 1
2 1:2 + 2 + 10 + 2
5 7:2 + 2 + 10 + 2
0 3:2 + 2 + 10 + 2
4 2:2 + 2 + 10 + 4
源代码:
package com.qiusongde; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class UFQuickFind { private int[] id;//access to component id (site indexed)
private int count;//number of components public UFQuickFind(int n) {
//initialize count and id
count = n; id = new int[n];
for(int i = 0; i < n; i++) {
id[i] = i;
} } public int count() {
return count;
} public boolean connected(int p, int q) {
return find(p) == find(q);
} public int find(int p) {
return id[p];
} public void union(int p, int q) { int pID = find(p);
int qID = find(q); //do nothing if p and q are already
//in the same component
if(pID == qID)
return; //rename p's component to q's name
for(int i = 0; i < id.length; i++) {
if(id[i] == pID)
id[i] = qID;
}
count--; } @Override
public String toString() {
String s = ""; for(int i = 0; i < id.length; i++) {
s += id[i] + " ";
}
s += "\n" + count + " components"; return s;
} public static void main(String[] args) { //initialize N components
int N = StdIn.readInt();
UFQuickFind uf = new UFQuickFind(N);
StdOut.println(uf); while(!StdIn.isEmpty()) { int p = StdIn.readInt();
int q = StdIn.readInt(); if(uf.connected(p, q)) {//ignore if connected
StdOut.println(p + " " + q + " is connected");
continue;
} uf.union(p, q);//connect p and q
StdOut.println(p + " " + q);
StdOut.println(uf);
} } }
算法(Algorithms)第4版 练习 1.5.1的更多相关文章
- 1.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- Write operations are not allowed in read-only mode (FlushMode.MANUAL)
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述 搭建SSH框架后,为测试事务配置是否生效,因此在事务配置中取消了保存方法,然后再保存方法中手动抛出异常(已测试配置事务后没有保存成功), ...
- centos root登录password 忘记解决的方法
Centos系统 登陆root忘记password 解决方式: (1)开机启动系统,在进入linux系统之前按键Esc 进入例如以下界面:(须要注意:Centos是安装在虚拟机里面的话,须要将鼠标点进 ...
- .Vue 文件 ES6 语法 webstorm 中的一个识别Bug
webstorm 2017 版本中即使安装了vue template file 设置了 js 语言为 es6 语法仍旧会出现识别不了划线的情况,苦寻很久,最后解决方式如下 <script typ ...
- git 修改远程仓库地址
以前的老项目需要修改git路径,为了保留之前的上传记录和分支等可以通过以下方法解决这个问题 sourceTree项目远程仓库,直接修改origin路径,然后提交一个commit即可将项目上传到新的gi ...
- 关于ViewData,ViewBag,TempData三者学习记录!
关于ViewData,ViewBag,TempData三者学习分享! 1.ViewData和TempData是字典类型,赋值方式用字典方式,ViewData["Key"] . 2. ...
- centos7设置ip
centos7不能再通过setup命令来设置ip了,但可以通过修改网卡配置文件来设置ip 在/etc/sysconfig/network-scripts目录下找到网卡配置文件,修改之前内容如下 TYP ...
- 【elasticsearch】安装合集
[elasticsearch](1)centos7 使用yum安装elasticsearch 2.X [elasticsearch](2)centos7 超简单安装elasticsearch 的监控. ...
- ngnix 参考配置
#user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...
- 【Python学习】之yagmail库实现发送邮件
上代码: import yagmail sendmail = 'xxx@126.com' sendpswd = 'xxx' receivemail = 'xxx@qq.com' # 连接邮箱服务器 y ...
- readonly const关键字
readonly 关键字与 const 关键字不同. const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 因此,根据所使用的构造函数,readonly ...