CS 415, Section 001 | Sonoma State University | Spring, 2024 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although much of this course has been well developed in recent semesters, the SSU CS faculty recently have approved a partially-updated course description. Currenly, the Web site is reasonably stable, but modest refinements are possbile.
Since May, 2024, the California Faculty Association (CFA) – the labor union of professors, lecturers, librarians, counselors, and coaches across the 23 California State University campuses – has been in negotiations with the management of the California State University System. After a one-day strike on Monday, January 22, the two sides have reached a tentative agreement, and the strike has been called off. Effective Tuesday, January 23, SSU classes (including CS 415) will be held as scheduled.
Download the file analysis.tar.gz, and decompress it with the line
tar -xvf analysis.tar.gz
Upon decompression, a new directory analysis should have been created, and this directory should contain TestAnalysis.cpp, a main program, a header file loop.h, and functions (run1.cpp) ... run5.cpp)). The program 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 with two arguments.
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.
Run program testAnalysis.cpp
for Exercise 3
with n=100, following the instructions above:
./testAnalysis 3 100
Although timings will vary from machine to machine, `lThe output likely will look similar to the following:
Program to run and time loop-based code segments Running Exercise 3 with time iterations = 300 run repeated 800 squared times result returned: 45000 looping time: 0.00 method execution time 72.34 loop execution time: 72.34
Within the code, adjust the variable NUMBER_REPETITIONS, so that the time reported for "method execution time" is about a minute or a little more.
Now review the code to answer the following:
n
at
100 in each case. To what extent does the "looping time" change
with each experiment?
#include "loop.h" int run1(int n) { int sum = 0; for (int i=0 ; i < 6*n ; i+=3) sum++; return sum; }
w++
is largely equal to x = x + 1
, P is roughly equal to A + S]
Note: In your answer, be sure to give a separate time required for each line of the code. Likely, the amount of time for a line will involve some expression involving at least some of the variables n, A, B, S, M, and P.
run1
,
and explain your conclusions. For example, you likely will want to
highlght which terms will dominate for large n and which will have minimal
impact on the total time.
run1
procedure, with n equal 100, 200, 300, and
400, and record the times.
n = 100
and n = 400
in your expression for
Big-Ω, and divide.)
#include "loop.h" int run2(int n) { int sum = 0; for (int i=0 ; i < n ; i++) sum++; for (int j=0 ; j <= n ; j++) sum++; return sum; }
run2
procedure, with n equal 100, 200,
300, and 400, and record the times.
n = 100
and n = 400
in your expression for
Big-Ω, and divide.)
#include "loop.h" int run3(int n) { int sum = 0; for (int i=0 ; i < n/2 ; i++) for (int j=0 ; j < 2*n ; j++) sum++; return sum; }
run3
procedure, with n equal 100, 200,
300, and 400, and record the times.
n = 100
and n = 400
in your expression for
Big-Θ, and divide.)
#include "loop.h" int run4(int n) { int sum = 0; for (int i = 1 ; i <= n ; i++ ) for (int j = i-2; j <= i+1; j++) sum++; return sum; }
run4
procedure, with n equal 100, 200,
300, and 400, and record the times.
n = 100
and n = 400
in your expression for
Big-Θ, and divide.)
#include "loop.h" int run5(int n) { int sum = 0; for (int i = 1 ; i <= n * n ; i++ ) for (int j = 1; j <= i; j++) sum++; return sum; }
run5
procedure, with n equal 10
and 20, and record the times.
n = 10
and n = 20
in your expression for
Big-Θ, and divide.)
created December 21, 2021 revised December-January 2021 revised Summer 2022 revised 1-2 January 2023 typos corrected 27 January 2023 updated Summer 2023 typo corrected 28 January 2024 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|