package com.qiusongde;

import edu.princeton.cs.algs4.StdOut;

public class Exercise1523 {

    public static void main(String[] args) {

        int T = Integer.parseInt(args[0]);
int[] edgesQF = new int[T];
int[] edgesQU = new int[T]; for(int N = 250; true; N += N) { double timeQF = ErdosRenyi.timeTrialForQF(T, N, edgesQF);
double timeQU = ErdosRenyi.timeTrialForQU(T, N, edgesQU); double meanQFConnect = ErdosRenyi.mean(edgesQF);
double meanQUconnect = ErdosRenyi.mean(edgesQU); StdOut.printf("%6d %7.1f %7.1f %7.1f %7.1f\n", N, meanQFConnect, timeQF, meanQUconnect, timeQU); } } }
package com.qiusongde;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.UF;
import edu.princeton.cs.algs4.WeightedQuickUnionUF; public class ErdosRenyi { public static int countByUF(int N) { int edges = 0;
UF uf = new UF(N); while (uf.count() > 1) {
int i = StdRandom.uniform(N);
int j = StdRandom.uniform(N);
uf.union(i, j);
edges++;
} return edges; } public static int countByQF(int N) { int edges = 0;
UFQuickFind uf = new UFQuickFind(N); while (uf.count() > 1) {
int i = StdRandom.uniform(N);
int j = StdRandom.uniform(N);
uf.union(i, j);
edges++;
} return edges; } public static int countByWeiQUPathCom(int N) { int edges = 0;
UFWQuickUnionPathCom uf = new UFWQuickUnionPathCom(N); while (uf.count() > 1) {
int i = StdRandom.uniform(N);
int j = StdRandom.uniform(N);
uf.union(i, j);
edges++;
} return edges; } public static int countByQU(int N) { int edges = 0;
UFQuickUnion uf = new UFQuickUnion(N); while (uf.count() > 1) {
int i = StdRandom.uniform(N);
int j = StdRandom.uniform(N);
uf.union(i, j);
edges++;
} return edges; } public static int countByWeiQU(int N) { int edges = 0;
WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N); while (uf.count() > 1) {
int i = StdRandom.uniform(N);
int j = StdRandom.uniform(N);
uf.union(i, j);
edges++;
} return edges; } public static double timeTrialForQF(int T, int N, int[] edges) { Stopwatch timer = new Stopwatch(); // repeat the experiment T times
for (int t = 0; t < T; t++) {
edges[t] = ErdosRenyi.countByQF(N);
} return timer.elapsedTime(); } public static double timeTrialForWeiQU(int T, int N, int[] edges) { Stopwatch timer = new Stopwatch(); // repeat the experiment T times
for (int t = 0; t < T; t++) {
edges[t] = ErdosRenyi.countByWeiQU(N);
} return timer.elapsedTime(); } public static double timeTrialForQU(int T, int N, int[] edges) { Stopwatch timer = new Stopwatch(); // repeat the experiment T times
for (int t = 0; t < T; t++) {
edges[t] = ErdosRenyi.countByQU(N);
} return timer.elapsedTime(); } public static double mean(int[] edges) {
return StdStats.mean(edges);
} public static void main(String[] args) { int n = Integer.parseInt(args[0]); // number of vertices
int trials = Integer.parseInt(args[1]); // number of trials
int[] edges = new int[trials]; // repeat the experiment trials times
for (int t = 0; t < trials; t++) {
edges[t] = countByUF(n);
} // report statistics
StdOut.println("1/2 n ln n = " + 0.5 * n * Math.log(n));
StdOut.println("mean = " + StdStats.mean(edges));
StdOut.println("stddev = " + StdStats.stddev(edges));
} }
package com.qiusongde;

public class Stopwatch {

    private final long start;

    public Stopwatch() {
start = System.currentTimeMillis();
} public double elapsedTime() {
long now = System.currentTimeMillis();
return (now - start) / 1000.0;
} }

结果:

   250   854.0     0.0   738.0     0.0
500 1554.0 0.0 1832.0 0.0
1000 3291.0 0.0 3616.0 0.0
2000 10467.0 0.0 7477.0 0.0
4000 17488.0 0.0 15609.0 0.0
8000 41016.0 0.0 37187.0 0.1
16000 70888.0 0.2 74066.0 0.8
32000 166090.0 0.9 153517.0 4.1
64000 370711.0 3.5 333206.0 19.4
128000 698381.0 16.3 884453.0 233.5

比较奇怪的是Quick-union的运行时间比Quick-find的还要久。

算法(Algorithms)第4版 练习 1.5.23的更多相关文章

  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. iOS SDWebImage Error Domain=NSURLErrorDomain Code=-1202 “此服务器的证书无效

    sdwebImage 加载网络图片的时候,如果使用的https证书未经过认证,或者证书有问题,会出现Error Domain=NSURLErrorDomain Code=-1202 "此服务 ...

  2. [译]GLUT教程 - 整合代码2

    Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far II 以下是前面几节的完整整合代码: # ...

  3. android常用权限

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...

  4. nginx proxy_pass 里的”/”

    见配置,摘自nginx.conf 里的server 段: server { listen 80; server_name abc.163.com ; location / { proxy_pass h ...

  5. Harbor的搭建(vmware企业级docker镜像私服)

    1.下载harbor,地址https://github.com/vmware/harbor2.进入harbor-master/Deploy目录,修改harbor.cfg文件,主要修改以下信息      ...

  6. Docker-Compose 自动创建的网桥与局域网冲突解决方案

    环境: 使用docker-compose.yml 部署应用,docker 默认的网络模式是bridge ,默认网段是172.17.0.1/16  ,不巧的是我们局域网也使用的172.22. xx 网段 ...

  7. STL源代码分析--萃取编程(traits)技术的实现

    1.为什么要出现? 依照默认认定.一个模板给出了一个单一的定义,能够用于用户能够想到的不论什么模板參数!可是对于写模板的人而言,这样的方式并不灵活.特别是遇到模板參数为指针时,若想实现与类型的參量不一 ...

  8. Lumen Repository(仓储)

    在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离:http://laravelacademy.org/post/3063.html Eloquent: 集合:ht ...

  9. OKR与KPI管理的区别与联系

    OKR是一种新兴的管理体系,最近几年被引进中国.由于在IT.互联网.金融.游戏等知识密集型企业中有着显著的效果,得到中国企业的认可. OKR是英文Objectives & Key Result ...

  10. 【BZOJ3309】DZY Loves Math 莫比乌斯反演+线性筛(好题)

    [BZOJ3309]DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10 ...