Problems and Solution - Homework 1 - Algorithms and Data Structures | CSE 331, Assignments of Computer Science

Material Type: Assignment; Class: Algorithms and Data Structures; Subject: Computer Science & Engineering; University: Michigan State University; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-u2b
koofers-user-u2b 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 331: Homework 1 Spring 2009
Due by 11:59 PM Friday 23 January 2009 (100 points total)
This color indicates recent changes since the homework was first released. In
this case, these are just additional comments in response to some questions that
may be of assistance to you. You do not need to redo problem 1 if you already
did it and did not create a random vector this way, so long as you created some
kind of test vector.
Submission
Submit via http://www.cse.msu.edu/handin/ 3 files: <name>Results.pdf and
<name>h1.cpp where <name>is your last name; as in jonesResults.pdf and a
makefile to compile the code. It should generate as an output binary <name>h1.
You are responsible for making sure that your code compiles and runs
on arctic.cse.msu.edu. It should be executable as <name>h1 <algorithm>
<N>, where the argument <algorithm>may be 1, 2, or 4, and <N>may be
any positive integer.
Problems:
Do the following problems from Chapters 1 and 2 of our text. We do not want
just the “answers” (such as true or false or 5N2); we want the reasoning behind
the answers. Additionally, take a look at the homework submission guidelines
on the course webpage and use those as a general formatting guide.
1. Report on timing algorithms 1,2,4 for solving the Maximum Subsequence
Sum problem as covered in the text book. Collect algorithms 1,2,4 into
one program so that each can be run on the same sequence. C++ code
can be found in the directory ”Examples/MaxSubSum/*” of the course
web page. An example program that uses random numbers and timing is
given under ”Examples/Timing/STLsort.cpp”. You can copy these from
”∼cse331/web/Examples/”. This code compiles and runs on arctic. Be
aware that there exist differences between wall clock time and the actual
time that your task may be running on a CPU (particularly a shared CPU
such as arctic with many users). There are further differences between
system CPU and user CPU time. The question of how you should time
your code will be a decision that is ultimately left up to you. When you
have completed the problem, save your code as <name>h1.cpp, provide
a make file, and submit it via Handin. Please be aware that you may
lose points if your code does not meet the submission criteria
outlined above. Email the TA at [email protected] if you have
any questions.
1
pf3

Partial preview of the text

Download Problems and Solution - Homework 1 - Algorithms and Data Structures | CSE 331 and more Assignments Computer Science in PDF only on Docsity!

CSE 331: Homework 1 Spring 2009

Due by 11:59 PM Friday 23 January 2009 (100 points total)

This color indicates recent changes since the homework was first released. In this case, these are just additional comments in response to some questions that may be of assistance to you. You do not need to redo problem 1 if you already did it and did not create a random vector this way, so long as you created some kind of test vector.

Submission

Submit via http://www.cse.msu.edu/handin/ 3 files: Results.pdf and h1.cpp where is your last name; as in jonesResults.pdf and a makefile to compile the code. It should generate as an output binary h1. You are responsible for making sure that your code compiles and runs on arctic.cse.msu.edu. It should be executable as h1 , where the argument may be 1, 2, or 4, and may be any positive integer.

Problems:

Do the following problems from Chapters 1 and 2 of our text. We do not want just the “answers” (such as true or false or 5N 2 ); we want the reasoning behind the answers. Additionally, take a look at the homework submission guidelines on the course webpage and use those as a general formatting guide.

  1. Report on timing algorithms 1,2,4 for solving the Maximum Subsequence Sum problem as covered in the text book. Collect algorithms 1,2,4 into one program so that each can be run on the same sequence. C++ code can be found in the directory ”Examples/MaxSubSum/*” of the course web page. An example program that uses random numbers and timing is given under ”Examples/Timing/STLsort.cpp”. You can copy these from ”∼cse331/web/Examples/”. This code compiles and runs on arctic. Be aware that there exist differences between wall clock time and the actual time that your task may be running on a CPU (particularly a shared CPU such as arctic with many users). There are further differences between system CPU and user CPU time. The question of how you should time your code will be a decision that is ultimately left up to you. When you have completed the problem, save your code as h1.cpp, provide a make file, and submit it via Handin. Please be aware that you may lose points if your code does not meet the submission criteria outlined above. Email the TA at [email protected] if you have any questions.

(a) Make a table that shows the runtime for each of the 3 algorithms on sequences of length N=100, 200, 400, 800, 1600, 3200, 6400. To test these functions create a random vector that has alternating positive and negative numbers. This creates a problem that is interesting– if they are all positive, then the maximum subset problem is trivial– the entire set is the best subset. Just try to use the same set (e.g. use the same seed) to get a decent benchmark. Do not worry too much about random variation– this will not disguise the underlying trends. We suggest you use random numbers in between -100 and

  1. The following code can be used as an example to generate your test vector, although as with most examples there are other ways to do this. #include <stdlib.h>

...

// Use the same seed every time so the same random vector is // generated. Call this once. srand(1000);

... // fill up the random vector with alternating positive and // negative values for(int count=0;count<size;count+=2) { randvector[count] = rand() % 100; } for(int count=1;count<size;count+=2) { randvector[count] = - (rand() % 100); }

Note that algorithm 1 will be very slow for large values of N. If it is taking a very long time to complete (say, more than ten minutes), you may comment out the call to algorithm 1 and time the other two algorithms instead. Algorithm 4 is very fast. If it completes in zero seconds when you run it, you will need to test it with larger values of N (e.g. N=12800, 25600, etc.) until you start to get actual results. You may want to comment out the calls to algorithms 1 and 2 when you do this. ( points)

(b) Give an argument for the complexity of each algorithm, using the ratios of runtime as the sequence size N is doubled. (10 points) (c) Change each of the 3 functions by instrumenting them as follows.