Lab 10: Understanding time complexity analysis

advertisement
Lab 10: Understanding time complexity analysis
Part 1:
This part of the lab will give you practice at estimating the running time in your
programs.
Look at the code at end of this document. Save it as a text file and load it into your IDE
(BlueJ or Eclipse).
BEFORE YOU RUN THE CODE, copy the following table on a piece of paper and fill
in the second and third columns: Statement Count Formula and My Estimate of running
time (before execution). Possible value might include: O(1), O(n), O(nlogn), O(n^2),
O(n^3) for column 3. Review your lecture notes and the course website as needed.
These reminders will help, where n is the size of the input to a method.





O(1) f the running time of a method never takes more than a constant amount of
time.
O(n) if the running time of the method is a linear function of n
O(nlogn) if the fastest growing term of the running time function of the method is
nlogn
O(n^2) if the running time formula for a method is quadratic
O(n^3) if the running time formula is a polynomial whose largest term is n^3.
method
Statement
Count
Formula
My Estimate of
running time
(before
execution).
My Estimate of
running time
(after execution)
Comments if column
2 and 3 differ
methodA
methodB
methodC
methodD
methodE
Notice that each method starts and ends with a call to System.currentTimeMillis(). You
can ignore these calls in your running time estimates. The calls will keep track of the
actual running time of each method when you run the program in the next part.
Now run the program. It may take up to a minute for the code to complete execution. If it
is taking longer than 2 minutes, let your TA know. You may need to decrease the input
size. The program will run methodA and output the running time for a series of
increasing sizes of n. The running time is given in milliseconds.
Now graph the results using Microsoft Excel as discussed in the lecture, and see if your
estimate for the running time for methodA was accurate. Fill in column 2 for methodA
and fill in column 3 if your original estimate was not accurate.
Now comment out the first block of 5 lines in the main method.
Next uncomment the 5 lines in the next block and run the program. The results for
methodB will display. Fill out the table and repeat this process until you have run code
for each of the methods A-E.
A possible note here about why the times may vary from execution to execution, and note
that the execution times found are not necessarily absolute. The CPU may be running
other processes, etc.
Now show your TA your completed table and your graphs for each method.
Part 2:
To conduct the measures of time complexity in part 2, you will need a source of random
numbers.
You can use Java to create random numbers. Actually Java does not create random
numbers; it creates pseudorandom numbers. This means that if you set the same seed
(and keep everything else the same), Java will produce the same series of random
numbers each time. This can be a desirable quality for testing, because we can conduct
tests on our code with the same set of data.
Having the same data set will also be helpful in time complexity analysis, because we
know that the differences in running time we see are not due to different sets of data.
There are two main ways to create random numbers in Java.
1. the random( ) method of the class Math
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html
2. the Random class in the java.util package
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html
We want to use the second method today, because we want to be able to set the seed.
Your task: using a seed value of 221 and the Random class, generate 20 pseudorandom
numbers between 1 and 10,000 inclusive, printing each number as it is generated.
Now run your program again with the same seed and verify that the 20 pseudorandom
numbers generated are the same.
Now taking what you have learned in part 1 and 2, implement a program that compares
the running time of bubblesort and selection sort for arrays of size 12,500, 25,000,
50,000, 100,000. Use Java’s pseudorandom number generator to populate the arrays with
random numbers between 1 and 1,000,000 inclusive.
Graph the results using Microsoft Excel (or equivalent) as discussed in the lecture. Print
the graphs and discuss how the results compare with the expected running times of
bubble sort and selection sort.
Part 3. Demo your outlab for your TA.
Lab Program Code
/**
*
*/
package lab10;
import java.util.Date;
import java.util.Timer;
/**
* @author CBE
*
*/
public class ComplexityExperiment {
public static void main (String args []){
System.out.println("Time for method A in milliseconds");
System.out.println("n=250: time: " + methodA(250));
System.out.println("n=500: time: " + methodA(500));
System.out.println("n=1000: time: " + methodA(1000));
System.out.println("n=2000: time: " + methodA(2000));
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
System.out.println("Time for method B in milliseconds");
System.out.println("n=250,000,000: time: " + methodB(250000000));
System.out.println("n=500,000,000: time: " + methodB(500000000));
System.out.println("n=1,000,000,000: time: " + methodB(1000000000));
System.out.println("n=2,000,000,000: time: " + methodB(2000000000));
System.out.println("Time for method C in milliseconds");
System.out.println("n=12,500: time: " + methodC(12500));
System.out.println("n=25,000: time: " + methodC(25000));
System.out.println("n=50,000: time: " + methodC(50000));
System.out.println("n=100,000: time: " + methodC(100000));
System.out.println("Time for method D in milliseconds");
System.out.println("n=12,500: time: " + methodD(12500));
System.out.println("n=25,000: time: " + methodD(25000));
System.out.println("n=50,000: time: " + methodD(50000));
System.out.println("n=100,000: time: " + methodD(100000));
System.out.println("Time for method E in milliseconds");
System.out.println("n=250,000,000: time: " + methodE(250000000));
System.out.println("n=500,000,000: time: " + methodE(500000000));
System.out.println("n=1,000,000,000: time: " + methodE(1000000000));
System.out.println("n=2,000,000,000: time: " + methodE(2000000000));
}
public static double methodA (int n){
double startTime =System.currentTimeMillis() ;
int i = 0;
int j = 0;
int k = 0;
int total = 0;
while (i<n){
while (j<n){
while (k<n) {
total++;
k++;
}
k=0;
j++;
}
j=0;
i++;
}
return System.currentTimeMillis() - startTime ;
}
public static double methodB (int n){
double startTime =System.currentTimeMillis() ;
int i = 0;
int total = 0;
while (i<n){
total++;
i++;
}
return System.currentTimeMillis() - startTime ;
}
public static double methodC (int n){
double startTime =System.currentTimeMillis() ;
int i = 0;
int j = 0;
int total = 0;
while (i<n){
while (j<n){
total++;
j++;
}
j=0;
i++;
}
return System.currentTimeMillis() - startTime ;
}
public static double methodD (int n){
double startTime =System.currentTimeMillis() ;
int i = 0;
int j = 0;
int total = 0;
while (i<n){
while (j<i){
total++;
j++;
}
j=0;
i++;
}
return System.currentTimeMillis() - startTime ;
}
public static double methodE (int n){
double startTime =System.currentTimeMillis() ;
int i = 0;
int j = 0;
int total = 0;
while (i<n){
while (j<n){
total++;
j++;
}
i++;
}
return System.currentTimeMillis() - startTime ;
}
}
Download