CS 2223 Mar 25 2021
Expected demonstration: Compare Operation
Assume you know: pp. 10-35
Lecture Challenge: Fewest Comparisons, Empirical Binary Array Search
Classical selection:
Pleyel: Symphony
in C major, Op. 66, B. 154 (1803)
Musical Selection:
Dancing
Queen, Abba (1976)
Visual Selection:
Sunday
Afternoon on the Island of La Grande Jatte, Georges Seurat (1884-86)
Live Selection:
Purple Haze, Jimmi Hendrix (1970)
Daily Question: DAY02
1 Winning isn’t everything, it’s the only thing
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.1 What happens when BINARY ARRAY SEARCH is given unorderd values?
What is the chance that BINARY ARRAY SEARCH can continue to work? I’ve added a RandomTest to empirically quantify this. It runs T=1,000 independent trials, when given an array containing permutations of the integers from 0 to 14 (i.e., a total of 15) in some random order.
What do you suppose is the most number of values that contains() returns true?
What do you suppose is the least number of values that contains() returns true?
Does it matter how big T is?
1.2 Will this loop terminate?
When it comes to reviewing an algorithm to determine that it will properly terminate, it can become difficult. Will the following algorithm terminate?
static long trial (long v) { long numLoops = 0; while (v != 1) { if (v % 2 == 0) { v = v / 2; } else { v = 3*v + 1; } numLoops++; } return numLoops; }
Consider execution as found in FiniteOrInfinite to empirically see whether your intuition is valid.
1.3 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 are used 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.
Here are my solutions to the (Cube Sum) and (BRITNEY SPEARS), (INGRID STEEGER), and (ROGER DALTREY) anagram lecture challenges. 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 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.4 Opening Questions
As a note, to participate in the Discord discussions this term, you will need to join the server. In the Canvas announcements, I have posted the link which should be good for the first week of the course.
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.5 Solutions to questions
Fewest comparisons for largest
We aim to prove 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 element, or n-1.
Review the sample FindMaxRecursively code available for day02 in repository.
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.6 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://gitlab03.wpi.edu/heineman/cs2223
I will make sure to provide videos showing how to access and retrieve the code.
1.7 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.
ThreeSumModified (based on code on page 173 in book)
These can all be demonstrated by launching the Shell program as I will present in class. You don’t have to run Shell directly (I use it during lecture to simplify my presentation).
1.8 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 Apr 05 2021.
1.9 Lecture Challenges
Each day I will present you with the opportunity to exercise and further develop your problem solving skills. These lecture challenges are intended to give you the chance to spend 20 minutes or so thinking about a problem and trying to solve it.
1.9.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.9.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.9.3 Anagram Finder
Given yesterday’s lecture challenge 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 start with the obvious Brute Force approach to compute all different possible permutations of the letters "BRITNEY SPEARS" to form a thirteen letter word. Note: these implementations can be found in the Git Repository in the class Anagram13. Here are some raw results with timing.
0 britneyspears Wed Mar 06 23:21:45 EST 2019 50000000 binarrpyeests Wed Mar 06 23:22:13 EST 2019 100000000 btspeinsearry Wed Mar 06 23:22:39 EST 2019 150000000 bnaeyissrtper Wed Mar 06 23:23:04 EST 2019 200000000 byrtnseraepis Wed Mar 06 23:23:30 EST 2019 ...
This reaches a solution in about 36 minutes, because of the unfortunate situation where the first letter of the discovered anagram, "p", is the 9th letter that it chooses to start words.
The following shows raw timings when using Anagram15 to compute a fifteen-letter anagram of the letters GRADS TENSE SHINE.
0 gradstenseshine Wed Feb 20 21:39:26 EST 2019 50000000 graseindsshnete Wed Feb 20 21:39:57 EST 2019 100000000 grateshseeninds Wed Feb 20 21:40:28 EST 2019 150000000 graeinsseedtshn Wed Feb 20 21:40:58 EST 2019 200000000 grasdteehninsse Wed Feb 20 21:41:29 EST 2019 250000000 graetnenseshids Wed Feb 20 21:41:59 EST 2019 300000000 grasshienedstne Wed Feb 20 21:42:30 EST 2019 350000000 grahinseedntsse Wed Feb 20 21:43:01 EST 2019 ...
So this takes about 27 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 to exhaustively search the entire space.
Is there a better programming solution?
1.10 Daily Question
It’s time for our second Daily Question! Ready? Be sure that you setup your Assistments account, as discussed in class. You can find information in Canvas assignments for each daily question.
The assigned daily question is DAY02
If you have any trouble accessing this question, please let me know immediately on discord.
1.11 Version : 2021/03/28
(c) 2021, George T. Heineman