CS 2223 Mar 27 2023
Visual Selection: Haystacks - White Frost, Sunrise, Claude Monet (1890)
Live Selection: The Entertainer (on Guitar), Chet Atkins (1975)
Jazz Selection: The Man I Love, Mary Lou Williams (1978)
1 Hash Tables
But first... One great rock show can change the world!.
For HW2, be sure that you check out the latest copy of Analysis.java to be sure you have the proper code to model. My apologies, but an older version was in the repository. Your proc should be:
public static long proc(int[] A, int lo, int hi) { long v = power(A[lo], 2) + power(A[hi], 2); if (lo == hi) { return v + power(v, 2); } int m = (lo + hi) / 2; long total = proc(A, lo, m) + 3*proc(A, m+1, hi); while (hi > lo) { total += power(A[lo], 2); lo += 2; // THIS IS CORRECT } return total; }
1.1 Importance of Sorting
Sorting is useful for two reasons – it provides information to humans in a useful order to make it easier for us to process. Using the knowledge of BINARY ARRAY SEARCH, you can see that another reason to sort an array is to reduce the lookup time. However, there is a cost associated with sorting, namely the need to secure a contiguous block of memory, which makes it challenging when new values are added to the collection (or values need to be removed).
We now complete the explanation of a data type that can provide efficient functionality of key operations (i.e., put, get, delete) without demanding all values in a collection are completely sorted.
1.2 Delete information from Symbol Table
What if you want to remove a (key, value) pair from the symbol table? Then you would invoke the delete(key) method. To efficiently remove a node from a linked list, you need to know its previous node. But how can you do this if all of the Node objects only point to the next one?
public void delete(Key key) { if (first == null) { return; } Node prev = null; Node n = first; while (n != null) { if (key.equals (n.key)) { if (prev == null) { // no previous? Must have been first first = n.next; // Be responsible and update first } else { prev.next = n.next; // have previous one link around } return; // Node has been removed, so return now! } prev = n; // advance both, but be careful n = n.next; // to update prev before updating n } }
This logic is repeated in countless ways when working with linked lists. Pay particular attention to how the code needs to determine whether the key to be deleted is the first key in the linked list. Whenever this happens, first needs to be updated to be the second node in the linked list. And if this second node doesn’t exist, then the linked list had only had a single item in the first place, so first is properly set to null.
*MIC DROP*
1.3 Big O Approximation
Assuming that there were N elements in the Symbol Table, what is your analysis of the running time of the core operations, put, get, and delete? State your answer in terms of N.
Let’s come up with some more O(...) examples.
The idea is to come up with a model that can be used to compute the order of growth as N grows. Thus if you have a polynomial equation, you can eliminate the lower order terms regardless of their constants, because as N grows they will matter less and less.
Thus N3 + 1,000,000*N is going to be ~ N3.
Why? Because one N is larger than 100, N3 grows much, much faster than N.
Let’s look at some other examples (exercise 1.4.5, p. 208):
N+1
1 + 1/N
(1 + 1/N)*(1+2/N)
2N3 - 15*N2 + N
log (2N) / log(N)
log (N2+1)/log (N)
N100/2N
1.4 Daily Exercise
Let’s try to analyze the the recursive solution in terms of the maximum number of comparisons?
You should have been able to declare C(n) as the number of comparisons and then defined its behavior as:
C(N) = C(N/2) + C(N/2) + 1
assuming that N=2n as a power of 2.
C(N) = 2*C(N/2) + 1
C(N) = 2*(2*C(N/4) + 1) + 1
C(N) = 2*(2*(2*C(N/8) + 1) + 1) + 1
and this leads to...
C(N) = 8*C(N/8) + 4 + 2 + 1
since N = 2n and we are still at k=3...
C(2n) = 2k*C(N/2k) + (2k-1)
Now we can continue until k = n = log N, which would lead to...
C(2n) = 2n*C(N/2n) + (2n-1)
and since C(1) = 0, we have
C(N) = N - 1
1.5 Thoughts on HW2
Rubric is posted, together with a video giving high-level overview.
Is anyone working on the bonus questions?
1.6 Tomorrow is a WPI Wellness Day
There will be no class tomorrow because it is a wellness day. Please take advantage of this opportunity to spend some time on non-academic activities like hanging out with your friends, watching a movie, or going to a museum.
I will see you all on Thursday.
1.7 Version : 2023/04/07
(c) 2023, George T. Heineman