1 Winning isn’t everything, it’s the only thing
1.1 Important concepts from readings
1.2 Opening Questions
1.3 Solutions to questions
1.4 Demonstration: Eclipse and Java
1.5 Demonstration
1.6 Lecture Takeaways
1.7 Daily Exercises
1.7.1 Fewest Comparisons
1.7.2 Closed form formulas
1.8 Version : 2015/ 11/ 03

CS 2223 Oct 29 2015

Lecture Path: 02
Back Next

Expected reading: pp. 3-7, 9, 25, 36-41, 47, 172-175
Expected demonstration: None
Assume you know: pp. 10-35
Daily Exercise: Two questions

Just think of it! If I hadn’t hit on music I should never have been able to do anything in the world!
Giacomo Puccini

1 Winning isn’t everything, it’s the only thing

1.1 Important concepts from readings

In reviewing these readings, pay attention to these concepts

Also, the goal for today is to show in class how to execute Java code in Eclipse. In particular, how to execute code using Sedgewick’s sample code that he uses throughout the book, and we will as well.

I will put together a number of videos for Java help once I assign HW1.

Finally, I have to adjust the schedule/syllabus for the entire class, because Tuesday was a Friday schedule! More in class on Thursday.

I held online office hours Tuesday night but no one showed.

Here are my solutions to the daily exercises (Anagram) and (Fib Sum). Note that for fifteen letters, there are 15! or 1,307,674,368,000 ways of rearranging the letters.

1.2 Opening Questions

Let’s pick up with the question I asked you to consider on Tuesday:

1.3 Solutions to questions

Fewest comparisons for largest

We aim to provide the following statement, P(n): Given an array with n>1 elements, you need n-1 comparisons to determine its largest element.

Let’s prove by Mathematical Induction.

Base case: if you have n=2 elements, you need one comparison to determine the largest item, or n-1.

Inductive case: Assume it holds for n elements. What happens when there are n+1 elements? First you compute the largest element, max, of the first n elements, which you can do in n-1 comparisons. Now compare max against the n+1th element using one additional comparison to determine the largest element. The total number of comparisons is n-1+1 or n comparisons.

Since both the basis and the inductive step have been performed, by mathematical induction, the statement P(n) holds for all natural numbers n. Q.E.D.

Are you satisfied? Has this proven the claim?

Fewest comparisons to determine largest and 2nd largest

We assume n>2. A naive algorithm would look like the following:

Algorithms are represented in pseudocode for simplicity

# Given array A of n>2 elements largest = A[0] next = A[1] if next > largest swap next, largest # we now largest & smallest of first 2 elements for i = 2 to n-1 if A[i] > largest next = largest largest = A[i] else if A[i] > next next = A[i] print "largest is ", largest, " and next one is ", next

How would you characaterize the input array in this best case?

In the best case, the fewest number of comparisons will also be n-1. However, in the worst case, you will need to make two comparisons for each of the remaining n-2 elements, so the total number of comparisons is 2*(n-2)+1 or 2n-3.

Is this best we can do? Given the readings for today, do you have any inspiration?

I could prove the above claim using induction but are you satisfied?

Figure 1: Ranking Image

The purpose of this exercise is to show that you can accurately account for all performance costs for a given algorithm. Using this cost model, you will be able to show that one algorithm will outperform another algorithm even before you have completed their implementations. In science, this is known as prediction, and science is eminently concerned with the predictive qualities of models and theories.

1.4 Demonstration: Eclipse and Java

As we get started, I want to make sure we are all on the same page. You should have installed Eclipse and I assume your machine already has a Java installation. Now in Eclipse, retrieve the latest version of the CS 2223 Git repository. Recall that this can be found here:

https://fusion.wpi.edu/anonscm/git/cs2223/cs2223.git

And you can anonymously retrieve the code and data samples using Git within Eclipse.

1.5 Demonstration

Let’s start with some questions about performance and counting operations.

The following demonstrations are from Sedgewick. I have taken these from the book and made it easier for you to execute the programs that you, yourselves, will be typing in from the book.

These can all be demonstrated by launching the Shell program as I will present in class.

1.6 Lecture Takeaways

1.7 Daily Exercises

Each day I will present you with the opportunity to exercise and further develop your problem solving skills. These daily exercises are intended to give you the chance to spend 20 minutes or so thinking about a problem and trying to solve it.

1.7.1 Fewest Comparisons

You are given five values, A, B, C, D and E. What is the LEAST number of comparisons you need to place these five values into ascending order.
Take notes in your course note book as you attempt to solve this problem.

1.7.2 Closed form formulas

Given yesterday’s daily exercise on Fibonacci numbers, try your hand at this one:

Recall that we used Sn to represent the sum of the first n Fibonacci numbers, Σ Fn. What if we wanted to compute the second-order sum, Tn = Σ Sn. That is, can you come up with a recursive formula that computes Tn? Oddly enough, this one is a bit easier. Take notes in your course note book as you attempt to solve this problem.

1.8 Version : 2015/11/03

(c) 2015, George Heineman