CS 415, Section 001 | Sonoma State University | Spring, 2023 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although much of this course is well developed, some details can be
expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.
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 Worksheet on the Analysis of Non-recursive Algorithms.
Consider the following code segment:
int exercise1(int n) { int sum = 0; for (int i=1 ; i <= n*n*n ; i++) for (int j=1 ; j <= i ; j++) sum++; return sum; }
Perform a careful micro-analysis of the time required for each line of this code. 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. 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?
Consider the following code segment:
int exercise2(int n) { int sum = 0; for (int i=1 ; i <= 2*n ; i++) for (int j=1 ; j <= i*i*i ; j++) sum++; return sum; }
Repeat steps a-e from Exercise 1 for this code.
Consider the following code segment:
int exercise3(int n) { int sum = 0; for (int i=1 ; i <= n ; i*= 2) sum++; return sum; }
Repeat steps a-e from Exercise 1 for this code.
Consider the program simple-loop-analysis-2.c.
Following steps a-e from Exercise 1, give a micro-analysis of the iter_compute procedure to describe its run time.
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.
On a particular machine and compiler, suppose the running time for this code is 5 seconds when n is 800. Based on your answers earlier in this problem, estimate the likely time for the running of this code when n is 1600 and when n is 8000. Justify your answer briefly.
Referring to program simple-loop-analysis-2.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 fourthPower.c, which computes the fourth power of a positive integer. (Note that part a is not required, but you are expected to complete parts b, c, d, and e.)
computeFourth
function.
computeFourth
function.
created December, 2021 revised December-January 2021 expanded August 1, 2022 revised January, 2023 revised Summer, 2023 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|