Algorithms and OOD (CSC 207 2014F) : Outlines

Outline 22: Analyzing Algorithms


Held: Monday, 6 October 2014

Back to Outline 21 - Anonymous Inner Classes. On to Outline 23 - Linear and Binary Search.

Summary

We consider ways, formal and informal, to describe the running time of algorithms.

Related Pages

Overview

Administrivia

Upcoming Work

Cool Things Coming to Campus

Extra Credit Opportunities

Academic

Peer Support

Miscellaneous

Summary

Activity One: Iterative Analysis

What's the running time of the following algorithm? Why?

/**
 * Count all of the values in l for which predicate p holds.
 */
public int countValues(List l, Predicate p)
{
  int count = 0;
  for (int i = 0; i < l.length(); i++)
    {
      if (p.test(l.get(i)))
        count += 1;
    } // for
  return count;
} // countValues

How about this one? Why?

 /**
  * Make a string that contains n copies of str.
  */
 public String replicate(String str, int n)
 {
   String result;
   for (int i = 0; i < n; i++)
     {
       result = result.append(str);
     } // for
   return result;
 } // replicate

Activity Two: Recursive Analysis

Let's try to solve each of these

Activity Three: Proving Things About Big O

You can drop constant multipliers

Big-O is transitive

You can drop lower-order terms

My Old Approach

The following are notes I used the first few times I taught this material in CSC 207. They have since been incorporated in the reading on algorithm analysis.

A Motivating Problem: Exponentiation

Here's a simple iterative solution using a for loop

double result = 1.0;
for (int i = 0; i < y; i++)
    result *= x;

Here's a divide and conquer solution.

 To compute x^y
 If y is 0
   return 1
 Else if y is odd
   return x*x^(y-1)
 Else if y is even
   return square(x^(y/2))

Comparing Algorithms

Potential Problems

Is there an exact number we can provide for the running time of an algorithm? Surprisingly, no.

Asymptotic Analysis

Big-O Formalized

Implications of Big-O

You can formally prove all of the following (and probably will, in some course)

Doing Informal Asymptotic Analysis

Recurrence Relations

Let's try to figure out the running time of a few recursive algorithms given descriptions of the relationships of running times.

I find it easiest to "work out" some example inputs and then to look for patterns.

Experimental Analysis