算法(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搭建的教程 ...
随机推荐
- spring boot 集成mybatis报错
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of ...
- Java内存模型FAQ(一) 什么是内存模型
原文:http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html 第一章 译者:方腾飞 在多核系统中,处理器一般有一层或者多层的缓存,这 ...
- oracle海量数据中提升创建索引的速度
基本信息情况: 数据库版本:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production 操作系统版本:Ce ...
- iOS GCD倒计时
GCD倒计时的好处在于不用考虑是否定时器无法释放的问题,runloop的问题,还有精度更加高 使用GCD创建定时器方法 -(void)startCountDown:(NSInteger)maxTime ...
- 【Selenium + Python】自动化测试之发送邮件正文以及附件同时发送
废话不多说,直接上代码: import unittest import time import os import smtplib from HTMLTestRunner import HTMLTes ...
- android 布局特点
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_cont ...
- 44.Android MD5Util
44.Android MD5Util public class MD5Util { public static String getMD5String(String key) { char hexDi ...
- CrtmpServer支持Android与IOS进行RTMP直播遇到的_checkbw问题
在进行移动端视频直播项目时遇到的问题.手机端在推的流时的是没问题的,主要如今是IOS和安卓连接CRtmpServer后进行播放时checkBW过不了,出现异常:NetConnection.Call.F ...
- 已备份数据库的磁盘结构版本号为611,server支持版本号为539,无法还原或升级数据库
提供的是bak文件是2005备份的,还原到本地的sqlserver2000,提示:已备份数据库的磁盘上结构版本号为611.服务器支持版本号539,无法还原或升级数据库. 网上找了下,原因是611是sq ...
- 【BZOJ1899】[Zjoi2004]Lunch 午餐 贪心+DP
[BZOJ1899][Zjoi2004]Lunch 午餐 Description 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时 ...