算法(Algorithms)第4版 练习 1.5.3
id数组和treesize数组变化情况:
0 1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1 1
10 components
9 0
1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1
9 components
3 4
9 1 2 3 5 6 7 8 9
1 1 1 1 1 1 1 1 2
8 components
5 8
9 1 2 3 3 5 6 7 9
1 1 1 2 1 1 1 1 2
7 components
7 2
9 1 3 3 5 6 7 5 9
1 1 1 2 1 2 1 1 2
6 components
2 1
9 7 3 3 5 6 7 5 9
1 1 1 2 1 2 1 1 2
5 components
5 7
9 7 7 3 3 6 7 5 9
1 1 1 2 1 2 1 1 2
4 components
0 3
9 7 7 3 7 6 7 5 9
1 1 1 2 1 2 1 5 1
3 components
4 2
9 7 7 9 3 7 6 7 5
1 1 1 2 1 2 1 1 4
2 components
森林图:

操作次数分析:
find函数每次访问数组次数是1 + 2 * depth
connected函数每次调用两次find函数
union函数每次调用两次find函数(如果两个连接点不在同一个树的话,则多一次数组访问)
public static void main(String[] args) {
//initialize N components
int N = StdIn.readInt();
UFWeightedQuickUnion uf = new UFWeightedQuickUnion(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:9和0的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 5,总的为2 * 1 + 2 * 1 + 5
3 4:3和4的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 5,总的为2 * 1 + 2 * 1 + 5
5 8:5和8的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 5,总的为2 * 1 + 2 * 1 + 5
7 2:7和2的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 5,总的为2 * 1 + 2 * 1 + 5
2 1:2的深度为1,1的深度为0。find访问数组次数分别为3、1,connected为3 + 1, union为3 + 1 + 5,总的为3 + 1 +3 + 1 + 5
5 7:5的深度为0,7的深度为0。find访问数组次数分别为1、1,connected为1 + 1, union为1 + 1 + 5,总的为1 + 1 +1 + 1 + 5
0 3:0的深度为1,3的深度为0。find访问数组次数分别为3、1,connected为3 + 1, union为3 + 1 + 5,总的为3 + 1 +3 + 1 + 5
4 2:4的深度为2,2的深度为1。find访问数组次数分别为5、3,connected为5 + 3, union为5 + 3 + 5,总的为5 + 3 +5 + 3 + 5
源代码:
package com.qiusongde; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class UFWeightedQuickUnion { private int[] id;//parent link(site indexed)
private int[] treesize;//size of component for roots(site indexed)
private int count;//number of components public UFWeightedQuickUnion(int N) { count = N; id = new int[N];
for(int i = 0; i < N; i++)
id[i] = i; treesize = new int[N];
for(int i = 0; i < N; i++)
treesize[i] = 1; } public int count() {
return count;
} public boolean connected(int p, int q) {
return find(p) == find(q);
} public int find(int p) { while(p != id[p])
p = id[p]; return p; } public void union(int p, int q) { int pRoot = find(p);
int qRoot = find(q); if(pRoot == qRoot)
return; //make smaller root point to larger one
if(treesize[pRoot] < treesize[qRoot]) {
id[pRoot] = qRoot;
treesize[qRoot] += treesize[pRoot];
} else {
id[qRoot] = pRoot;
treesize[pRoot] += treesize[qRoot];
} count--; } @Override
public String toString() {
String s = ""; for(int i = 0; i < id.length; i++) {
s += id[i] + " ";
}
s += "\n"; for(int i = 0; i < treesize.length; i++) {
s += treesize[i] + " ";
}
s += "\n" + count + " components"; return s;
} public static void main(String[] args) { //initialize N components
int N = StdIn.readInt();
UFWeightedQuickUnion uf = new UFWeightedQuickUnion(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.3的更多相关文章
- 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 ...
随机推荐
- Hibernate二次学习二----------session.flush、session.doWork
目录 1. session 2. session.flush 3. session.doWork 4. 完整代码 5. 总结 © 版权声明:本文为博主原创文章,转载请注明出处 1. session H ...
- js中有特殊字符的编码格式
在get和post方法中,如果传入的参数值有特殊字符,如:“&”,在get中的url需要拼接,可以使用encodeURICompontent来编码来转化 回调就是在上面传递实际参数,传递给aj ...
- smarty模版使用php标签,如何获取模版变量
smarty模版使用php标签,如何获取模版变量 in: 后端程序 已经assign一个模版变量$assign,由于要做特殊的循环输出,使用for循环,因此使用到了php标签,但是php语句和模版语句 ...
- vim与windows/linux之间的复制粘贴小结
vim与windows/linux之间的复制粘贴小结 用 vim这么久了,始终也不知道怎么在vim中使用系统粘贴板,通常要在网上复制一段代码都是先gedit打开文件,中键粘贴后关闭,然后再用vim打开 ...
- 大师养成计划之一:搭建springmvc框架
搭建spring-mvc框架 搭建spring-mvc框架步骤: 1.搭建web项目spring-mvc1 2.引入jar包 3.配置web.xml 3.1拷贝头文件: <web-app xml ...
- 51系列xdata、idata、data的用法
从数据存储类型来说,8051系列有片内.片外程序存储器,片内.片外数据存储器,片内程序存储器还分直接寻址区和间接寻址类型,分别对应code.data.xdata.idata以及根据51系列特点而设定的 ...
- 【Mac系统 + Python + Django】之开发一个发布会系统【Django视图(二)】
此学习资料是通过虫师的python接口自动化出的书学习而来的,在此说明一下,想学习更多的自动化的同学可以找虫师的博客园,非广告,因为我python+selenium自动化也是跟虫师学的,学习效果很好的 ...
- 【SQLServer2008】之Win10 安装 SQL Server 2008
查看安装步骤链接: http://jingyan.baidu.com/article/1709ad8092be974634c4f0e7.html
- json:js和jquery中轻量级数据交换格式
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...
- IT人和普洱茶
IT人与普洱茶 作为一个平凡的IT人,在小孩眼中我就像黑客帝国的主角一样了不起:在亲戚眼中我是在写字楼做办公室吹空调的人:在朋友眼中我就是一个会写代码.掌握高科技术的人:在女友眼中我是一个在名企工作的 ...