CS 2223 Mar 15 2022
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 Mathematics of BINARY ARRAY SEARCH
BINARY ARRAY SEARCH cuts the range of values to be searched "more or less in half" with each pass through the while loop. It is worth reviewing how this works in the ideal case, where N is 1 smaller than a power of 2 (such as 7 or 15). The following array has N=7 values in ascending order; as mentioned yesterday, index values are from 0 to N-1.
The first time through the while loop, lo=0, hi=6, and mid=3.
+++++++++++++++++++++++++++++ | 7 | 19| 22| 29| 36| 44| 65| +++++++++++++++++++++++++++++ 0 1 2 3 4 5 6 lo mid hi
If looking for a value smaller than 29 (A[mid] above) then the search continues in the left-hand side by setting hi to mid-1, resulting in the following scenario:
+++++++++++++++++++++++++++++ | 7 | 19| 22|///|///|///|///| +++++++++++++++++++++++++++++ 0 1 2 lo mid hi
Observe that the original collection had 7 values which was 1 smaller than a power of 2. Can you see how this range of 3 values is also 1 smaller than a power of 2? This behavior is common in computer science, namely reducing a problem into a smaller sub-problem while retaining some of the characteristics of the original problem.
So, if you know that the initial array contains N values (where N is 1 smaller than a power of 2) can you figure out the most number of times the while loop in BINARY ARRAY SEARCH executes to locate the value (or determine it wasn’t there in the first place)?
With 1 value, while loop executes 1 time.
With 3 values, while loop executes 2 times.
With 7 values, while loop executes 3 times.
So with 2k-1 values, while loop executes k times.
In short, when N is 1 less than a power of 2, k = log2(N+1).
1.2 Extended BINARY ARRAY SEARCH to count duplicates
One idea might be to use BINARY ARRAY SEARCH to locate the value within the array, and then sweep to the left and to the right.
This inefficient solution is coded in InefficientCountDuplicates.
Why do I claim that it is inefficient? Because when there are a vast number of duplicate values, this solution continues to plod along without taking advantage of the ordered data values.
A better solution (naturally, involves slightly more complicated code) is found in BinarySearchCountDuplicates. I will explain in lecture.
1.3 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.4 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) challenge
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.5 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.6 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 the 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.
I have one more word to add on this subject: Bracketology
1.7 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.8 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.9 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 24 2022.
1.10 Lecture Challenges
Press to Reveal
Answers
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.
Did anyone go ahead and run the code to come up with some ideas?
1.10.1 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.
1.10.2 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.11 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.12 Version : 2022/03/17
(c) 2022, George T. Heineman