博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Algs4-1.5.22Erods-renyi模型的倍率实验
阅读量:7262 次
发布时间:2019-06-29

本文共 2885 字,大约阅读时间需要 9 分钟。

1.5.22Erods-renyi模型的倍率实验。开发一个性能测试用例 ,从命令行接受一个int值T并进行T次以下实验:使用练习1.5.17的用例生成随机连接,和我们的开发用例一样使用UnionFind来检查触点的连通性,不断循环直到所有触点均相互连通。对于每个N,0打印出N值和平均所需的连接数以及前后两次运行时间的比值。使用你的程序验证正文中的猜想:quick-find算法和quick-union算法的运行时间是平方级别的,加权quick-union算法则接近线性级别。

答:
图片
public class E1d5d22
{
    public static int countOfQuickFind(int N)
    {
        int Times=0;
        QuickFindUF uf=new QuickFindUF(N);
         while (uf.count()>1)
           {
               int p=StdRandom.uniform(N);
               int q=StdRandom.uniform(N);
               Times++;
               uf.union(p,q);
            }//end while
        return Times;
    }
   
     public static int countOfQuickUnion(int N)
    {
        int Times=0;
        QuickUnionUF uf=new QuickUnionUF(N);
        while (uf.count()>1)
           {
               int p=StdRandom.uniform(N);
               int q=StdRandom.uniform(N);
               Times++;
               uf.union(p,q);
            }//end while
        return Times;
    }
    
   public static int countOfWeightedQuickUnion(int N)
    {
        int Times=0;
        WeightedQuickUnionUF uf=new WeightedQuickUnionUF(N);
        while (uf.count()>1)
           {
               int p=StdRandom.uniform(N);
               int q=StdRandom.uniform(N);
               Times++;
               uf.union(p,q);
            }//end while
        return Times;
    }
         
   public static void DoubleTestQuickFind(int T)
   {
        int totalConnections=0;
        double lastElapsedTime=0;
        double currentElapsedTime=0;
        StdOut.println("---QuickFind---");
        for(int N=2;N<=Math.pow(2,15);N=N+N)
            {
               Stopwatch timer=new Stopwatch();
               totalConnections=0;
               for (int t=1;t<=T;t++)
                 {
                   totalConnections=totalConnections+countOfQuickFind(N);
                 }
              currentElapsedTime=timer.elapsedTime();
              StdOut.printf("N=%6d  avg Connection=%8d  time=%8.2f   Rate=%5.2f\n",N,totalConnections/T,currentElapsedTime,currentElapsedTime/lastElapsedTime);
              lastElapsedTime=currentElapsedTime;
            }
   }
  
  
   public static void DoubleTestQuickUnion(int T)
   {
        int totalConnections=0;
        double lastElapsedTime=0;
        double currentElapsedTime=0;
        StdOut.println("---QuickUnion---");
        for(int N=2;N<=Math.pow(2,15);N=N+N)
            {
               Stopwatch timer=new Stopwatch();
               totalConnections=0;
               for (int t=1;t<=T;t++)
                 {
                   totalConnections=totalConnections+countOfQuickUnion(N);
                 }
              currentElapsedTime=timer.elapsedTime();
              StdOut.printf("N=%6d  avg Connection=%8d  time=%8.2f   Rate=%5.2f\n",N,totalConnections/T,currentElapsedTime,currentElapsedTime/lastElapsedTime);
              lastElapsedTime=currentElapsedTime;
            }
   }
  
   public static void DoubleTestWeightedQuickUnion(int T)
   {
        int totalConnections=0;
        double lastElapsedTime=0;
        double currentElapsedTime=0;
        StdOut.println("---WeightedQuickUnion---");
        for(int N=2;N<=Math.pow(2,15);N=N+N)
            {
               Stopwatch timer=new Stopwatch();
               totalConnections=0;
               for (int t=1;t<=T;t++)
                 {
                   totalConnections=totalConnections+countOfWeightedQuickUnion(N);
                 }
              currentElapsedTime=timer.elapsedTime();
              StdOut.printf("N=%6d  avg Connection=%8d  time=%8.2f   Rate=%5.2f\n",N,totalConnections/T,currentElapsedTime,currentElapsedTime/lastElapsedTime);
              lastElapsedTime=currentElapsedTime;
            }
   }
  
    public static void main(String[] args)
    {
        int T=Integer.parseInt(args[0]);
       DoubleTestQuickFind(T);
       DoubleTestQuickUnion(T);
       DoubleTestWeightedQuickUnion(T);
    }
}

转载于:https://www.cnblogs.com/longjin2018/p/9854853.html

你可能感兴趣的文章
React Native项目自动化打包发布
查看>>
iOS 部署智能合约
查看>>
kafka生产者Producer参数设置及参数调优建议-kafka 商业环境实战
查看>>
2018.06.27 Day01 牛客网java刷题
查看>>
Python正则表达式初识(九)
查看>>
说说 Vue.js 中的 v-cloak 指令
查看>>
Python微信公众号开发
查看>>
react-native 开发前的见面礼 -- 误入依赖坑
查看>>
手把手带你实现一个符合promiseA+规范的promise类库
查看>>
Rx中的那些操作符
查看>>
Kotlin的基本语法和类型
查看>>
聊聊iOS中的多继承和多重代理
查看>>
1.Ajax基础知识及核心原理
查看>>
Okhttp源码阅读(二)——一个缓存是怎么触发的
查看>>
Vuecli3开发环境搭建
查看>>
Go语言学习(1) - 简介
查看>>
【刘文彬】EOS商业落地利器:多签名操作与应用
查看>>
Nginx转发TCP实现负载均衡
查看>>
使用nodejs搭建HTTPS server
查看>>
开发交易所平台系统撮合周期费用
查看>>