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:  Analysis of Nonrecursive Algorithms

Summary

In this lab, you will analyze the complexity of some code and experimentally test your analyses.

Preparation

Download the file analysis.tgz, and decompress it with the line

tar -xvf analysis.tgz

Upon decompression, a new directory analysis should have been created, and this directory should contain TestAnalysis.cpp, a main program, and functions (run1.cpp) ... run5.cpp)). TestAnalysis.cpp allows you to conduct experiments for the subsequent exercises. To compile this program use the line:

make TestAnalysis

The program uses command-line input.

For example, to run the code for Exercise 1 with n=100, you would type

 ./testAnalysis 1 100

Note: In this lab, a variable NUMBER_REPETITIONS is specified to repeat a segment of code NUMBER_REPETITIONS2 times. This allows reasonable timings to be compared within the accuracy of the clock.

Exercises

Exercise 0

Run program testAnalysis.cpp for Exercise 1 with n=100, following the instructions above:

 ./testAnalysis 1 100

Although timings will vary from machine to machine, `lThe output likely will look similar to the following:

  Program to run and time loop-based code segments
  Running Exercise 1 with time iterations = 100
         run repeated 8000 squared times
   result returned:  50
   looping time:        0.12 
   method execution:    3.93 
   execution time:      3.82 

Now review the code to answer the following:

  1. Explain what is meant by the output line, "run repeated 8000 squared times".
  2. What is measured by the "looping time"?
  3. Run the program several times with the same command-line parameters. To what extent does the "looping time" vary from run to tun?
  4. Run the program several times with different values for the Exercise number (but keeping the value of n at 100 in each case. To what extent does the "looping time" change with each experiment?
  5. What is measured by the "method execution" time?
  6. What is measured by the "execution time"?

Exercise 1

run1.cpp
#include "loop.h"

int run1(int n) {
  
  int sum = 0;

  for (int i=0 ; i < n ; i+=2)
    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:

    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 run1.
  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?
  6. Run the run1 procedure, with n equal 100 and 400.
    1. How many times longer does the time for 400 take versus the time for 100? (Hint: Simply divided the larger time by the smaller time to answer this question.)
    2. Based on your determination of Big-Ω, how many times longer would the code for 400 take over the time for 100? (Hint: Use n = 100 and n = 400 in your expression for Big-Ω, and divide.)
    3. To what extent is the factor for actual run time similar to the factor based on Big-Ω?

Exercise 2

run2.cpp
#include "loop.h"

int run2(int n) {

  int sum = 0;
  
  for (int i=0 ; i < n ; i++)
    for (int j=0 ; j < n ; j++)
      sum++;
  
  return sum;
}

Repeat steps a-f from Exercise 1 for this code.

Exercise 3

run3.cpp
#include "loop.h"

int run3(int n) {

  int sum = 0;

  for (int i=0 ; i < n ; i++)
    sum++;
  for (int j=0 ; j < n ; j++)
    sum++;
  
  return sum;
}

Repeat steps a-f from Exercise 1 for this code.

Exercise 4

run4.cpp
#include "loop.h"

int run4(int n) {

  int sum = 0;

  for (int i=0 ; i < n ; i++)
    for (int j=0 ; j < n * n ; j++)
      sum++;

  return sum;
}

Repeat steps a-f from Exercise 1 for this code.

Exercise 5

run5.cpp
#include "loop.h"

int run5(int n) {

   int sum = 0;

   for (int i=1 ; i <=n ; i = i * 2 )
     sum++;
   
   return sum;
 }

Repeat steps a-f from Exercise 1 for this code.

created December 21, 2021
revised December-January 2021
revised Summer 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.