CS 2223 Mar 13 2018
Expected reading: pp. 3-7, 9, 25, 36-41, 47, 172-175
Expected demonstration: None
Assume you know: pp. 10-35
Daily Exercise: Fewest Comparisons
Classical selection:
Pleyel: Symphony
in C major, Op. 66, B. 154 (1803)
Musical Selection:
Dancing
Queen, Abba (1976)
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.
Here are my solutions to the daily exercises (Cube Sum) and (Anagram). Note that there are 13!=6,227,020,800 possible ways to arrange 13 letters. What algorithm did I use to solve this problem?
I also asked questions about the defective BINARY ARRAY SEARCH. Note you can execute the code in algs.day01.TimingComparison to empirically compute a value. Run it multiple times to see if you can come up with an estimated value, or perform some mathematics to determine the value.
Press to Reveal.
Assume you have a sorted collection of exactly 2n integers
from 1 to 2n. Now you search for each of these numbers in the
collection using the defective BINARY ARRAY SEARCH.
What were your results for:
p(i) – success rate in finding a
number
pe(i) – success rate for even number
po(i) –
success rate for odd number
Answers
1.2 Opening Questions
Let’s pick up with the question I asked you to consider yesterday:
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
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 Mar 23 2018.
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 Binary Search Question
Yesterday I asked if you could compute the probability that a target search actually finds a value from the original sorted array in the performance experiments.
Press to Reveal
Answers
1.7.3 Anagram Finder
Given yesterday’s daily exercise on finding a single-word anagram of 13 letters, it is natural to try to find a way to automatically find an anagram. Let’s raise the stakes on the problem a bit and try to find an anagram for fifteen letters.
Let’s start with the obvious Brute Force approach to compute all different possible permutations of the letters "GRADS TENSE SHINE" to form a fifteen letter word. Note: these implementations can be found in the Git Repository in the class algs.days.day02.Anagram15. Here are some raw results with timing.
0 gradstenseshine Wed Feb 28 13:45:33 EST 2018 50000000 graseindsshnete Wed Feb 28 13:46:05 EST 2018 100000000 grateshseeninds Wed Feb 28 13:46:36 EST 2018 150000000 graeinsseedtshn Wed Feb 28 13:47:08 EST 2018 200000000 grasdteehninsse Wed Feb 28 13:47:39 EST 2018 250000000 graetnenseshids Wed Feb 28 13:48:10 EST 2018 300000000 grasshienedstne Wed Feb 28 13:48:41 EST 2018 350000000 grahinseedntsse Wed Feb 28 13:49:12 EST 2018 ...
So this takes about 31 seconds for 50,000,000 words. Not bad! But remember, this is trying to find 15! = 1,307,674,368,000 possible 15 letter words. At this rate, it will take 13,512 minutes or 9.38 days.
Is there a better programming solution?
1.8 Version : 2018/03/17
(c) 2018, George Heineman