CS 415, Section 001 | Sonoma State University | Fall, 2022 |
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, ...).
Once such reference is "Summation formulas" from Tripod
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 ; i++) for (int j=1 ; j <= i*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 have minimal
impact on the total time, and why?
Consider the following code segment:
int exercise2(int n) { int sum = 0; for (int i=1 ; i ≤ n*n ; i++) for (int j=1 ; j ≤ i ; j++) sum++; return sum; }
Repeat steps a-e from Exercise 1 for this code.
Consider the program simple-loop-analysis.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.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 square.c, which computes the square of a positive integer. (Note that part a is not required, but you are expected to complete parts b, c, d, and e.)
computeSquare
function.
computeSquare
function.
created December, 2021 revised December-January 2021 expanded August 1, 2022 link corrected September 6, 2022 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|