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. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

  10. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. sublime 插件篇

    添加插件 进入https://packagecontrol.io/installation   找到 或者 直接复制 import urllib.request,os,hashlib; h = '6f ...

  2. .net 哈希表和字典的基本用法

    哈希表 传送门:https://www.cnblogs.com/xpvincent/archive/2013/01/15/2860841.html using System; using System ...

  3. linux查看mysql运行日志

    在命令行输入下面命令就可以了,如果看到报错的话,直接copy去谷歌就OK: [root@localhost /]# vim /var/log/mysqld.log

  4. 定时执行线程池ScheduledExecutorService的使用

    ScheduledExecutorService progressExecutorService = Executors.newScheduledThreadPool(1); ScheduledFut ...

  5. sgu101-欧拉回路

    101. Domino time limit per test: 0.25 sec.  memory limit per test: 4096 KB Dominoes – game played wi ...

  6. 前言和第一章.NET的体系结构

    前言 COM:组件对象模型(Component Object Model COM)源自对象链接和嵌入(Object Linking and Embedding )OLE. DCOM:(Distribu ...

  7. 跳转 nginx 跳转 apache跳转

    公司在google上投广告,需要做一些很简单的站去google上投广告,当用户在google上点击那些很简单的网站的时候,就会跳转到真实的网站.但是,如果用户直接在浏览器输入域名,并访问的话,那样就不 ...

  8. Android使用JUnit进行单元测试

    前言:为什么要进行单元测试?单元测试能快速是开发者,找到代码中的问题所在,因为是单元测试,所以代码只执行响应的测试单元,执行快解决问题的效率高,同时提高代码的质量. Android中的单元测试可简单分 ...

  9. PHP-Manual的学习----【语言参考】----【类型】-----【Resource 资源类型】

    2017年8月24日11:29:361.资源 resource 是一种特殊变量,保存了到外部资源的一个引用.资源是通过专门的函数来建立和使用的.2.由于资源类型变量保存有为打开文件.数据库连接.图形画 ...

  10. 洛谷P2661 信息传递==coedevs4511 信息传递 NOIP2015 day1 T2

    P2661 信息传递 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知 ...