算法(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
算法(Algorithms)第4版 练习 1.5.22的更多相关文章
- 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# 习题题解——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搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- GreenDao学习
官方文档地址:http://greenrobot.org/greendao/documentation//introduction/ 英语不好的可以使用谷歌翻译,很轻松的看懂文档,不需要FQ. 看不懂 ...
- 002android初级篇之ViewPager及PagerSlidingTabStrip listview的使用
002android初级篇之ViewPager及PagerSlidingTabStrip listview的使用 ViewPager ViewPager类直接继承了ViewGroup类,所有它是一个容 ...
- iOS中获取系统相册中的图片
一.获取单张图片 思路: 1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片 2.设置代理,遵守代理协议 注意这个UIImagePickerContr ...
- iOS 企业版 安装失败 原因
首先要吐槽下国内的论坛水分略多,以下问题大多是查询stackoverflow等论坛解决的.推荐一款软件,Log Guru,用来查看app安装时的系统日志,很多问题要看日志才知道错误点. 1.首先有几 ...
- OKR与KPI管理的区别与联系
OKR是一种新兴的管理体系,最近几年被引进中国.由于在IT.互联网.金融.游戏等知识密集型企业中有着显著的效果,得到中国企业的认可. OKR是英文Objectives & Key Result ...
- MFC添加菜单资源与菜单执行函数的两种命令形式
添加资源->新建一个菜单资源->选择相应的对话框 菜单的执行函数命令形式: COMMAD 是指点击菜单后的执行命令 UPDATE_COMMAND_UI 是指点击菜单后菜单状态的函数
- 4 Values whose Sum is 0(二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 21370 Accep ...
- Mybatis的动态SQL实现
一.动态SQL简介 MyBatis的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的 ...
- ElasticSearch(十七)初识倒排索引
现在有两条document: doc1:I really liked my small dogs, and I think my mom also liked them. doc2:He never ...
- python中TCP和UDP区别
TCP(Transmission Control Protocol)可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TCP的应用:Web浏览 ...