Warning:
This course is under development.
Although the basic structure of this course is largely established,
nothing on this Web site should be considered official or even possibly
correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.
Algorithm Analysis
Summary
In this lab, you will analyze the complexity of some
code and experimentally test your analyses.
Preparation
Load into Eclipse the java programs in the
package analysis.
TestAnalysis.java is the main program that calls the other pieces
(Loop1.java ... Loop8.java). TestAnalysis.java allows
you to conduct experiments for the subsequent exercises.
The program uses command-line input.
-
The first command line argument is the exercise number.
-
The second command line argument is a value for n.
To supply this information, select the "Run as Configurations..." option.
Once the window comes up, be sure you have selcted TestAnalysis,
and select the "Arguments" tab.
For example, to run the code for Exercise 1 with n=15, you would
type
1 15
in the arguments window, and then click "Run".
Note: In this lab, a variable NUMBER_REPETITIONS is
specified to repeat a segment of
code NUMBER_REPETITIONS2 times. This allows reasonable
timings to be compared within the accuracy of the clock.
-
If you find that experiments are going so fast that all times are close to
0, you may want to increase
NUMBER_REPETITIONS (i.e., add a 0 to
multiply by 10).
-
If you find that experiments are taking a great deal of time, you might
want to decrease
NUMBER_REPETITIONS(i.e., remove a 0 to
divide by 10).
Exercises
General Notes:
-
In each of the folloiwng exercises, when you are asked to determine the
Big-h running time, you will need to start with a micro-analysis. In your
analysis, use the following constants for the time it takes a machine to do
specific operations:
-
A — one assignment
-
B — one evaluation of a Boolean (e.g., a comparison)
-
S — one addition or subtraction
-
M — one multiplication or division
-
P — one increment (e.g., ++)
-
Once you have an expression for the time required, based on a
micro-analysis, you are asked to switch to a macro-analysis to get Big-Oh
running time. Although it may be tempting to argue, "just use the highest
power", in at least four cases you must give a careful mathematical
argument (based on inequalities and algebra).
-
Reference: "Summation formulas" from Tripod
Exercise 1
- What is the Big-Oh running time of
Loop1?
-
Run the code with N = 10.
-
Repeat this run several times, and observe what variation seems to occur
within these timings.
-
What (range of) values do you expect for N = 20?
-
Verify your prediction.
-
Try several more values of N.
-
Compare your analysis with the actual running times. Do they agree?
Exercise 2
- What is the Big-Oh running time of
Loop2?
-
Run the code with several values of N.
-
Compare your analysis with the actual running times. Do they agree?
Exercise 3
-
What value will result from N =
10? N = 20?
- What is the Big-Oh running time of
Loop3?
-
Run the code with a several values of N.
-
Compare your analysis with the actual running times. Do they agree?
Exercise 4
- What is the Big-Oh running time of
Loop4?
-
Run the code with several values of N.
-
Compare your analysis with the actual running times. Do they agree?
Exercise 5
-
What value will result from N= 4? N = 5?
-
Can you recall the closed-form for the value that will be
returned? (If not, you might want to consult [Weiss, Section 5.2].)
- What is the Big-Oh running time of
Loop5?
-
If you wish, you may run the algorithm for several values
of N and compare your analysis with the actual running
times.
Exercise 6
-
A closed form analysis of
Loop6 is still possible,
using the same formula as used in Exercise 5.b. Hazard a guess as
to the formula and predict the result for N = 3 and N = 4.
-
Test your predictions.
-
Based on your formula, or your observations from the code, what is
the Big-Oh running time of
Loop6?
-
Run the algorithm for a few more values of N and compare
your analysis with the actual running times.
Exercise 7
-
What value will result from N = 4? N = 8? N = 16
- What is the Big-Oh running time of
Loop7?
-
Run the algorithm for a few values of N and compare your
analysis with the actual running times.
Exercise 8
-
What is the Big-Oh running time of
Loop8?
-
Run the code with several values of N.
-
Compare your analysis with the actual running times. Do they agree?
-
Given the results from b, what would you say the running
time seems closer to?
-
What is it about the innermost loop that causes the actual results
to differ widely from the Big-Oh?
Remember, Big-Oh is an upper bound!
For those with extra time
Ok, now that you've thoroughly flexed your analysis muscles and given
your CPU a very modest workout, let's switch gears (slightly).
Say we have an N×N matrix such that values are
increasing from left to right in every row, and values are also
increasing from top to bottom in every column. The following is an example:
| 1 | 2 | 4 | 6 |
| 3 | 5 | 7 | 9 |
| 4 | 8 | 11 | 13 |
| 7 | 9 | 12 | 14 |
One question we may ask is: How efficiently can we do search in such a matrix?
Exercise 9
Suppose one algorithm implements a binary search on each row, and
eventually indicates whether the value was found on any of the rows. What is the running time of this algorithm in terms of N?
Exercise 10
Now someone crafty comes along an realizes they can do a more efficient search. Consider the value at the upper-right corner of the matrix.
-
If this value is greater than your query, where in the matrix
should you look next? (I.e., where should you "move" to?) Left?
Down? Something else??
-
If the value in the upper-right corner is less than your query,
where should you look next?
-
Can you repeat these tests based on your current location to
devise a complete search algorithm?
-
What is the running time of your algorithm? (It should be better
than the one in Exercise 10!)
Adapted from Mark Allen Weiss, Data Structures and Problem Solving Using Java, 3/e Exercises 5.15, 5.16, and 5.21
created 7 January 2009 by Jerod Weinman
updated 23 February 2010 by Jerod Weinman
revised 27-28 February 2012 by Henry M. Walker
refined 20 January 2020 by Henry M. Walker
|
|
|
For more information, please contact
Henry M. Walker at
walker@cs.grinnell.edu.
|