CS 415, Section 001 Sonoma State University Fall, 2022
 
Algorithm Analysis
Instructor: Henry M. Walker

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

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.

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

This worksheet is organized into three somewhat-related parts:

Reference

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

Once such reference is "Summation formulas" from Tripod

Algorithmic Analysis

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

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

      • 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]

      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.

    2. Once time is determined for each line, add to obtain the total time needed.
    3. If your answer for the total time involves a long summation of terms, use one or more "Summation formulas" from Tripod 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 have minimal impact on the total time, and why?
  2. 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.

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

    1. Following steps a-e from Exercise 1, give a micro-analysis of the iter_compute procedure to describe its run time.

    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.

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

Recurrence Relations

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

Recurrence Relations AND Program Format

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

    1. (optional): Run the program several times to check that it works properly in computing the square 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 html pages.
    4. (to be submitted): Develop a recurrence relation that describes the run time for the computeSquare function.
    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 c.
    Notes:
created December, 2021
revised December-January 2021
expanded August 1, 2022
link corrected September 6, 2022
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2022 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.