Session 10

Implementing Algorithms

CS 3530
Design and Analysis of Algorithms

Opening Exercise

Today let's do a little retrospective of Homework 2. Work in groups of two or three. Discuss these questions and share your answers.


We often spend too little time discussing programs... Students sometimes discuss problems and programs while doing assignments, but... Take stock. Share.

What was unclear in the spec?

Good design or programming choices?

What were the biggest challenges in this assignment?

A couple of other questions: What did you learn? What else do you hope to learn?

Code, Code, Code

Look at my Ruby solution...

Look at my Java solution...

How might we use this code to count the number of base operations executed -- to help us analyze these algorithms empirically? Consider ways to "tool" the code...

Now a Big Question:

How do you know your code is correct?

Look at my tests: Ruby versus Java versus Python.

Follow-up on Exam 1

Let's make a quick pass through Exam 1.

Problem 1: Anagram

(I have code!

Problem 2: Definitions.

Your explanations >> your actual answers.

Problem 3: Mismatch

(I wish I had code...)

Problem 4: Insertion Sort

(How can we improve this algorithm? Swap only once!)

Problem 5: Recursion

This is quite similar to a problem we did when learning to analyze a recursive algorithm in Session 8:
    R(0)                    = 0
    R(n) =                    R(n-1) + 1
         = (R(n-2) + 1) + 1 = R(n-2) + 2
         = (R(n-3) + 1) + 2 = R(n-3) + 3
         = (R(n-4) + 1) + 3 = R(n-4) + 4
           ...              = R(n-i) + i
     [substitute i = n]     = R(n-n) + n
                            = R(0)   + n
                            =  0     + n
                            = n

That shouldn't be two surprising -- the algorithm makes a linear walk from n to 0!

(You can use the same technique to compute the value of foo(n)... Try it!)

After an exam, it is a really good idea to solve the problems again in the leisure of your own room. Write code. Ask questions.

Wrap Up

Eugene Wallingford ..... ..... February 13, 2014