算法(Algorithms)第4版 练习 1.5.14
package com.qiusongde; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class UFWQuickUnionByHeight { private int[] id;//parent link(site indexed)
private int[] treeheight;//size of component for roots(site indexed)
private int count;//number of components public UFWQuickUnionByHeight(int N) { count = N; id = new int[N];
for(int i = 0; i < N; i++)
id[i] = i; treeheight = new int[N];
for(int i = 0; i < N; i++)
treeheight[i] = 0; } public int count() {
return count;
} public boolean connected(int p, int q) {
return find(p) == find(q);
} public int find(int p) { int root = p;//initialize root //find root(id[p] save the parent of p)
while(root != id[root])
root = id[root]; return root; } 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(treeheight[pRoot] < treeheight[qRoot]) {
id[pRoot] = qRoot;
}
else if(treeheight[pRoot] == treeheight[qRoot]) {
id[qRoot] = pRoot;
treeheight[pRoot]++;
}
else {
//treeheight[pRoot] > treeheight[qRott]
id[qRoot] = pRoot;
} 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 < treeheight.length; i++) {
s += treeheight[i] + " ";
}
s += "\n" + count + " components"; return s;
} public static void main(String[] args) { //initialize N components
int N = StdIn.readInt();
UFWQuickUnionByHeight uf = new UFWQuickUnionByHeight(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");
StdOut.println(uf);
continue;
} uf.union(p, q);//connect p and q
StdOut.println(p + " " + q);
StdOut.println(uf);
} } }
运行结果:
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
10 components
4 3
0 1 2 4 4 5 6 7 8 9
0 0 0 0 1 0 0 0 0 0
9 components
3 8
0 1 2 4 4 5 6 7 4 9
0 0 0 0 1 0 0 0 0 0
8 components
6 5
0 1 2 4 4 6 6 7 4 9
0 0 0 0 1 0 1 0 0 0
7 components
9 4
0 1 2 4 4 6 6 7 4 4
0 0 0 0 1 0 1 0 0 0
6 components
2 1
0 2 2 4 4 6 6 7 4 4
0 0 1 0 1 0 1 0 0 0
5 components
8 9 is connected
0 2 2 4 4 6 6 7 4 4
0 0 1 0 1 0 1 0 0 0
5 components
5 0
6 2 2 4 4 6 6 7 4 4
0 0 1 0 1 0 1 0 0 0
4 components
7 2
6 2 2 4 4 6 6 2 4 4
0 0 1 0 1 0 1 0 0 0
3 components
6 1
6 2 6 4 4 6 6 2 4 4
0 0 1 0 1 0 2 0 0 0
2 components
1 0 is connected
6 2 6 4 4 6 6 2 4 4
0 0 1 0 1 0 2 0 0 0
2 components
6 7 is connected
6 2 6 4 4 6 6 2 4 4
0 0 1 0 1 0 2 0 0 0
2 components
证明可参照P229页的Proposition H的证明。
算法(Algorithms)第4版 练习 1.5.14的更多相关文章
- 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 ...
随机推荐
- 解决H5在微信浏览器或QQ浏览器修改title的问题
传送门:http://blog.csdn.net/code_for_free/article/details/51195468 如果是Android,使用 document.title = ‘1231 ...
- 自动化测试 python2.7 与 selenium 2 学习
windows环境搭建 # 下载 python[python 开发环境] http://python.org/getit/ # 下载 setuptools [python 的基础包工具]setupto ...
- vivado2013.4和modelsim联合仿真
vivado2013.4和modelsim联合仿真 Hello,Panda 最近在做Zynq的项目,曾经尝试使用ISE+PlanAhe ...
- Cassandra安装和初次使用
Cassandra安装和初次使用 卡珊德拉(Cassandra)又译卡桑德拉.卡珊卓,为希腊.罗马神话中特洛伊(Troy)的公主,阿波罗(Apollo)的祭司.因神蛇以舌为她洗耳或阿波罗的赐予而有预言 ...
- emacs的常用配置备份
据说有人搞丢了自己的emacs的配置,然后一怒之下抛弃了emacs投身vim,我还是做个emacs配置的备份吧, 虽然我现在也算不上emacs的发烧友. 这里的配置大多是从网上参考的,最多的是下面的链 ...
- UVA-11625-Nice Prefixes (DP+矩阵快速幂)
题目(vjudge) 题面 题意: 你有K个字母,你需要用K个字母组成L长度的字符串,定义对于该字符串的任意前缀P 必须满足 ,输出方案数%1000000007的值. 思路: 首先可以想到一种简 ...
- centos7.2 开发 部署 .net core
1.centos7.2 安装 nginx官方文档:http://nginx.org/en/linux_packages.html#mainline 创建nginx.repo 文件 Pre-Built ...
- RecyclerView加载更多用notifyDataSetChanged()刷新图片闪烁
首先来看看对比ListView看一下RecyclerView的Adapter主要增加了哪些方法: notifyItemChanged(int position) 更新列表position位置上的数据可 ...
- UFLDL深度学习笔记 (五)自编码线性解码器
UFLDL深度学习笔记 (五)自编码线性解码器 1. 基本问题 在第一篇 UFLDL深度学习笔记 (一)基本知识与稀疏自编码中讨论了激活函数为\(sigmoid\)函数的系数自编码网络,本文要讨论&q ...
- COM对象模型
COM对象内存布局,多继承是虚继承吗? 接口之间怎么切换? 1) 是普通的多继承,不是虚继承.因为父类接口只是含有纯虚函数,不含任何数据成员,所以问题不大. 2) QueryInterface可以用来 ...