CS 415, Section 002 | Sonoma State University | Spring, 2022 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Download the file analysis.tgz, and decompress it with the line
tar -xvf analysis.tgz
Upon decompression, a new directory analysis should have been created, and this directory should contain TestAnalysis.cpp, a main program, and functions (run1.cpp) ... run8.cpp)). TestAnalysis.cpp allows you to conduct experiments for the subsequent exercises. To compile this program use the line:
make TestAnalysis
The program uses command-line input.
For example, to run the code for Exercise 1 with n=100, you would type
./testAnalysis 1 100
Note: In this lab, a variable NUMBER_REPETITIONS is specified to repeat a segment of code NUMBER_REPETITIONS2 times. This allows reasonable timings to be compared within the accuracy of the clock.
General Notes:
In exercises 1 and 2, when you are asked to determine the Big-O, Big- Ω, or Big-Θ running times, you will need to start with a micro-analysis. In your analysis, use the following constants for the time it takes a machine to do specific operations:
Again for exercises 1 and 2, once you have an expression for the time required, based on a micro-analysis, you are asked to switch to a macro-analysis to get Big-O, Big- Ω, or Big-Θ running times. Although it may be tempting to argue, "just use the highest power", in at least four cases you must give a careful mathematical argument (based on inequalities and algebra).
For later exercises, when you are asked to determine Big-O, Big- Ω, or Big-Θ running times,, a micro-analysis is not needed, but you need to provide a solid justification of your conclusions.
Reference: "Summation formulas" from Tripod
#include "loop.h" int run1(int n) { int sum = 0; for (int i=0 ; i < n ; i+=2) sum++; return sum; }
run1
?#include "loop.h" int run2(int n) { int sum = 0; for (int i=0 ; i < n ; i++) for (int j=0 ; j < n ; j++) sum++; return sum; }
run2
?#include "loop.h" int run3(int n) { int sum = 0; for (int i=0 ; i < n ; i++) sum++; for (int j=0 ; j < n ; j++) sum++; return sum; }
run3
?#include "loop.h" int run4(int n) { int sum = 0; for (int i=0 ; i < n ; i++) for (int j=0 ; j < n * n ; j++) sum++; return sum; }
run4
?#include "loop.h" int run5(int n) { int sum = 0; for (int i=0 ; i < n ; i++) for (int j=0 ; j < i ; j++) sum++; return sum; }
run5
?#include "loop.h" int run6(int n) { int sum = 0; for (int i=0 ; i < n ; i++) for (int j=0 ; j < n * n ; j++) for (int k=0 ; k < j ; k++) sum++; return sum; }
run6
is still possible,
using the same formula as used in Exercise 5.b. Hazard a guess as
to the formula and predict the result for n = 3 and n = 4.
run6
?
#include "loop.h" int run7(int n) { int sum = 0; for (int i=1 ; i < n ; i = i * 2 ) sum++; return sum; }
run7
?#include "loop.h" int run8(int n) { int sum = 0; for (int i=1 ; i <= n ; i++) for (int j=1 ; j <= i * i ; j++) if ( j % i == 0) for (int k=0; k < j ; k++ ) sum++; return sum; }
run8
created December 21, 2021 revised December-January 2021 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |