算法(Algorithms)第4版 练习 1.5.2
0 1 2 3 4 5 6 7 8 9
10 components
9 0
0 1 2 3 4 5 6 7 8
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 2 8 0
5 components
5 7
0 1 1 4 4 8 6 2 0
4 components
0 3
1 1 4 4 8 6 2 1 0
3 components
4 2
4 1 1 4 8 6 2 1 0
2 components
森林图:

操作次数分析:
find函数每次访问数组次数是1 + 2 * depth
connected函数每次调用两次find函数
union函数每次调用两次find函数(如果两个连接点不在同一个树的话,则多一次数组访问)
public static void main(String[] args) {
//initialize N components
int N = StdIn.readInt();
UFQuickUnion uf = new UFQuickUnion(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 + 1,总的为2 * 1 + 2 * 1 + 1
3 4:3和4的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 1,总的为2 * 1 + 2 * 1 + 1
5 8:5和8的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 1,总的为2 * 1 + 2 * 1 + 1
7 2:7和2的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 1,总的为2 * 1 + 2 * 1 + 1
2 1:2和1的深度都为0,find访问数组次数为1,connected为2 * 1, union为2 * 1 + 1,总的为2 * 1 + 2 * 1 + 1
5 7:5的深度为1,7的深度为2。find访问数组次数分别为3、5,connected为3 + 5, union为3 + 5 + 1,总的为3 + 5 +3 + 5 + 1
0 3:0的深度为0,3的深度为1。find访问数组次数分别为1、3,connected为1 + 3, union为1 + 3 + 1,总的为1 + 3 +1 + 3 + 1
4 2:4的深度为0,2的深度为1。find访问数组次数分别为1、3,connected为1 + 3, union为1 + 3 + 1,总的为1 + 3 +1 + 3 + 1
源代码:
package com.qiusongde; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class UFQuickUnion { private int[] id;//save the site's parent link(site indexed)
private int count;//number of components public UFQuickUnion(int n) { 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) { //find root
//id[p] save the parent of p
while(p != id[p])
p = id[p]; return p;
} public void union(int p, int q) { int pRoot = find(p);//find pRoot
int qRoot = find(q);//find qRoot if(pRoot == qRoot)
return; id[pRoot] = qRoot;
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();
UFQuickUnion uf = new UFQuickUnion(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.2的更多相关文章
- 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 ...
随机推荐
- linux下yum安装maven
Maven 官网:http://maven.apache.org/ 源码安装 http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binarie ...
- PHP利用GD库绘图和生成验证码图片
首先得确定php.ini设置有没有打开GD扩展功能,測试例如以下 print_r(gd_info()); 假设有打印出内容例如以下,则说明GD功能有打开: Array ( [GD Version] = ...
- poj 2553 The Bottom of a Graph(强连通、缩点、出入度)
题意:给出一个有向图G,寻找所有的sink点.“sink”的定义为:{v∈V|∀w∈V:(v→w)⇒(w→v)},对于一个点v,所有能到达的所有节点w,都能够回到v,这样的点v称为sink. 分析:由 ...
- 【转】【FTP】之windows8.1上搭建FTP服务器方法
参考地址:<windows8.1上搭建FTP服务器方法>
- SecureCRT 设置字体跟颜色
SecureCRT 绝佳配色方案, 保护你的眼睛 分类: Linux 软件使用2013-05-17 08:45 24038人阅读 评论(11) 收藏 举报 SecureCRT 绝佳配色方案, 保护你的 ...
- VMWare虚拟机下为Ubuntu 12.04.2配置静态IP(NAT方式)
http://www.cnblogs.com/objectorl/archive/2012/09/27/vmware-ubuntu-nat-static-ip-settings.html 参考以上方式 ...
- ipmitool常用命令
然后查看ip等信息,使用如下命令: ipmitool lan print 远程访问 可使用如下命令尝试: ipmitool -I lanplus -H 10.108.125.227 -U Admini ...
- 一篇很不错的关于WPF DataGrid的文章,包含validation
https://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples
- Android屏幕密度(Density)和分辨率概念详解
移动设备有大有小,那么如何适应不同屏幕呢,这给我们编程人员造成了很多困惑.我也是突然想到这些问题,然后去网上搜搜相关东西,整理如下. 首先,对下面这些长度单位必须了解. Android中的长度单位 ...
- Java数据结构-线性表之顺序表ArrayList
线性表的顺序存储结构.也称为顺序表.指用一段连续的存储单元依次存储线性表中的数据元素. 依据顺序表的特性,我们用数组来实现顺序表,以下是我通过数组实现的Java版本号的顺序表. package com ...