zoukankan      html  css  js  c++  java
  • 算法(Algorithms)第4版 练习 1.5.24

    package com.qiusongde;
    
    import edu.princeton.cs.algs4.StdOut;
    
    public class Exercise1524 {
    
        public static void main(String[] args) {
            
            int T = Integer.parseInt(args[0]);
            int[] edgesWeiQU = new int[T];
            int[] edgesWeiQUPath = new int[T];
            
            for(int N = 250; true; N += N) {
                
                double timeWeiQU = ErdosRenyi.timeTrialForWeiQU(T, N, edgesWeiQU);
                double timeWeiQUPath = ErdosRenyi.timeTrialForWeiQUPath(T, N, edgesWeiQUPath);
                
                double meanWeiQUconnect = ErdosRenyi.mean(edgesWeiQU);
                double meanWeiQUPathconnect = ErdosRenyi.mean(edgesWeiQUPath);
                
                StdOut.printf("%6d %7.1f %7.1f %7.1f %7.1f
    ",
                        N, meanWeiQUconnect, timeWeiQU, meanWeiQUPathconnect, timeWeiQUPath);
            }
        
        }
    
    }
    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;
            UFWeightedQuickUnion uf = new UFWeightedQuickUnion(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 countByWeiQUPath(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 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 timeTrialForWeiQUPath(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.countByWeiQUPath(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   765.1     0.0   770.4     0.0
       500  1723.7     0.1  1700.9     0.1
      1000  3763.9     0.2  3729.4     0.1
      2000  8159.1     0.3  8281.4     0.2
      4000 17678.9     0.7 17976.7     0.5
      8000 38155.4     1.6 38141.0     1.1
     16000 81985.7     3.6 82095.1     2.6
     32000 175095.7     8.1 173558.2     5.5
     64000 373984.6    19.1 372914.1    12.7
    128000 793368.9    48.3 786458.0    30.8
  • 相关阅读:
    无声购票弹窗
    C#多线程与异步的区别
    关于adb驱动
    事务日志初探(二)---简单恢复模式
    预写式日志(Write-Ahead Logging (WAL))
    如果正确读取SQL Server中的扩展事件?
    索引初探(三)
    事务日志还原的次意外的操作失误
    索引初探(二)
    索引的初探(一)
  • 原文地址:https://www.cnblogs.com/songdechiu/p/6572711.html
Copyright © 2011-2022 走看看