CS 2223 Oct 29 2015
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
BINARY ARRAY SEARCH
Recursion and thinking recursively
Simplified input (StdIn) and output (StdOut) which use from Sedgewick to simplify the processing of input.
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:
What is the fewest number of comparisons to determine the largest integer in the array. Can you prove this?
What is the fewest number of comparisons to determine the largest and the 2nd largest integer in the array. Can you prove this?
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
How would you characaterize the input array in this best case?
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?
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.
CompareOperation
Bisection Experiment
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.
BinarySearch
ThreeSumModified
These can all be demonstrated by launching the Shell program as I will present in class.
1.6 Lecture Takeaways
Analyzing algorithms is based on counting the number of times key operations occur. These include loops as well as costly operations, such as comparing two values or swapping two elements in an array.
You are cleared to work on Homework HW1 which is due 2PM Friday November 6th.
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