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. PHP array_walk() 函数

    定义和用法 array_walk() 函数对数组中的每个元素应用用户自定义函数.在函数中,数组的键名和键值是参数. <?php function myfunction($value,$key,$ ...

  2. Struts2学习之拦截器

    © 版权声明:本文为博主原创文章,转载请注明出处 拦截器: - Struts2大多数核心功能都是通过拦截器实现的,每个拦截器完成某项功能 - 拦截器方法在Action执行之前或之后执行 工作原理: - ...

  3. Angular 资料大集合

    https://angular.cn/   Angular 的中文网 http://www.apjs.net/#dir1    Angular 的中文网 http://www.ngnice.com/  ...

  4. CAN协议学习(二)MCAN控制器介绍

    更多细节请参看MCAN2文档mcan2_ps.pdf. 一.MCAN2简介 MCAN2是Mentor公司开发的一个CAN2.0网络控制器的软核,初版2001年末版2006年.MCAN 2控制器实现了B ...

  5. asp.net web.config配置节说明

    web.config 文件查找规则:      (1)如果在当前页面所在目录下存在web.config文件,查看是否存在所要查找的结点名称,如果存在返回结果并停止查找.      (2)如果当前页面所 ...

  6. spring+mybatis基于 AOP实现业务日志管理

    最近在项目上用到了操作日志的相关,之前的解决方案就是自己写一个日志project,然后统一调用日志接口即可,这样方便自定义定制,因为有很多设备控制之类的都是需要确认一下的,但是,对数据的操作,比如,增 ...

  7. GIS开发站点收藏

    Arcgis API for Silverlight ArcGIS API for Silverlight开发入门 C#开发ArcGIS

  8. Dbvisualizer 连接oracle数据库

    软件及驱动下载: 链接:https://pan.baidu.com/s/1OhuRDCd6FDi21NyCEdN2dA 密码:0rtp 软件破解办法: 1. 找到<C:\Program File ...

  9. less (css预处理)

    用法 1. 必须在head内 2. 样式文件必须先加载 <head> <meta charset='utf-8'> <link rel="stylesheet/ ...

  10. 跟我一起用Symfony写一个博客网站;

    我的微信公众号感兴趣的话可以扫一下, 或者加微信号   whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...