算法(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",
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
算法(Algorithms)第4版 练习 1.5.24的更多相关文章
- 1.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——2.4
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更方便的版本见:https ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
随机推荐
- 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!
[COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...
- freemarker在xml文件中遍历list数据
delete from pub_channelpackage where channelcode = :channelcode and channeltype = :channeltype ...
- Winform 动态 画图 不闪
一.问题:解决winform动态画图闪的问题,网上搜的方法,大部分都是: “this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlSty ...
- Log4J 基本使用
Log4j由三个重要的组件 构 成:日志 信息 的优先级,日志信息的输出目的地,日志信息的输出格式. 日志信息的优先级 从高到低有ERROR . WARN . INFO . DEBUG ,分别用来指定 ...
- 【Python + selenium】之BSTestRunner:NameError: name 'unicode' is not defined
参考文章:<python3.6 :NameError: name 'unicode' is not defined>
- php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false
php 在linux 用file_exists() 函数判断 另外一台服务器映射过来的文件是否存在 总是返回false .如下案例 $type="android"; $url=&q ...
- jquery基础研究学习【HTML】
jQuery HTMLjQuery 捕获jQuery 设置jQuery 添加元素jQuery 删除元素jQuery CSS 类jQuery css() 方法jQuery 尺寸 笔记:
- linux下nginx php配置redis
之前一直遇到,Module compiled with module API=20090626这个坑问题!!! NOTICE: PHP message: PHP Warning: PHP Star ...
- cocoapods 错误处理
error: RPC failed; curl 56 SSLRead() return error -36 [!] /usr/bin/git clone https://github.com/Coco ...
- iOS-Core-Animation-Advanced-Techniques(二)
本文转载至 http://www.cocoachina.com/ios/20150104/10816.html 视觉效果和变换 (四)视觉效果 嗯,园和椭圆还不错,但如果是带圆角的矩形呢? 我们现在能 ...