1. package com.qiusongde;
  2.  
  3. import edu.princeton.cs.algs4.StdOut;
  4.  
  5. public class Exercise1523 {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. int T = Integer.parseInt(args[0]);
  10. int[] edgesQF = new int[T];
  11. int[] edgesQU = new int[T];
  12.  
  13. for(int N = 250; true; N += N) {
  14.  
  15. double timeQF = ErdosRenyi.timeTrialForQF(T, N, edgesQF);
  16. double timeQU = ErdosRenyi.timeTrialForQU(T, N, edgesQU);
  17.  
  18. double meanQFConnect = ErdosRenyi.mean(edgesQF);
  19. double meanQUconnect = ErdosRenyi.mean(edgesQU);
  20.  
  21. StdOut.printf("%6d %7.1f %7.1f %7.1f %7.1f\n", N, meanQFConnect, timeQF, meanQUconnect, timeQU);
  22.  
  23. }
  24.  
  25. }
  26.  
  27. }
  1. package com.qiusongde;
  2.  
  3. import edu.princeton.cs.algs4.StdOut;
  4. import edu.princeton.cs.algs4.StdRandom;
  5. import edu.princeton.cs.algs4.StdStats;
  6. import edu.princeton.cs.algs4.UF;
  7. import edu.princeton.cs.algs4.WeightedQuickUnionUF;
  8.  
  9. public class ErdosRenyi {
  10.  
  11. public static int countByUF(int N) {
  12.  
  13. int edges = 0;
  14. UF uf = new UF(N);
  15.  
  16. while (uf.count() > 1) {
  17. int i = StdRandom.uniform(N);
  18. int j = StdRandom.uniform(N);
  19. uf.union(i, j);
  20. edges++;
  21. }
  22.  
  23. return edges;
  24.  
  25. }
  26.  
  27. public static int countByQF(int N) {
  28.  
  29. int edges = 0;
  30. UFQuickFind uf = new UFQuickFind(N);
  31.  
  32. while (uf.count() > 1) {
  33. int i = StdRandom.uniform(N);
  34. int j = StdRandom.uniform(N);
  35. uf.union(i, j);
  36. edges++;
  37. }
  38.  
  39. return edges;
  40.  
  41. }
  42.  
  43. public static int countByWeiQUPathCom(int N) {
  44.  
  45. int edges = 0;
  46. UFWQuickUnionPathCom uf = new UFWQuickUnionPathCom(N);
  47.  
  48. while (uf.count() > 1) {
  49. int i = StdRandom.uniform(N);
  50. int j = StdRandom.uniform(N);
  51. uf.union(i, j);
  52. edges++;
  53. }
  54.  
  55. return edges;
  56.  
  57. }
  58.  
  59. public static int countByQU(int N) {
  60.  
  61. int edges = 0;
  62. UFQuickUnion uf = new UFQuickUnion(N);
  63.  
  64. while (uf.count() > 1) {
  65. int i = StdRandom.uniform(N);
  66. int j = StdRandom.uniform(N);
  67. uf.union(i, j);
  68. edges++;
  69. }
  70.  
  71. return edges;
  72.  
  73. }
  74.  
  75. public static int countByWeiQU(int N) {
  76.  
  77. int edges = 0;
  78. WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);
  79.  
  80. while (uf.count() > 1) {
  81. int i = StdRandom.uniform(N);
  82. int j = StdRandom.uniform(N);
  83. uf.union(i, j);
  84. edges++;
  85. }
  86.  
  87. return edges;
  88.  
  89. }
  90.  
  91. public static double timeTrialForQF(int T, int N, int[] edges) {
  92.  
  93. Stopwatch timer = new Stopwatch();
  94.  
  95. // repeat the experiment T times
  96. for (int t = 0; t < T; t++) {
  97. edges[t] = ErdosRenyi.countByQF(N);
  98. }
  99.  
  100. return timer.elapsedTime();
  101.  
  102. }
  103.  
  104. public static double timeTrialForWeiQU(int T, int N, int[] edges) {
  105.  
  106. Stopwatch timer = new Stopwatch();
  107.  
  108. // repeat the experiment T times
  109. for (int t = 0; t < T; t++) {
  110. edges[t] = ErdosRenyi.countByWeiQU(N);
  111. }
  112.  
  113. return timer.elapsedTime();
  114.  
  115. }
  116.  
  117. public static double timeTrialForQU(int T, int N, int[] edges) {
  118.  
  119. Stopwatch timer = new Stopwatch();
  120.  
  121. // repeat the experiment T times
  122. for (int t = 0; t < T; t++) {
  123. edges[t] = ErdosRenyi.countByQU(N);
  124. }
  125.  
  126. return timer.elapsedTime();
  127.  
  128. }
  129.  
  130. public static double mean(int[] edges) {
  131. return StdStats.mean(edges);
  132. }
  133.  
  134. public static void main(String[] args) {
  135.  
  136. int n = Integer.parseInt(args[0]); // number of vertices
  137. int trials = Integer.parseInt(args[1]); // number of trials
  138. int[] edges = new int[trials];
  139.  
  140. // repeat the experiment trials times
  141. for (int t = 0; t < trials; t++) {
  142. edges[t] = countByUF(n);
  143. }
  144.  
  145. // report statistics
  146. StdOut.println("1/2 n ln n = " + 0.5 * n * Math.log(n));
  147. StdOut.println("mean = " + StdStats.mean(edges));
  148. StdOut.println("stddev = " + StdStats.stddev(edges));
  149. }
  150.  
  151. }
  1. package com.qiusongde;
  2.  
  3. public class Stopwatch {
  4.  
  5. private final long start;
  6.  
  7. public Stopwatch() {
  8. start = System.currentTimeMillis();
  9. }
  10.  
  11. public double elapsedTime() {
  12. long now = System.currentTimeMillis();
  13. return (now - start) / 1000.0;
  14. }
  15.  
  16. }

结果:

  1. 250 854.0 0.0 738.0 0.0
  2. 500 1554.0 0.0 1832.0 0.0
  3. 1000 3291.0 0.0 3616.0 0.0
  4. 2000 10467.0 0.0 7477.0 0.0
  5. 4000 17488.0 0.0 15609.0 0.0
  6. 8000 41016.0 0.0 37187.0 0.1
  7. 16000 70888.0 0.2 74066.0 0.8
  8. 32000 166090.0 0.9 153517.0 4.1
  9. 64000 370711.0 3.5 333206.0 19.4
  10. 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. [译]GLUT教程 - 弹出菜单基础

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> Popup Menus 弹出菜单也是GLUT的一部分.虽然 ...

  2. leetCode(37):Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. 深入了解Erlang 垃圾回收机制以及其重要性(转)

    声明:本片文章是由Hackernews上的[Erlang Garbage Collection Details and Why ItMatters][1]编译而来,本着学习和研究的态度,进行的编译,转 ...

  4. python操作Excel读写--使用xlrd (转)

    (转自:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html) 一.安装xlrd模块 到python官网下载http://pypi ...

  5. 转载 iOS全局检测网络变化的实时状态

      昨天浏览了cocoaChina,发现了一遍文章是优化Reachablity框架的出来的检测网络类,大家都知道这个Reachablity框架是用来检测网络变化的!但是也是有一点bug,事实上,基于此 ...

  6. python并发编程之多进程、多线程、异步和协程

    一.多线程 多线程就是允许一个进程内存在多个控制权,以便让多个函数同时处于激活状态,从而让多个函数的操作同时运行.即使是单CPU的计算机,也可以通过不停地在不同线程的指令间切换,从而造成多线程同时运行 ...

  7. lumen手记:自定义Validate表单验证

    版权声明:本文为博主原创文章,未经博主允许不得转载. 今天开始跳lumen的表单验证Validate类的坑,确实好坑!!! 首先,lumen的表单验证返回是无状态的json格式api,这... 所有开 ...

  8. Python的Django框架中的Context使用

    Python的Django框架中的Context使用 近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它. 一个context是一系列变 ...

  9. python 快速排序详述

    快速排序是对“冒泡排序”的优化算法,都属于交换排序类. 描述:它通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据要小,然后再按此方法对这两部分数据分别进行快速 ...

  10. Pipeline模式(netty源码死磕6)

    精进篇:netty源码死磕6  巧夺天工--Pipeline模式揭秘 1. 巧夺天工--Pipeline模式揭秘 1.1. Pipeline模式简介 管道的发名者叫,Malcolm Douglas M ...