Sonoma State University
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

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.

Assignment on Analysis of Non-recursive Algorithms, Recurrence Relations, and Program Format

This worksheet is organized into three somewhat-related parts:

References

  1. Many Web sites provide summation formulas for sums of the form:

    1a + 2a + 3a + ... + na

    for various constants a (e.g., a = 1, 2, 3, ...).

    One such reference is Common Computational Formulae from the course's home page.

  2. All programs submitted this semester must follow the SSU CSS Style Guide for C/C++

Algorithmic Analysis

This section complements the previous Assignment on the Analysis of Non-recursive Algorithms.

  1. 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;
          }
        
    1. 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:

      • A — one assignment
      • B — one evaluation of a Boolean (e.g., a comparison)
      • S — one addition or subtraction
      • M — one multiplication or division
      • P — one increment (e.g., ++) [since the line w++ is largely equal to x = x + 1, P is roughly equal to A + S]
      • R — one function/procedure return

      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.

    2. Once time is determined for each line, add to obtain the total time needed. (Again, using any common computational formilae is not allowed for this part.)
    3. If your answer for the total time involves a long summation of terms, use one or more Common Computational Formulae to obtain a relatively simple algebraic expression for the total time.
    4. Use your result for the total time to determine the Big-O, Big-Ω, and Big-Θ running times for exercise1.
    5. Explain your conclusions for Big-O, Big-Ω, and Big-Θ, by indicating what term[s] in the expression for total time will dominate when n is large. That is, for large n, which terms for total time will dominate and which will have minimal impact, and why?
    6. 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.

    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.

  2. 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.

  3. 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.)

  4. Consider the program simple-loop-analysis-3.c.

    1. 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.)

    2. 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.

Recurrence Relations

  1. 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.)

Recurrence Relations AND Program Format

  1. 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.)

    1. (optional): Run the program several times to check that it works properly in computing the fifth power of postive integers.
    2. (to be submitted): The program is terribly formatted and hard to read. Reformat the program, so that it follows the C/C++ Style Guide
    3. (to be submitted): Run Doxygen on the reformatted program, and print the resulting html pages.
    4. (to be submitted): Develop a recurrence relation that describes the run time for the computeFifth function.
      Notes: In determining this recurrence relation, remember
      • a complete recurrence relation requires at least one base case and at least one recursive case.
      • the recursive case should relate the work when computing the run time for n to the run time for n-1.
      • throughout the focus should be run time for the computation, not the actual value of n5.
    5. (to be submitted): Use the method of backward substitution (from Levitin, Section 2.4) to determine a solution to the recurrence relation from part d.
    Notes:
created December, 2021
revised December-January 2021
expanded August 1, 2022
revised January, 2023
revised Summer, 2023
revised November 20-25, 2024
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2025 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.