Laboratory Exercises For Computer Science 153

Time and Space Complexity

Time and Space Complexity

Goals: This laboratory exercises introduces some principles of algorithm effectiveness, including the amount of time and memory required for the algorithm. Big-O notation is introduced to provide an informal measure of the time or space required by an algorithm.

In considering the solution to a problem, it is natural to ask how effective that solution might be. Also, when comparing solutions, one might wonder if one solution were better than another. Altogether, one might use many criteria to evaluate such solutions, including:

For this laboratory exercise, we focus on algorithm execution time and the use of memory.

Algorithm Execution Time

In determining algorithm execution time, we may proceed in several ways: Each of these approaches has advantages, but each also has drawbacks. Execution times on a specific machine normally depend upon details of the machine and on the specific data used. Timings may vary from data set to data set and from machine to machine, so experiments from one machine and one data set may not be very helpful in general.

The analysis of instructions may take into account the nature of the data -- for example, one might consider what happens in a worst case. Also, such analysis commonly is based on the size of the data being processed -- the number of items or how large or small the data are. This is sometimes called a microanalysis of program execution. Once again, however, the specific instructions may vary from machine to machine, and detailed conclusions from one machine may not apply to another.

A high-level analysis may identify types of activities performed, without considering exact timings of instructions. This is sometimes called a macroanalysis of program execution. This can give a helpful overall assessment of an algorithm, based on the size of the data. However, such an analysis cannot show fine variations among algorithms or machines.

For many purposes, it turns out than a high-level analysis provides adequate information to compare algorithms. For the most part, we follow that approach here.

  1. Reread Section 2.3.1 of the textbook.
Since that section focuses upon the processing of one or two numbers, the analysis is based on the size of those numbers. Further, for the examples in that procedure, within each procedure, only a few steps (a comparison and a few arithmetic operations) are performed before the procedure is called recursively. Said another way, the number of machine instructions performed within a procedure is limited (perhaps under 10) before the procedure is called again. Thus, the total number of steps performed by an algorithm is roughly proportional to the number of procedure calls.

Applying this idea in more detail, the text concludes that the number of instructions required for procedure mult%1 is proportion to b, while the number of operations for procedure mult%2 is proportion to log b.

  1. Explain in your own words why this conclusion seems justified. (Be sure to ask the instructor if you have questions.)
A macroanalysis ignores proportionality constants from a microanalysis; as differences from machine to machine tend to change the proportionality constant, not the nature of the main terms. Informally, the book suggests that the order of an algorithm is the amount of time required to execute an algorithm, ignoring the proportionality constants. Thus, procedure mult%1 has order b -- written mult%1 has O(b). Similarly, mult%2 has O(log b).
  1. Solve exercises 2-15 through 2-19 from the textbook.
Section 2.3.2 discusses the amount of space required by an algorithm. Again, the analysis can focus on a specific machine, or one may proceed at a higher level.
  1. Reread Section 2.3.2 of the textbook.

  2. Define in your own words what is meant by tail recursion.

  3. Solve exercises 2-21 through 2-23 from the textbook.
Work to be turned in: Note: When one of the above exercises requires you to write a program, be sure to run and test the program following the format specified for the Supplemental Problems.


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/153/lab-complexity.html

created January 14, 1998
last revised January 14, 1998