0% found this document useful (0 votes)
27 views3 pages

SC330: A A D S E: Pplied Lgorithms and ATA Tructures For Ngineers

This document provides instructions for an assignment involving algorithms and data structures. Students are asked to: 1) Solve problems related to sums and derivatives. 2) Complete a truth table comparing the asymptotic behavior of functions. 3) Implement and time two algorithms for calculating the greatest common divisor (GCD) of integers: a simple algorithm and Euclid's algorithm. Plot the timing results.

Uploaded by

teddymax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

SC330: A A D S E: Pplied Lgorithms and ATA Tructures For Ngineers

This document provides instructions for an assignment involving algorithms and data structures. Students are asked to: 1) Solve problems related to sums and derivatives. 2) Complete a truth table comparing the asymptotic behavior of functions. 3) Implement and time two algorithms for calculating the greatest common divisor (GCD) of integers: a simple algorithm and Euclid's algorithm. Plot the timing results.

Uploaded by

teddymax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SC330: APPLIED ALGORITHMS AND

DATA STRUCTURES FOR ENGINEERS


Exercise 1, Spring 2014

1)

Sums are fun! (See CLRS Appendix A)


a) Using

10

= 1024

find the sum

10
X
3.1415

2i

i=0

b) A.1-1 Give simple expression for sum:

c) A.1-3 Given

by taking the derivative of both sides and multiplying by

x on both sides show

Hint first prove (A.8).

d) Using LHopitals rule to show that (A.5) implies (A.1) see Guillaume de l'Hpital on
Wikipedia of course!
e) A.2-4 Give a lower and upper bound to

using

Show that these bounds hold relative to the exact answer in (A.4)
2) Fill in the following table with T or F for each relation.
f(n)

g(n)

6n + 7
n

f(n) O(g(n))

f(n) (g(n))

f(n) (g(n))

f(n) (g(n))

f(n) (g(n))

3) This is a short programming exercise which will accomplish three goals:


a) Demonstration of concepts learned thus far.
b) Dusting off programming skills (see nice tutorial at
http://www.cplusplus.com/doc/tutorial/)
c) Introduce you to the lab space is at BME Computational Simulation Facility,
24 Cummington Mall rooms B03 and B04
[a]. Your mission, should you choose to accept it (and you must) will be to time algorithms
that find the greatest common divisor (GCD) between two (randomly) chosen integers p and q of
size O(N). You will do this using two different algorithms.
(Algorithm 1) aka the Dumb Algorithm
Start from i = MIN(p,q)
Count down until i is a divisor of both p and q,
i.e. both (p mod i)=0 and (q mod i)=0 .
(Algorithm 2) aka Euclids Algorithm
Start with q< = p
While q > 0 do
r = p mod q
p=q
q=r
end
return p
[b.] A main.cpp file containing a basic skeleton of the overall program has been provided to
you. Notice that some parts of the code should not be changed and comments have been entered
to guide you where to put your code or which parts you can edit.
You should first write the corresponding C programs for the two algorithms given above. (Fill in
the given blank function bodies in the main.cpp). You are then going to time them to determine
which one performs empirically better and do some data analysis to see how the scale in N. To
do so, main.cpp has included timing routines from the <time.h> standard library:
http://www.cplusplus.com/reference/ctime/ Note, that the time has a resolution of only 1 second
so you will need to set loops to iterate over many instances and report the average time per GCD
execution.
[c]. Finally, once you have your algorithms and timing functions properly programmed,
plot 30 points for the time required for each algorithm for increasing values of N. The value of N
should be chose separately for each because one is so much slower than the other. To get a good

average measurement for each N you should generate many random sets of p, q = O(N).
The following code gives long integers for p,q with p => q:
p = ((RAND_MAX+1)*rand() + rand())%N + 1 ;
q = ((RAND_MAX+1)*rand() + rand())%N + 1 ;
if(p < q) {
unsigned long int tmp = p;
p = q;
q = tmp;
};
[d] Try to plot each algorithm on both linear and log plots. Should see that
the DumbGCD is fit well a linear function in T(N) = const*N, whereas the EuclidGCD
is fit well a linear function in log(N), T(N) = const*log(N).
Graphing the output:
You can use any graphic method that you like but a basic one on all UNIX machines is gnuplot.
See instruction at http://www.gnuplot.info/
Submitting Assignment:
All of the results for problem 1 and 2 above and the two figures for problem 3 should be
submitted as pdf file to the blackboard, EXCEPT the program main.cpp.
You should fill in the answers to Problem 1 and 2 directly into the Word document.
Submitting the program:
For the programming problem, a Makefile has been provided for you to be able easily compile
your program during testing/development. Before submitting your program, MAKE SURE that
your code compiles in Linux using the Makefile provided.
- Put your main.cpp into a folder named yourBUid_hw1 and tar (or zip) the folder.
- Email this tar/zip file (yourBUid_hw1.tar) to [email protected] with the EXACT
subject line yourBuid_hw1 for your submission to be correctly filtered.

You might also like