zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    遍历指定目录及其子目录下所有文件
    vim 配置
    解决 Mendeley Linux 中文输入问题
    全角半角字符对照表
    chrome 替换多线程下载管理器
    查看系统日志
    中大东校区iNode For linux 配置笔记
    anaconda 虚拟环境笔记
    linux 网络操作
    deepin 装机
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4837063.html
Copyright © 2011-2022 走看看