CSc 150
Homework 1 -- Analyzing Time Complexity
Due Wednesday, May 5, 2010
Part 1: Source Code and Algorithm Analysis
For each of the following code fragments or algorithms,
determine the worst-case time complexity and express it in Big-O
notation in
terms of the number of input. You must explain each of your
answers. You
will only get partial credit if you don't have explanations.
-
public void one(int n)
{
int a;
for (int i=n; i>0; i-=2)
a=i;
}
-
public void two(int n, int p)
{
int a;
if (p>0)
{
for (int i=0; i<n; i++)
for (int j=1; j<=n; j++)
a=i*j;
}
else
{
for (int i=0; i<n; i++)
a=i;
}
}
-
public void three(int[] myArray)
{
int n=myArray.length;
int count=0;
do
{
count++;
n=n/2;
} while (n>1);
n=myArray.length;
for (int i=1; i<=count; i++)
for (int j=0; j<n; j++)
System.out.print(i*myArray[j]);
}
Unfamiliar with Java's do-while loop? Check out
this
tutorial or any number of others just by Googling it.
-
public void four(int n, int p)
{
int a;
for (int k=0; k<p; k++)
{
System.out.println(k);
}
for (int i=1; i<=6; i++)
this.one(n); // call to method "one" above
}
-
public void five(int[] A, int[] B)
{
int n=A.length;
int m=B.length;
for (int i=0; i<m; i++) {
for (int j=0; j<=i; j++) {
B[i]=j;
}
}
for (int p=n-1; p>=0; p--) {
for (int q=p-1; q>=0; q--) {
A[p]=q;
}
}
}
Part 2: Run-time Analysis
For this part, you will be analyzing
the time
complexity of various sorting routines. However, instead of analyzing
the source
code, you will be conducting experiments at runtime by measuring how
long each
routine takes in terms of time and data structure accesses.
Go to our BlackBoard web page
and
download two files from the Course Material section. The first
is the compiled code for six different sorting
methods, called Sort.jar. It
has the following public interface:
public class Sort
{
public static int[] method1(int[] A)
{
// returns a new sorted array. The array A is not changed.
}
public static int[] method2(int[] A)
{
// returns a new sorted array. The array A is not changed.
}
public static int[] method3(int[] A)
{
// returns a new sorted array. The array A is not changed.
}
public static int[] method4(int[] A)
{
// returns a new sorted array. The array A is not changed.
}
public static int[] method5(int[] A)
{
// returns a new sorted array. The array A is not changed.
}
public static int[] method6(int[] A)
{
// returns a new sorted array. The array A is not changed.
}
// getMethod<X>Count: returns the number of array accesses
// that occurred in the last execution of the method.
public static long getMethod1Count()
public static long getMethod2Count()
public static long getMethod3Count()
public static long getMethod4Count()
public static long getMethod5Count()
public static long getMethod6Count()
} // end of class Sort
Note that these methods are all static, which means that
you can
execute a method just by saying Sort.getMethod1Count().
The second file you need to download is Client.java. This
is the
source code containing the main() method that runs each of the
above
sorting methods.
Setup
To use these two files in Eclipse, start a new project.
Add in
Client.java by dragging the file into the lefthand pane in
Eclipse and making sure it ends up in the src folder.
To add the Sort.jar file to
your
project, go to the Project menu and select Properties.
Make sure Java Build Path is selected in the lefthand pane.
Click on
the Libraries tab, and then use the Add External JARs... button
to select
Sort.jar from your local system. Once included, the
main()
method should be runnable.
Once your project is runnable, be sure to study the
Client.java
file to understand what's going on. Notice that there are two ways in
which the
time complexity of the sorting methods is being measured:
- The code is measuring the time in milliseconds of execution of
each method
(using the currentTimeMillis() method of System)
- The code is counting the number of array accesses the method
makes.
Your Mission
Your task is to deduce the worst-case time
complexity of
each of the six methods and express it in Big-O notation. To accomplish
this,
you need to run the main() method many times using many
different
arrays of various lengths and containing many different types of
numbers. For
example, the main() method currently fills the array with
random
numbers. You could also try identical numbers, a sorted list, and a
reverse-sorted list, to name a few. For each type of contents, you
should run
many experiments with a variety of different array lengths. For each
run, keep
track of the time complexity measurements that the program outputs.
Using this data, I want you to make two plots for each method:
- A plot of the time each of the array content types (random,
sorted, etc.)
takes (on the vertical axis) against the array length (on the
horizontal axis)
- A plot of the number of array accesses each of the array content
types
makes (on the vertical axis) against the array length (on the
horizontal axis)
Based on the plots, I want you to conjecture on the
worst-case time
complexity of each of the six methods and express it in Big-O notation.
Be
sure to include a brief explanation supporting each of your conjectures.
Also,
for each method, discuss possible reasons for any differences you find
between
the two plots for the method. You must do your plots electronically
(not by
hand.) Microsoft Excel did a pretty good job when I tried it.
Notes:
- Please turn in paper copies of your graphs, but I also want
to see the actual raw data that you obtained (in an organized way).
If you don't
wish to waste paper, you
can turn
in your graphs as an Excel file on BlackBoard.
- The thoroughness of your tests play a big role here. You should
alter the
main() method in many ways to make sure you get the
worst-case
complexity. How does the method fare on random lists of numbers? On a
list
that's already sorted? On a list that's sorted in reverse? On a list
of
identical numbers? And, of course, you must be vigilant about trying
all kinds
of array sizes. Don't be discouraged if you start getting strange or
even
negative numbers. A negative number usually means the variable
overflowed its capacity (you tried to store a number that was
too big
for it to handle). Just use data that you got up to that point. And if
you
start waiting too long for it to finish (more than 10 minutes), don't
use a
bigger array than that. You could, however, comment out the other
methods and
test each one individually! Try to test as big an array as you
reasonably can. You'll need at least 10 data points for each
type and length of array that you test in order to be reasonably
sure that you can see the pattern.
- Use more than just the graphs for determining the time complexity.
Remember that virtually all complexities greater than O(n) look like
concave-up curves. So use the numeric data too. If you think something
is
O(n2), then if you double the data size, you ought to
quadruple the
number of steps.
- You should treat this project like a physics or chemistry
experiment in which
you are making measurements and making conjectures based on your
measurements.
Therefore a significant number of points will be taken off if you have
not
sufficiently tested the algorithms or if you have not sufficiently
analyzed
your results. Make sure your plots are clear, well-labeled, neat, etc.
You
need to include a discussion of the tests you made so that I can
understand
where your data came from. The harder it is for me to understand what
you did,
the fewer points you will receive.
Gentle Reminder
Homeworks, like programming
assignments, are
individual projects. I encourage you to talk to others
about the
general nature of the homework and ideas about how to pursue it.
However, the
technical work, the writing, and the inspiration behind these must be
substantially your own. If any person besides you contributes in any way
to the
project, you must credit their work on your homework. Similarly, if you
include
information that you have gleaned from other published sources, you must
cite
them as references. Looking at, and/or copying, other people's programs
or
written work is inappropriate, and will be considered cheating.
Return to Homework
Index