算法(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 ...
随机推荐
- Python内置函数之sorted()
sorted(iterable,*,key=None,reverse=False) 对可迭代对象进行排序,默认ASCII进行排序. 例子: sorted(iterable,*,key=None,rev ...
- PHP性能:序——谈性能
PHP性能:序——谈性能 这里不谈PHP的性能和其他语言的性能,这里讨论PHP自身的性能问题. 性能是什么? 通俗的来讲,性能,就是在固定的环境下能做的事情的多少. 为什么要性能? 1.每一个软件或网 ...
- python的list求和与求积
python中,无论是对的list求和还是求积,我都给出了两种方法. 1.对list求和 1.1 s=0 for i in range(10): s+=i 1.2 s=sum(range(10)) 2 ...
- MyEclipse中Save could not be completed
在MyEclipse下编程时,保存的时候,假设出现例如以下图所看到的错误: - 刘立 - 707903908的博客" src="http://img0.ph.126.net/9y4 ...
- Java 学习 day08
01-面向对象(多态-概念) package myFirstCode; /* 多态:可以理解为事务存在的多种体现形态. 人:男人,女人 动物:猫,狗 猫 x = new 猫(); 动物 x = new ...
- android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据
主要是基于HTTP协议与服务端进行交互. 涉及到的类和接口有:URL.HttpURLConnection.HttpClient等 URL: 使用一个String类型的url构造一个URL对象,如: U ...
- jmeter--基于http+json接口的功能测试
jmeter--基于http+json接口的功能测试 测试项目叫做smile_task,简称sm_task.这是一个基于nodejs超简单的todo list,sm_task没有任何UI界面(纯接口) ...
- python基础-第五篇-5.2递归
又是一个阳光明媚的日子,小白看着刚刚从东边升起的太阳,感觉太阳爷爷也在向她打招呼,小白就不经的微笑起来!心想:今天又会学到什么有趣的东西呢?有些小期待,也有些小激动! 小刘来得比小白还早,两辆相视而笑 ...
- 如何判断移动终端访问还是PC访问?
我们经常需要知道访问网站的设备是移动终端还是PAD还是PC,下面给出判断的java代码供参考.实现的原理就是获取HTTP消息头里User-Agent和x-wap-profile,User-下面是Use ...
- activiti--5 -----------------Activiti 工作流 流程各个步骤所涉及到的表
ACT_RE_*: 'RE'表示repository. 这个前缀的表包含了流程定义和流程静态资源 (图片,规则,等等). ACT_RU_*: 'RU'表示runtime. 这些运行时的表,包含流程实例 ...