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

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

Worksheet:  Analysis of Recursive and Non-recursive Algorithms

Instructions:

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

Problems

  1. Consider the following code segment:

        int sum = 0;
    
        for (int i=1 ; i <= n * n; i+= n)
          for (int j=1 ; j <= 10 ; j++)
            for (int k=0; k < i ; k++ )
                sum++;
    
    1. Develop an algebraic expression for the function f(n) that describes the time required for processing, given n. (If the time depends upon specific details of input, consider a "worst-case" analysis.)

    2. Determine if the resulting code has O(n), O(n2), O(n3), O(n4), and O(n5). In each case, give a careful argument justifying your conclusions.

    3. Determine if the resulting code has Θ(n), Θ(n2), Θ(n3), Θ(n4), and Θ(n5). In each case, give a careful argument justifying your conclusions.

    4. Naturally, the running time of this code segment will depend upon the particular computer and compiler used. On a particular machine and compiler, suppose the running time for this code is 8 seconds when n is 1000. Based on your answers earlier in this problem, estimate the likely time for the running of this code when n is 2000 and when n is 5000. Justify your answer briefly.

    Notes

  2. Consider the C program simple-loop-analysis.c.

    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, determine if the resulting code has Θ(n), Θ(n2), Θ(n3), Θ(n4), and Θ(n5). In each case, give a careful argument justifying your conclusions.

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

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

  3. Review the recurrence examples from Appendix B in the text, and study the discussion of the Master Theorem that begins in page 490.

    1. Find two examples that illustrate Case 1 of the Master Theorem .
    2. Find two examples that illustrate Case 2 of the Master Theorem .
    3. Find two examples that illustrate Case 3 of the Master Theorem .

    Notes:

created December, 2021
revised December-January 2021
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.