LAB 5            Fall 2001            Michael Siff      

Measuring the time complexity of algorithms

Lab Report due Tuesday, October 30

Introduction

The goal of this lab is to learn more about the efficiency of algorithms. In particular, you will experiment to:

For the lab report, you will need to produce graphs. I prefer that you do this by hand on graph paper, but you can use tools to assist you. If you do use tools, such as Excel, etc. please indicate what they are and how you used them. To produce graphs, the y-axis should represent time, the x-axis should represent the size or value of the inputs.

It is often the case that a computer can execute an algorithm so quickly that it seems to take no time at all. To improve our ability to measure the time it takes for an algorithm to execute, several of the exercises below involve programs that can be set to execute the same algorithm with the same input repeatedly for a specified number of iterations. Thus, you may not be able to measure how long it takes to multiply two numbers together once. But if you repeat the process a 1000 times, a non-zero time will be reported, and you can simply divide that time by 1000 to get an estimate for how long it takes to execute a single multiplication operation.

  1. The sum from 1 to n

    In class we discussed two different methods of summing the integers between 1 and n. The first, obvious method is to literally add up:

      1 + 2 + 3 + ... + n
    
    as illustrated by this algorithm:
      input n
      i <- 1
      s <- 0
      while (i <= n) do
        s <- s + i
        i <- i + 1
      output s
    
    The second, less obvious technique (due to Gauss, among others) is to realize that:
      1 + 2 + 3 + ... + n = n*(n+1)/2
    
    as in this algorithm:
      input n
      x <- n + 1
      y <- n * x
      z <- y / 2
      output z
    
    Both algorithms are implemented in this servlet. Experiment with using the two different versions - make sure both the methods both produce the same, correct results.

    First, the 1+2+...+n method. Find a number of iterations for this method that reports reasonable user times (values bigger than 0.001, but smaller than 1.0, let's say) for a relatively small value of n. Use that number of iterations throughout the experiment. Vary the value of n substantially and record both kinds of times (wall and user) for summing 1 to n this way. Graph your results - two plots on the same graph one wall-time vs. n, the other user-time vs. n. Do you think the time complexity of this algorithm is linear, sublinear, or superlinear? Explain.

    Repeat the same process for Gauss' method.

    Explain which method you think is more efficient and why.

  2. Multiplication

    In class we discussed two different algorithms for computing the product of two numbers. One is the binary version of the our standard paper-and-pencil method. The other is the simpler method of repeated addition. Both algorithms are implemented in this servlet. Experiment with using the two different versions of multiplication - make sure that both methods both produce the same, correct results.

    First, the standard, binary method. Find a number of iterations for this method that reports reasonable user times (values bigger than 0.001, but smaller than 1.0, let's say) for relatively small values of a and b. Use that number of iterations throughout the experiment. Enter a fixed value for a. Vary the value of b substantially and record both kinds of times (wall and user) for computing a times b this way. Also, record the number of bits in the binary representation of b. Graph your results - two plots on the same graph one wall-time vs. b, the other user-time vs. b. Two plots on another graph, one wall-time vs. the number of bits in b, the other user-time vs. the number of bits of b. Do you think the time complexity of this algorithm is linear, sublinear, or superlinear? Explain.

    Repeat this process for a fixed value of b, but varying for a.

    Repeat the same two processes for the repeated-addition method.

    Explain which method you think is more efficient and why.

  3. Frequency analysis

    For this problem, you are to use the frequency-analysis tool to determine its efficiency. There are several parameters to vary: the period, whether or not to include overlapping groups, and, of course, the size of the input. You need not generate a graph (although you are welcome to), but you should describe what experiments you ran and what you can conclude about frequency analysis from your experiments. Is the time complexity of the algorithm being used linear, sublinear, or superlinear? Explain.

  4. Alphabetizing

    For this problem, you are to use this alphabetization servlet to determine its efficiency. The servlet take a piece of text and reorganizes the symbols occurring in the text into alphabetical order (with distinctions between uppercase and lowercase). Measure times for varying lengths of text to be alphabetized. Also, try and measure differences between the structure of the text - namely do two texts that have the same length, but one with hardly any recurring letters, and one with lots of recurring letters take the same amount of time to alphabetize? how about the order - does it matter whether the text is already alphabetized or alphabetized in reverse? Produce a graph with two plots (wall-time vs. size of input and user-time vs. size of input). Explain you graph: Is the time complexity of the algorithm being used linear, sublinear, or superlinear?

  5. Which do you think is more useful for measuring the efficiency of algorithms "wall time" or "user time"? Explain.


crypto home assignments labs notes tools contact instructor