CS 2223 Mar 26 2021
1 Well Begun Is Half Done
1.1 Important concepts from readings
1.2 Opening Questions
1.3 Data Types
1.3.1 Encapsulation
1.3.2 Design APIs
1.4 Demonstration:   Bisection computation
1.5 Dijkstra’s Two-Stack Algorithm
1.6 Stacks appear in many places
1.7 Lecture Takeaways
1.8 Lecture Challenges
1.8.1 Special Number
1.9 Daily Question
1.10 Interview Challenge
1.11 Version :   2021/  03/  28

CS 2223 Mar 26 2021

Lecture Path: 03
Back Next

Expected reading: pp. 96-99 (Designing Abstract Data Types, Section 1.2), 121-129 (Section 1.3)
Assume you know: pp. 64-89 (Section 1.2)
Lecture Challenges: Permutation strategies, Special Number

Visual Selection:

Daily Question: DAY03

1 Well Begun Is Half Done

1.1 Important concepts from readings

Today I asked you to read about three fundamental data types in computer science. These are Bag, Stack and Queue. Two of these (Stack and Queue) are already implemented by the JDK (as class java.util.Stack and interface java.util.Queue). You need to understand the interfaces for all three data types. You need to understand the choices you make when implementing them.

For the homeworks in this class, you should not use any of the java.util.* classes to complete the assignments. This will be increasingly important, especially in HW2, because I am asking you to learn how to structure data on your own, without taking advantage of fully-formed classes that essentially solve the problem for you.

1.2 Opening Questions

Wirth’s Law: Software is getting slower more rapidly than hardware becomes faster.
Nicholas Wirth

I can’t believe I didn’t show the tournament / bracketology algorithm yesterday. I’ll do it now, so the following table makes sense.

First let’s complete the accurate accounting to find the greatest and second greatest elements in a collection of N elements. Given just a small sample, you can see the following:

Size

Number Operations

2

1

3

3

4

4

7

8

8

9

Find top two values in n + ceiling(log(n)) - 2

If you reflect on the bracket ideas I introduced, you can see that it still takes n-1 number of comparisons to locate the winner. Along the way you have derived useful information to help find the second largest. It can only be on the "path" of the tournament from the winner down to its first position. Given N elements, the height will be ceiling(log n), that is, the smallest integer larger than or equal to the real number of the computation log N. If we start with the final two teams in the tournament as our "#1" and "#2" then we only need to check ceiling(log(n)) - 1 numbers to find the second largest. Thus the maximum number of comparisons to find the top two numbers is n-1+ceiling(log(n))-1 or n + ceiling(log(n)) - 2.

The original code for the Apollo-11 program is available. It contains a routine that sorts three values.

Let’s start with the question I asked you to consider last lecture:

Here is my solution. Discuss.

Isn’t it interesting that you need 7 comparisons to completely sort all five elements but using this approach you need 6 comparisons to find the largest and second largest?

What do you make of the observation that 5 + ceiling(log(5)) - 2 = 6?

This question is of interest theoretically because we still don’t know the fewest number of comparisons to sort sixteen elements. Latest research from 2011 confirms that 46 comparisons suffices and that 44 do not. You would make international news if you could prove that 45 comparisons sufficed. Find the table of best comparison results on Wikipedia.

1.3 Data Types

The famous computer scientist Nicklaus Wirth published an influential book called "Algorithms + Data Structures = Programs (1978)." You need to understand these fundamental data types so you will be ready to tackle the more advanced ones later in this course.

Operation

Bag

Queue

Stack

add

add(Item)

enqueue(Item)

push(Item)

remove

--

dequeue()

pop()

size

size()

size()

size()

isEmpty

isEmpty()

isEmpty()

isEmpty()

We will look at each of these types in turn, as well as the fundamental core API. The notion of an API is found in the discussion from page 96-99. Here are the hilights:

These data types were designed to support well-defined behaviors but they also reflect common real-world scenarios. A Queue, for example, is a good way to model a line in a cafeteria. The first one into the line gets the food before everyone else behind him. A Stack can represent the trays in a cafeteria: You grab the topmost tray, which was the last one placed there. Finally a Bag might represent the coins in the cash register. When the cashier wants to grab a quarter, he just grabs one at random from the bin containing all quarters.

Note that these operations mutate the state of the data type in place.

We will be sure to separate discussions of a Data Type from implementation discussions of a Data Structure.

1.3.1 Encapsulation

A well-designed data type does not reveal its implementation, because that is irrelevant to the outside world. It is kept private and hidden. That said, it will always reveal (explicitly or not) its performance, which can be documented. The fundamental principle is to describe the time it takes to perform an operation in relation to the problem size N. We started this discussion on Mar 25 2021 and we will be prepared to complete on Mar 29 2021.

This relates to material found on page 172-177.

1.3.2 Design APIs

A data type is defined by the behavior that it supports. In this regard, an array is not a data type but is a fundamental data structure, much like an int represents 32-bit integers while a long represents 64-bit integers.

Page 97 of the book lists a number of pitfalls when designing the interface to a data type. In many ways, this is an engineering problem, and most people can recognize a well-designed interface when trying to use it for the first time.

We will aim for the following attributes when designing data type interfaces:

1.4 Demonstration: Bisection computation

One often needs to compute the root of continuous real functions. The root is where the function f(x) cross the x-axis, or in otherwords, where f(x) = 0. The Bisection method works for continuous functions if you are given two indices, a and b, for which f(a) and f(b) have opposite signs.

See code in BisectionMethod.

int signA = (int) Math.signum(f.compute(a)); int signB = (int) Math.signum(f.compute(b)); double c = (a+b)/2; while (Math.abs(b-a) > threshold) { c = (a+b)/2; int signC = (int) Math.signum(f.compute(c)); if (signA == signC) { a = c; } else { b = c; } }

The question to ask is this: How many times does this loop execute?

1.5 Dijkstra’s Two-Stack Algorithm

This algorithm on page 129 uses two stacks to process infix expressions with parentheses.

How do you solve problem "(1 + ((2+3) * (4*5)))"? Observe behavior and discuss.

You must walk through the trace on page 130-131 to verify you see how this algorithm works. On the homework you will see where it breaks down.

Sample Exam Question:

There is an alternate notation known as postfix notation. The above equation would be represented as "1 4 5 * 2 3 + * +". As its name implies, in postfix notation the operator comes after the arguments. Based on the structure of Dijkstra’s algorithm, devise a one stack solution to compute the value of this sequence of tokens.

On the exam, I would ask you to describe this algorithm using pseudocode, which we will be ready to discuss starting Monday.

Note that this is a practical problem. Have you heard of "Postscript"? This is the underyling notation for high-quality documents that, today, are known as PDF documents.

1.6 Stacks appear in many places

There are three engines on the left "A B C" in that order from left to right. You can move an engine straight through to the right track or it can move onto the spur track. Once on the spur track, the engine can only move onto the right track.

Figure 1: Track Manipulation

What is the only permutation of these three engines that you cannot reproduce on the right track?

1.7 Lecture Takeaways

1.8 Lecture Challenges

I described how the Anagram searcher would use a brute force approach to compute all different possible permutations of the words "GRADS TENSE SHINE" to find a fifteen-letter word.

Can you come up with strategies that might be used to improve the performance of this algorithm, given the above output and the sample list of english words.

1.8.1 Special Number

Can you find anything interesting about the number 1255? As a hint, review the homework 1 description and see if any of the questions might shine a light on why this is an interesting number...

1.9 Daily Question

The assigned daily question is DAY03

If you have any trouble accessing this question, please let me know immediately on discord

1.10 Interview Challenge

Each Friday I will post a sample interview challenge. During most technical interviews, you will likely be asked to solve a logical problem so the company can see how you think on your feet, and how you defend your answer.

There are five cups placed on a table upside down in a line. Let’s label them "A B C D E". Under one of the cups is a terrified squirrel. Your goal is to find the squirrel under the following restrictions.

You can lift up exactly one cup at a time. If you find the squirrel you win! If not, then you put the cup back in its original position, upside down again, and turn around and wait for five seconds.

When you are turned around, the squirrel has just enough time to move to one of the cups that is a direct neighbor to the one it was hiding underneath. For example, if the squirrel had been hiding in cup "C", it could move to cups "B" or "D". If it had been hiding under cup "E", then it can only move to cup "D".

Devise an algorithm that will find the squirrel after picking up a finite number of cups.

I will post my solution on Monday.

1.11 Version : 2021/03/28

(c) 2021, George T. Heineman