算法(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 ...
随机推荐
- angularjs中的时间格式化过滤
本地化日期格式化: ({{ today | date:'medium' }})Nov 19, 2015 3:57:48 PM ({{ today | date:'short' }})11/19/15 ...
- Android IntentService全然解析 当Service遇到Handler
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...
- jsp 页面导出excel时字符串数字变成科学计数法的解决方法
web导出excel数据格式化 原文地址:http://www.cnblogs.com/myaspnet/archive/2011/05/06/2038490.html 当我们把web页面上的数据 ...
- 初始化map和list的两种写法
第一种方法(常用方法): //初始化List List list = new ArrayList(); list.add("string1"); list.add("st ...
- CentOS6.4下编译安装Apache2.4+PHP5.6
安装Apache2.4: 首先从 http://httpd.apache.org/download.cgi#apache24下载apache源码包httpd-2.4.4.tar.gz从 http: ...
- 【Python + Selenium】之unittest测试用例满足条件,进行跳过测试Skip
直接上代码: __author__ = 'zc' import unittest class demoSkipTest(unittest.TestCase): a = 70 b = 50 print( ...
- chattr
chattr 功能:设置文件隐藏属性常用参数:+ 增加某个特殊权限,其他原本存在的参数不动- 删除某个特殊权限,其他原本存在的参数不动= 设置一定,且仅有后面接的参数 i 文件 ...
- iOS图片无损拉伸
一张图片如果放大的话一般情况下会失真,如果该图片是规则的,比如这个聊天气泡,可以用如下代码来设置 UIImage *rightImg = [UIImage imageNamed:@"Sen ...
- BZOJ 1602 [Usaco2008 Oct]牧场行走 dfs
题意:id=1602">链接 方法:深搜暴力 解析: 这题刚看完还有点意思,没看范围前想了想树形DP,只是随便画个图看出来是没法DP的,所以去看范围. woc我没看错范围?果断n^2暴 ...
- php之 人员的权限管理
1.想好权限管理的作用? 2.有什么权限内容? 3.既然有权限管理那么就会有管理员? 4.登录后每个人员的界面会是不一样的? 一.想好这个权限是什么? 就做一个就像是vip的功能,普通用户和vip用户 ...