AlgorithmsI Exercises: Analysis of Algorithms
Question 1
Suppose that you time a program as a function of N and produce
the following table.
N   seconds
-------------------
     1024     0.000
     2048     0.001
     4096     0.007
     8192     0.029
    16384     0.121
    32768     0.519
    65536     2.156
   131072     9.182
   262144    38.784
   524288   164.585
  1048576   698.592
  2097152  2958.141
Estimate the order of growth of the running time as a function of N.
Assume that the running time obeys a power law T(N) ~ a N^b. For your
answer, enter the constant b. Your answer will be marked as correct
if it is within 1% of the target answer - we recommend using
two digits after the decimal separator, e.g., 2.34.
Answer: 2.08
Question Explanation
The theoretical order-of-growth is N ^ (25/12) = 2.08
The empirical order-of-growth is N ^ (log_2 ratio)
log_2
        N   seconds     ratio     ratio
---------------------------------------
     1024     0.000         -         -
     2048     0.001         -         -
     4096     0.007      7.00      2.81
     8192     0.029      4.14      2.05
    16384     0.121      4.17      2.06
    32768     0.519      4.29      2.10
    65536     2.156      4.15      2.05
   131072     9.182      4.26      2.09
   262144    38.784      4.22      2.08
   524288   164.585      4.24      2.09
  1048576   698.592      4.24      2.09
  2097152  2958.141      4.23      2.08
Question 2
What is the order of growth of the worst case running time of the following code fragment
as a function of N?
int sum = 0;
for (int i = 0; i < N; i++)
    for (int j = i+1; j < N; j++)
            for (int k = j+1; k < N; k++)
                  for (int h = k+1; h < N; h++)
                         sum++;
Answer: N^4
int sum = 0;
for (int i = N*N; i > 1; i = i/2)
     sum++;
Answer: logN
The i loops iterates ~ lg (N^2) ~ 2 lg N times.
int sum = 0;
for (int i = 1; i <= N*N; i = i*2)
for (int j = 0; j < i; j++)
sum++;
Answer: N^2
The body of the innermost loop executes 1 + 2 + 4 + 8 + ... + N^2 ~ 2 N^2 times.
Question 3
Given the following definition of a MysteryBox object:
public class MysteryBox {
    private final long x0, x1, x2, x3;
    private final double y0, y1, y2, y3;
    private final boolean z0;
    private final int[] a = new int[320];
...
}
Using the 64-bit memory cost model from lecture, how many bytes does
each object of type MysteryBox use? Include all memory allocated when the
client calls new MysteryBox().
Answer: 1400
Question Explanation:
The correct answer is: 1400
public class MysteryBox {                           //   16 (object overhead)
    private final long x0, x1, x2, x3;              //   32 (4 long)
    private final double y0, y1, y2, y3;            //   32 (4 double)
    private final boolean z0;                       //    1 (1 boolean)
    private final int[] a = new int[320];           //    8 (reference to array)
                                                    // 1304 (int array of size 320)
    ...                                                   7 (padding to round up to a multiple of 8)
}                                                      ----
                                                       1400
AlgorithmsI Exercises: Analysis of Algorithms的更多相关文章
- 算法分析 Analysis of Algorithms -------GeekforGeeker 翻译
		
算法分析 Analysis of Algorithms 为什么要做性能分析?Why performance analysis? 在计算机领域有很多重要的因素我们要考虑 比如用户友好度,模块化, 安全性 ...
 - 6.046 Design and Analysis of Algorithms
		
课程信息 6.046 Design and Analysis of Algorithms
 - "Mathematical Analysis of Algorithms" 阅读心得
		
"Mathematical Analysis of Algorithms" 阅读心得 "Mathematical Analysis of Algorithms" ...
 - 《Mathematical Analysis of Algorithms》中有关“选择第t大的数”的算法分析
		
开头废话 这个问题是Donald.E.Knuth在他发表的论文Mathematical Analysis of Algorithms中提到的,这里对他的算法分析过程给出了更详细的解释. 问题描述: 给 ...
 - 612.1.002 ALGS4 | Analysis of Algorithms
		
我们生活在大数的时代 培养数量级的敏感! Tip:见招拆招 作为工程师,你先要能实现出来. 充实基础,没有什么不好意思 哪怕不完美.但是有时候完成比完美更重要. 之后再去想优化 P.S.作者Rober ...
 - Analysis of Algorithms
		
算法分析 Introduction 有各种原因要求我们分析算法,像预测算法性能,比较不同算法优劣等,其中很实际的一条原因是为了避免性能错误,要对自己算法的性能有个概念. 科学方法(scientific ...
 - Analysis of algorithms: observation
		
例子: 3-Sum 给定N个整数,这里面有多少个三元组,使其三个整数相加为0,如上面的例子为有4个三元组. 这个问题是许多问题如计算机几何,图形学等的基础. 用简单粗暴的方式来解决3-Sum问题 通过 ...
 - Time complexity analysis of algorithms
		
时间复杂性的计算一般而言,较小的问题所需要的运行时间通常要比较大的问题所需要的时间少.设一个程序P所占用的时间为T,则 T(P)=编译时间+运行时间. 编译时间与实例特征是无关的,且可假设一个编译过的 ...
 - AlgorithmsI Exercises: UnionFind
		
Question1 Give the id[] array that results from the following sequence of 6 unionoperations on a set ...
 
随机推荐
- java中的mmap实现--转
			
什么是mmap mmap对于c程序员很熟悉,对于java程序员有点陌生.简而言之,将文件直接映射到用户态的内存地址,这样对文件的操作不再是write/read,而是直接对内存地址的操作. 在c中提供了 ...
 - Understanding JTS--reference
			
Part I-An introduction to transactions If you look at any introductory article or book on J2EE, you' ...
 - Android(java)学习笔记212:中文乱码的问题处理(qq登录案例)
			
1.我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed&q ...
 - 论前端css初始化的重要性
			
新手,求喷,刚刚知道每个浏览器都有对 标签的初始化,就造成我们网站开发者开发的web程序,会在不同的网站上有不同的样式风格,这给用户带来了很不好的体验,这也是浏览器本身的原因造成的,这时候,我们不可能 ...
 - HTML5 FileReader读取Blob对象API详解
			
使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...
 - javascript google map circle radius_changed ,angularjs google map circle radius_changed
			
javascript: var cityCircle = new google.maps.Circle({ strokeColor: '#FF0000', strokeOpacity: 0.8, st ...
 - >/dev/null 2>&1 这句话的含义
			
1表示标准输出,2表示标准错误输出 2>&1表示将标准错误输出重定向到标准输出,这样,程序或者命令的正常输出和错误输出就可以在标准输出输出(也就是一起输出). 一般来讲标准输出和标准错误 ...
 - Plsql工具单步调试 存储过程或是 函数(oracle数据库)-留着自己用的
			
<案例1> 原地址: http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非 ...
 - winform(C#)拖拽实现获得文件路径
			
设置Form的AllowDrop为true private void Form1_DragDrop(object sender, DragEventArgs e) { ...
 - ajax jsonp 原理 以及对数据的处理
			
ajax请求 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xml ...