 |
CoSc2030: Computer Science II
Fall Semester 2007
Instructor: Dr. Thomas Bailey
|
Lab 11: Sets
This lab will explore set operations and the time that it takes to insert and extract numbers from a set. To begin this lab, write a file called Lab11Driver.cpp and include a main() method. Now download Timer.h and Timer.cpp files which will be used to time the code written in this lab, and RanGen.cpp and RanGen.h which will be used to create the random numbers needed for this lab. Create a spreadsheet in Excel where the various times for each part can be recorded and graphed.
Lab Assignment:
- In the main() method, make a new empty set of longs. Generate the numbers 1 to n and insert the numbers one after another into the set. Time this this process using a Timer object. Find an initial n that gives a time of around 10 seconds. Now divide n by 16, and run your algorithm on that number. Then divide n by 8, again run on that number, then divide by 4, and 2. Record the run times for each of these runs in the spreadsheet. The expected time complexity for this process will be Q(nlogn). Explain how your timing data supports the conjecture that the runtime is nlogn using a graph of the findings in the format of X with a log scale and Y = time / size * log(size).
-
Now create an empty set of doubles and time how long it takes to insert the number 1 through n. Then time how long it takes to find the numbers 1 through n in the set. Record the times in the spreadsheet. Again, find an initial n that gives a time of around 10 seconds. Time the process for n/16, n/8, n/4, n/2, and n. Record the run times for each of these values. The expected time complexity for this process will be Q(nlogn). Explain the time complexity found for this part. Explain how your timing data supports the conjecture that the runtime is Q(nlogn) using a graph of the findings in the format of X with a log scale and Y = time / size * log(size).
- Again create an empty set of doubles, but this time, insert n random samples obtained from the method .unif() (which samples the uniform distribution from 0 to 1) included with RanGen(). See how long the insertions take. Record the times for five different sizes in the spreadsheet. The insertion should now run a bit faster then for sort 2. This is because the numbers going in should not cause the underlying trees used to implement the set to be rearranged as often as before. For this part the runtime should be Q(nlogn). Explain how your timing data supports the conjecture that the runtime is Q(nlogn) using a graph of the findings in the format of X with a log scale and Y = time / size * log(size).
- Now insert n random values obtained with using the .unif() method into an empty set of doubles. Then take a sample of n numbers from .unif(), most of which will not be in the set, and time how long it takes to search for these numbers. That is, intentionally search for values that were NOT inserted into the set. The set will be searching down to an empty node every time. Record the run times for 5 different sizes in the spreadsheet. This process should also give a run time of Q(nlogn). Explain how your timing data supports the conjecture that the runtime is Q(nlogn) using a graph of the findings in the format of X with a log scale and Y = time / size * log(size).
- This time insert n random ints (use the .randint(1, n) method in RanGen) in the range from 1 to n, and report the size of the set for various values of n. The set will usually be smaller than n because there will be some duplicate in the list of inserted values. How does the size of the set vary with n? The expectation is that for large n, it will go to some constant c times n.
Turn In:
Upload Lab11Driver.cpp with your name commented in the top of the file, and your Excel spreadsheet entitled: Lab11YourName.
<< CoSc2030 Home
Copyright © 2005 Thomas Bailey