Use the copy-and-paste method to give an average-case analysis for the permute function in permute.cpp.
The rand() function runs in O(1) time (worst-case and average).
Check your analysis by running the algorithm on a variety of input sizes and measuring the time it takes.
The program pdriver.cpp can be used as a “driver” to execute the permute algorithm. This program expects a pair of command line arguments when run. The first argument is the number of items to permute. The second is the number of times you want to repeat the permutation process.
For example, if you compile this program to produce an executable named “pdriver”, then
pdriver 50 10
will generate 10 permutations of 50 elements each.
Because the purpose of this exercise is to generate timing data rather than to actually work with the algorithm, the generated permutations are not printed. Feel free to add output statements if you want to see the algorithms in action, but remember to remove those statements before proceeding on to the timing steps. (I/O is slow and may distort your timing results.)
On Linux systems, you can measure the run time of the programs (or of any Unix program/command) by placing the command “time” in front of the program invocation. For example,
time ./pdriver 50 10
will determine the time required to generate 10 permutations of 50 elements each.
The time will appear in a format similar to this:
real 0m5.530s user 0m3.362s sys 0m0.090s
The last two numbers are of the most interest to us. Their sum is the number of seconds the CPU devoted to execution of this program. This is often much less than the “clock time” (the third number), because the CPU is usually being shared by several different programs.
The user time is the amount of time spent in “user” code.
The sys time is the amount of time spent in “system” calls.Together, these consititute the “CPU time” used by the command.
The real time is the actual time that passed from when the program started to when it ended. This can be much longer than the CPU time.
We are interested in the CPU time because the other portion of the real time is being spent running other processes or programs other than the one were are timing.
An important factor to keep in mind is that we are looking for average times. If we ran the algorithm only a single time for a particular value of N, depending upon how the random numbers came out, we might get an unusually fast or an unusually slow run. We need to do multiple repetitions for each value of N so that we can get the average time of a single run of size N. That’s why the main driver function of this program is designed to allow you to request multiple repetitions of the algorithm for a fixed N.
Another reason for doing averages over many repetitions is to reduce the effect of measurement errors – always a possibility in any experiment. Even computer clocks have a finite resolution. For small values of N this algorithm might run in a fraction of one clock “tick”. Another source of measurement error in this kind of experiment is the fact that we can’t run just the permute algorithm in isolation. We have to run a whole program that launches the permute algorithm, so some of our measured time will be due to code other than the algorithm we are interested in.
Timings of less than 10 seconds are likely to be dominated by the overhead of starting and stopping the program, so you should adjust the repetition count (the second argument of the permute program) to make sure that all your timed runs take at least that many seconds. A minimum of 60 seconds is probably a good target.
In a non-Unix environment, getting the timing information is harder. You can, of course, time the program with a good old-fashioned stopwatch, but you’ll need to take special care to be sure that your own physical reaction time doesn’t affect the results. In addition, you are then measuring clock time, not CPU time. We have already discussed the dangers of using clock time.I strongly recommend, therefore, that you do this experiment on our Linux servers or on a Linux (or MacOS) machine of your own, if you have one.
R| 10000 | 5000 | 2000
TT | 60.52 | 56.5 | 110.2
What values of N should you use? Here are some things to keep in mind:
In
this lecture
we looked at a procedure for confirming a predicted complexity. In this assignment, we will employ a slightly expanded form of the same technique.
In your spreadsheet, make sure that your rows are sorted into increasing values of N. Then add a 4th column in which you compute T,1 the time to execute a single call to the permute function. This should be (approximately) TT/R:
N | 100 | 200 | 400
R | 10000 | 5000 | 2000
TT | .006052 | .0113 | .0551
Now, from this lecture, you should recall that,
These three observations allow us to “bracket” a function that we think represents the actual complexity.
Prepare a report on your analysis and experimental results. This report may be in a plain-text .txt file or in a PDF file. (Most word processors will allow you to save or export to PDF.)
The report should include
makefile.txt
DIR=${PWD}
ASST=$(notdir ${DIR})
MAINPROG=pdriver
ifneq (,$(findstring MinGW,$(PATH)))
DISTR=MinGW
EXE=.exe
LFLAGS=
else
DISTR=Unix
EXE=
LFLAGS=
endif
#
########################################################################
# Macro definitions for “standard” C and C++ compilations
#
CPPFLAGS=-g -D$(DISTR)
CFLAGS=-g
TARGET=$(MAINPROG)$(EXE)
CPPS=pdriver.cpp permute.cpp
LINK=g++ $(CPPFLAGS)
#
CC=gcc
CXX=g++
#
#
# In most cases, you should not change anything below this line.
#
# The following is “boilerplate” to set up the standard compilation
# commands:
#
OBJS=$(CPPS:%.cpp=%.o)
DEPENDENCIES = $(CPPS:%.cpp=%.d)
%.d: %.cpp
touch $@
%.o: %.cpp
$(CXX) $(CPPFLAGS) -MMD -o $@ -c $*.cpp
#
# Targets:
#
all: $(TARGET)
$(TARGET): $(OBJS)
$(LINK) $(FLAGS) -o $(TARGET) $(OBJS) $(LFLAGS)
clean:
-/bin/rm -f *.d *.o $(TARGET)
make.dep: $(DEPENDENCIES)
-cat $(DEPENDENCIES) > $@
include make.dep
pdriver.cpp
#include
#include
#include
#include
using namespace std;
void permute (int[], int);
int main(int argc, char** argv)
{
if (argc != 3)
{
cerr << "When you run this program, you must supply two parameters.\n"
<< "The first is the size of the array you want to permute.\n"
<< "The second is the number of trials you want to perform.\n"
<< "\n"
<< "For example, if you called this program pdriver, you\n"
<< "might invoke it as:\n"
<< " pdriver 100 10 \n"
<< "to generate 10 random permutations of 100 elements each."
<< endl;
return 1;
}
int N;
int trials;
{
istringstream arg1 (argv[1]);
arg1 >> N;
istringstream arg2 (argv[2]);
arg2 >> trials;
}
int *array = new int[N];
srand(time(0));
for (int t = 0; t < trials; t++)
{
permute (array, N);
// for (int i = 0; i < N; ++i) cout << array[i] << ' '; cout << endl;
}
return 0;
}
permute.cpp
#include
#include
#include
using namespace std;
unsigned rnd(unsigned limit)
{
return rand() % limit;
}
// Generate a random permutation of the integers from
// 0 .. n-1, storing the results in array a.
//
void permute (int a[], int n)
{
for (int i = 0; i < n; i++)
{
// Guess at a value to put into a[i]
int guess = rnd(n);
while (find(a, a+i, guess) != a+i)
{
// If it's one that we've already used, guess again.
guess = rnd(n);
}
a[i] = guess;
}
}
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.