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
时间: 2024-10-13 16:19:20

AlgorithmsI Exercises: Analysis of Algorithms的相关文章

《Mathematical Analysis of Algorithms》中有关“选择第t大的数”的算法分析

开头废话 这个问题是Donald.E.Knuth在他发表的论文Mathematical Analysis of Algorithms中提到的,这里对他的算法分析过程给出了更详细的解释. 问题描述: 给定一个数组a[1,2,...,n],用尽量少的比较次数找出数组中第t大的数.(假定这n个数两两不同). 算法描述: 对于这个问题,可以很容易想到对应的算法.一个 \(O(n\log n)\) 的排序算法总能解决问题(然鹅今天我们并不对数组进行完全的排序). 参照快速排序中的Partition操作,将

Analysis of algorithms: observation

例子: 3-Sum 给定N个整数,这里面有多少个三元组,使其三个整数相加为0,如上面的例子为有4个三元组. 这个问题是许多问题如计算机几何,图形学等的基础. 用简单粗暴的方式来解决3-Sum问题 通过三个for循环来执行 那么怎么计算它运行的时间呢? Java有一个内嵌的函数来计算运行时间:Stopwatch()           我们通过对不同的input size来运行获得运行时间,如上图所示,那么根据这些已得出来的值,我们可以推测出当input size为8K时,它的运行时间为多少吗?

612.1.002 ALGS4 | Analysis of Algorithms

我们生活在大数的时代 培养数量级的敏感! Tip:见招拆招 作为工程师,你先要能实现出来. 充实基础,没有什么不好意思 哪怕不完美.但是有时候完成比完美更重要. 之后再去想优化 P.S.作者Robert Sedgewick的导师是Knuth(高德纳!) Conclusion First 1.Running Time Operation table 2.Memory 1 SOP - Analysis 2 Observations Measuring the running time - autom

Analysis of algorithms: introduction

一系列的人物角色 Programmer,client,theoretician和blocking 学生可能会承担里面的一个或者多个角色 Running time 提出running time这个概念的可能要追溯到很远的时候,那时通过analytic engine来计算how many times turn lthe crank(手柄) 为什么要分析算法 一些高效算法的例子    快速傅里叶变换:NlogN而不是N2,快速傅里叶变换应用在很多领域(DVD,JPEG...) N-body simul

AlgorithmsI Exercises: UnionFind

Question1 Give the id[] array that results from the following sequence of 6 unionoperations on a set of 10 items using the quick-find algorithm. 6-3 2-3 5-3 5-1 9-3 3-0 Your answer should be a sequence of 10 integers, separated by whitespace.Recall:

analysis of algorithms

1. 2. binary search (sorted array) 给定查找对象,array,以及最大最小的范围:将查找对象与middle作比较,进而改变最大最小的范围,然后调用递归 时间复杂度的计算要考虑最坏的情况,本题中最坏的情况类比一条面包每天吃一半几天吃完的问题,时间复杂度为log以2为底n的对数 O(log2 n)可以直接写成O(log n),与底数无关(换底公式可以将它们换成统一的底) 3. array的缺点有两个,第一个是fixed size,所以一开始就要给所有元素预留出足够的

Analysis of Algorithms--preface

Analysis of Algorithms: First part of the course is focused on analysis. Second part of the course is focused on design. The analysis of algorithm is the theoretical study.(算法分析是理论研究) The theoretical study of computer-program performance and resource

A Software Engineer’s Adventures In Learning Mathematics

1. Reference https://medium.com/@warrenhenning/a-software-engineers-adventures-in-learning-mathematics-62140c59e5c 2. Detail A Software Engineer’s Adventures In Learning Mathematics What we hope ever to do with ease, we must first learn to do with di

[it-ebooks]电子书列表

#### it-ebooks电子书质量不错,但搜索功能不是很好 #### 格式说明  [ ]中为年份      ||  前后是标题和副标题  #### [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/ Learning Web App Developmen