Sonoma State University | ||
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although CS 415 has been well developed for several years, last year the CS faculty made a significant, long-term curricular change regarding SSU's Upper Division GE Area B Requirement.
Historically, CS Majors could satisfy this requirement by taking CS 454, Theory of Computation, and CS 454 will continue in this role for the next several semesters.
At some time in the future (but not Spring 2025), CS 415, Algorithm Analysis, will allow students to satisfy SSU's Upper Division GE Area B Requirement.
During an anticipated time of transition:
For future semesters, students should check with the CS faculty regarding which course(s) (CS 415 and/or CS 454) will satisfy SSU's Upper Division GE Area B Requirement.
This worksheet is organized into three somewhat-related parts:
Many Web sites provide summation formulas for sums of the form:
for various constants a (e.g., a = 1, 2, 3, ...).
One such reference is Common Computational Formulae from the course's home page.
All programs submitted this semester must follow the SSU CSS Style Guide for C/C++
This section complements the previous Assignment on the Analysis of Non-recursive Algorithms.
Consider the following code segment:
int exercise1(int n) { int sum = 0; /* line 1 */ for (int i=1 ; i <= n*n*n ; i++) /* line 2 */ for (int j=1 ; j <= i*i*i ; j++) /* line 3 */ sum++; /* line 4 */ return sum; }
Perform a careful micro-analysis of the time required for each line of this code, without any simplification using any common computational formilae. In your analysis, use the following constants for the time it takes a machine to do specific operations:
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, ignoring the function call and the return statement. 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.
exercise1
.
n
is large. That
is, for large n
, which terms for total time will
dominate and which will have minimal impact, and why?
Note: An important purpose of parts a and b is to practice a basic analysis of each line of a program, and this work is essential before considering the use of any common computational formulae. Thus, if a computational formula is used in either parts a or b, the score for this problem will be assigned zero points.
Consider the following code segment:
int exercise2(int n) { int sum = 0; /* line 1 */ for (int i=1 ; i <= 2*n*n ; i++) /* line 2 */ for (int j=1 ; j <= i*i ; j++) /* line 3 */ sum++; /* line 4 */ return sum; }
Repeat steps a-f from Exercise 1 for this code.
For this problem, assume n is a power of 2—that is,
n = 2k for an integer k.
Consider the following code segment:
// @pre: n = 2^k for an integer k int exercise3(int n) { int sum = 0; /* line 1 */ for (int i=1 ; i <= n ; i*= 2) /* line 2 */ sum++; /* line 3 */ return sum; }
Repeat steps a-e from Exercise 1 for this code. (Note, you are NOT asked to answer step f in this problem.)
Consider the program simple-loop-analysis-3.c.
Following steps a-e from Exercise 1, give a micro-analysis of the iter_compute procedure to describe its run time. (Note, you are NOT asked to answer step f in this problem.)
On the basis of your answer to part a in this problem, determine if the resulting code has Θ(n), Θ(n2), Θ(n3), Θ(n4), and Θ(n5). In each case, give a careful argument justifying your conclusions.
Referring to program simple-loop-analysis-3.c, develop a recurrence relation that describes the run time of rec_compute procedure. (For this problem, you need not solve the recurrence relation — but wait until later in the semester.)
Consider program fifthPower.c, which computes the fifth power of a positive integer. (Note that part a is not required, but you are expected to complete parts b, c, d, and e.)
computeFifth
function.computeFifth
function.
created December, 2021 revised December-January 2021 expanded August 1, 2022 revised January, 2023 revised Summer, 2023 revised November 20-25, 2024 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2025
by Henry M. Walker.
|