算法(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

转载于:https://www.cnblogs.com/songdechiu/p/6569060.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值