Algorithms and OOD (CSC 207 2014F) : EBoards

# CSC207.01 2014F, Class 22: Analyzing Algorithms

Overview

• Preliminaries.
• Upcoming Work.
• Extra Credit.
• Questions.
• A quick tour of the reading.
• Exercises in iterative analysis.
• Exercises in solving recurrence relations.
• Sketchy proofs.

## Preliminaries

• Maintain lab partners!
• Office hours
• Normal (MThF 1:45-3:15; walking 1:15-1:45)
• In the Grill 8:00-9:30 am Wed and Fri
• In my office 8:00-9:30 am Tue and Thu
• I've been asked to check if anyone is planning on taking TEC 154 in the spring.
• When you send email questions, please send code and exact error messages.

### Upcoming Work

• HW 7 due tomorrow.
• Exam 2 to be distributed Wednesday. (GHC folks should look online.)
• Linear and Binary Search

### Cool Things Coming to Campus

• Two versions of Anna Christie!
• Live Streaming of Verdi's Macbeth, Saturday at 11:55 a.m.

### Extra Credit Opportunities

• Grinnell Prize Events this week.

#### Peer Support

• Extra credit for the GC AC, Thursday night, Satuday night, Sunday afternoon
• Ajuna's Radio Show at 8pm

### Questions

Why have anonymous inner classes?

Before there were anonymous functions, it was the best way to get "one off" functions or objects into your program. "I want a button that does X".

You can write classes that reference a field of an object without having to deal with significant protection issues. "I want a button that changes my state in this way."

It requires fewer characters.

It might make it easier for someone to read your code; everything is near where you need it.

Do you want a GUI or a TUI for problem 4 on HW7?

I haven't taught you anything about GUI's, so I would assume that you will write a textual user interface. I'm fine if it's relatively primitive: Use a numeric menu to select a type of criterion and then prompt for info on that criterion. Probably at least three different type of criterion. Okay to have only one predicate, though.

If you have extra time and energy, you might permit compound criteria (using and, or, and not) or a syntax for expressing criteria.

How should I compute distance using latitude and longitude?

Ideally, you would look up that information online. For example, a reasonable formula seems to appear at http://www.movable-type.co.uk/scripts/latlong.html

When are you getting grades to us?

I sent out the first two sets of grades from our grader this morning. I expect to have the next set this coming weekend. I also hope to have some detailed comments this weekend.

The grading that I have to do is going to take a bit longer. I spend a long time on administrative paperwork this weekend as well as getting the algorithms reading written and exams sketched.

Part E of HW6 asks you to think about four ways of determining whether an Item has a particular name

Option one: Look at fields. Conceptually easy. Practically hard (Java protection is your friend.) May be impossible if the classes you're working with have private fields.

Option two: `getName()`. Traditionally we write getters, so it's what we should be doing anyway. But ... need to change the interface and write N similar methods.

Option three: `boolean equals(String name)`. Helpful for the remove method, in which the name matches, won't be helpful elsewhere. "Equals" has a particular implication - they are effectively the same object. Ambiguous - at least one person wanted to use the result of `toString` in the comparison.

Option four: `boolean hasName(String name)`. Better version of option three. Reveals less than option two, and perhaps more general.

## Summary

• We want to be able to compare algorithms in terms of their use of system resources.
• CPU (running time)
• Memory (space)
• Looking at approximate models is better than using exact numbers
• Input affects the running time
• External factors affect running time
• Different computers implement the same operations differently
• Instead, look at the shape of the curve of the upper bound of the running time of that algorithm on inputs of size n.
• Notation O(f(n)) - f(n) tells us the shape of the curve
• O(1)
• O(logn)
• O(n)
• O(nlogn)
• O(n^2)
• O(n!) - try not to write algorithms like this
• O(2^n) - try not to write algorithms like this
• Formal definition of big O

## Activity One: Iterative Analysis

What's the running time of the following algorithm? Why? (Discuss with your partner and be prepared to share your analysis with this class.) (The algorithm is expressed in something close to Java, but not actual Java.)

``````/**
* Count all of the values in l for which predicate p holds.
*/
public int countValues(List l, Predicate p)
{
int count = 0;                            // 1 step
for (int i = 0; i < l.length(); i++)      // n repetitions
{                                       // l.length() takes 1, i++ takes 1
if (p.test(l.get(i)))                 // test takes 1, get takes 1
count += 1;                         // increment takes 1
} // for
return count;                             // 1 step
} // countValues
``````
• O(n) [4 groups]

Challenge to that analysis

``````/**
* Count all of the values in l for which predicate p holds.
*/
public int countValues(List l, Predicate p)
{
int count = 0;                            // 1 step
for (int i = 0; i < l.length(); i++)      // n repetitions
{                                       // l.length() takes c*n, i++ takes 1
if (p.test(l.get(i)))                 // test takes 1, get takes 1
count += 1;                         // increment takes 1
} // for
return count;                             // 1 step
} // countValues
``````
• n repetitions of a body that is about n+6 steps
• n(n+6) + 2
• O(n^2)

``````/**
* Count all of the values in l for which predicate p holds.
*/
public int countValues(List l, Predicate p)
{
int count = 0;                    // 1
int len = i.length();             // n steps
for (int i = 0; i < len; i++)     // n repetitions
{
if (p.test(l.get(i)))
count += 1;
} // for
return count;
} // countValues
``````
• Is this now O(n), using the original analysis?
• Can we assume p.test is constant time? Well ... let's do so.
• What about l.get(i)? It takes about i steps to get the ith element.
• Two possiblities:
• Worst case: Always assume i == n-1. So get takes O(n)
• But i is often smaller. Is that a good assumption? Can't we just "unroll" the loop and do 1 + 2 + 3 + ... + n?
• Even if we're really careful on our counting, it's still O(n^2)

Everything after this point was not covered; just planned!

``````/**
* 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 recurrence relations

• f(1) = a; f(n) = b + f(n/2)
• f(1) = a; f(n) = b + f(n-1)
• f(1) = a; f(n) = b + n + f(n-1)
• f(1) = a; f(n) = b + 2*f(n/2)