算法(Algorithms)第4版 练习 1.5.22

package com.qiusongde;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdStats;

public class Exercise1522 {

    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 mean(int[] edges) {
        return StdStats.mean(edges);
    }

    public static void main(String[] args) {

        int T = Integer.parseInt(args[0]);

        int edgesQF[] = new int[T];
        int edgesQU[] = new int[T];

        double prevQF = timeTrialForQF(T, 125, edgesQF);
        double prevQU = timeTrialForWeiQU(T, 125, edgesQU);

        for(int N = 250; true; N += N) {

            double timeQF = timeTrialForQF(T, N, edgesQF);
            double timeQU = timeTrialForWeiQU(T, N, edgesQU);

            double meanQFConnect = mean(edgesQF);
            double meanQUconnect = mean(edgesQU);

            StdOut.printf("%6d %7.1f %7.1f %7.1f %7.1f\n", N, meanQFConnect, timeQF/prevQF, meanQUconnect, timeQU/prevQU);

            prevQF = timeQF;
            prevQU = 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 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 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   761.1     2.9   761.4     1.0
   500  1698.6     2.8  1717.8     5.2
  1000  3739.0     3.8  3737.7     2.0
  2000  8184.8     3.7  8174.3     2.1
  4000 17773.4     3.9 17799.1     2.2
  8000 38312.6     4.1 38229.6     2.2
时间: 2024-08-09 22:02:25

算法(Algorithms)第4版 练习 1.5.22的相关文章

1.3 Bags, Queues, and Stacks(算法 Algorithms 第4版)(一)

1.3.1 package com.qiusongde; import java.util.Iterator; import java.util.NoSuchElementException; import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdOut; public class FixedCapacityStackOfStrings implements Iterable<String> { privat

ubuntu命令行下java工程编辑与算法(第四版)环境配置

ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是出各种错误,特在此总结一下. ubuntu下java环境配置 由于网上教程比较多,而且也较全面,特此摆放一个链接,跟着此教程总就可以配置好oracle的java jdk,如果想更加省事,直接在命令行下键入java,会提示安装各种开源java jdk,只需要一个命令即可: sudo apt-get i

算法(第4版)PDF下载

网盘下载地址:算法(第4版)PDF下载 – 易分享电子书PDF资源网 作者: 塞奇威克 (Robert Sedgewick) / 韦恩 (Kevin Wayne) 出版社: 人民邮电出版社 原作名: Algorithms 4th edition 译者: 谢路云 出版年: 2012-10-1 页数: 636 定价: 99.00元 装帧: 平装 内容简介 · · · · · · 本书全面讲述算法和数据结构的必备知识,具有以下几大特色. ? 算法领域的经典参考书 Sedgewick畅销著作的最新版,反

排序算法总结(C语言版)

1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择排序 3.1     直接选择排序 3.2     堆排序 4.    归并排序 4.1     二路归并排序 4.2     自然合并排序 5.    分布排序 5.1     基数排序 1.插入排序 1.1      直接插入排序 将已排好序的部分num[0]~num[i]后的一个元素num[i+1]插入到之前已排好序的

算法(第四版)C#题解&mdash;&mdash;1.3.49 用 6 个栈实现一个 O(1) 队列

因为这个解法有点复杂,因此单独开一贴介绍. <算法(第四版)>中的题目是这样的: 1.3.49 栈与队列.用有限个栈实现一个队列,保证每个队列操作(在最坏情况下)都只需要常数次的栈操作. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识--用两个栈模拟一个队列 如何使用两个栈来模拟一个队列操作? 这是一道很经典的题目,答案也有很多种,这里只介绍之后会用到的一种方法. 首先我们有两个栈,H 和 T,分别

算法(第4版)-1.4.9 内存

总结:本小节讲述了Java的内存分配机制以及各种数据结构所使用的内存量. 重点: 1. 计算机中的电路很大一部分的作用就是帮助程序保存一些值并在稍后取出它们. 2. 计算机上的Java对内存的使用经过了精心的设计(程序的每个值在每次运行时所需的内存量都是一样的),但实现了Java的设备非常多,而内存的使用是和实现相关的. 3. 原始数据类型的常见内存.需求见算法(第4版)-1.1.2 原始数据类型与表达式. 4. · 对象本身的开销:16字节 · 对象的引用(内存地址):8字节 · 一般内存的使

“《算法》第4版第2章‘排序’”:初级排序算法(选择、冒泡、插入、希尔)

<算法>第4版作者是Robert Sedgewick 和 Kevin Wayne. 1. 选择排序 选择排序可以说是最简单的排序方法.首先,找到数组中最小的那个元素:其次,将它与数组的第一个元素交换位置(如果第一个元素就是最小元素,那么它就和自己交换):再次,在剩下的元素中找到最小的元素,将它与数组的第二个元素交换位置.如此往复,直到将整个数组排序. 该书中提出一个命题:对于长度为N的数组,选择排序需要大约N2/2次比较和N次交换.  程序如下: 1 void SelectionSort::s

“《算法》第四版第2章‘排序’”:初级排序算法

<算法>第4版作者是Robert Sedgewick 和 Kevin Wayne. 1. 选择排序 2. 冒泡排序 3. 插入排序 4. 希尔排序

《算法》第四版 IDEA 运行环境的搭建

<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程.我相信许多读者跟我一样,在学习Java的时候没有使用过命令行编译的形式去运行Java代码,直接使用Eclipse工具去进行开发的,因此,当看到书中 % java BinarySerach xxx.txt < xxx.txt 的时候,不免有点不知所措.笔者现在使用的IDE是IDEA,因此是想要在IDEA

算法导论 第三版 中文版

下载地址:网盘下载 算法导论 第三版 中文版 清晰 PDF,全书共8部分35章节,内容涵盖基础知识.排序和顺序统计量.数据结构.高级设计和分析技术.高级数据结构.图算法.算法问题选编.以及数学基础知识.非常实用的参考书和工程实践手册.此外,其他资源也已经上传,全部免费,欢迎大家下载! 第3版的主要变化 1.新增了van Emde Boas树和多线程算法,并且将矩阵基础移至附录. 2.修订了递归式(现在称为"分治策略")那一章的内容,更广泛地覆盖分治法. 3.移除两章很少讲授的内容:二项