In the first three questions of this assignment there is no programming - for those you should write up your answers (proofs or analyses) in a plain-text or Microsoft Word file and submit it as hw12.txt or hw12.doc. For the fourth problem, you should submit your Java program in a file named Wally.java.
You may use your remaining late days on this assignment, except for problem #4, the java problem. Since we will talk about that program on Wednesday, 4/30 and Thursday, 5/1 in class, there are no late days available for that one.
You have been hired by Nanosoft. Your boss, Gill Bates, drops by your spacious cubicle sipping a refreshing and stimulating Jolt Cola ("All the sugar, and twice the caffeine!"). Gill begins by reminding you that a decider is a program that takes an input and eventually halts and accepts (says "yes" to its inputs) or rejects (says "no" to its input). For example, a decider for the prime numbers would take a number, perhaps encoded in binary, as input and would eventually halt saying "yes" (it's prime) or "no" (it's not prime).
Through this part of the assignment, you will be analyzing the running time of algorithms. We will be interested in worst-case analysis. (The best case isn't interesting and the average case is often quite difficult to quantify.) You will need to express the worst-case running time using big-O notation. Finally, make sure to show all of your work in each written problem. Explain each step carefully.
for (int i=1 ; i≤n ; i *= 2)
for (int j=0 ; j<i ; ++j)
// constant time work here
for (int i=1 ; i≤n ; ++i)
for (int j=1 ; j<i ; j *= 2)
// constant time work here
reach(A,A,_). % any node is reachable from itself
% below we ask you to explain what this next line is "saying"
reach(A,B,[[X,Y] | R]) :- reach(A,B,R) ; (reach(A,X,R), reach(Y,B,R)).
For example, here are two representative runs:
?- reach( b, a, [[a,b],[b,c],[c,d],[d,e],[e,f],[f,g],[g,a]]).
Yes
?- reach( b, h, [[a,b],[b,c],[c,d],[d,e],[e,f],[f,g],[g,a]]).
No
Note that we are not concerned with generating any variable/value bindings here,
we are only concerned with checking reachability for a fully-bound set
of inputs.
Briefly explain in English what the second reach rule
is "saying". Then,
write a recurrence relation for T(n), the running
time
of the above reach function in terms of
n,
the number of edges in the graph G. Then, use the
recurrence-unwinding strategy to "unroll"
this recurrence relation in order to find a big-O
running time of this version of reachability. Count each logical
operation ; (or) and , (and) as one step. Show all of
your work!
Professor Dee Videnconker has proposed the following alternative
approach to multiplying two n-bit numbers x and y.
Notice that x has n/2 "upper bits" which we'll call "a" and n/2
"lower bits" which we'll call "b". Similarly, dividing y in two
parts gives us the upper n/2 bits, which we'll call "c", and the
lower n/2 bits, which we'll call "d".
So, x = a 2^{n/2} + b and y = c 2^{n/2} + d.
What's going on here? Notice that
multiplying "a" by 2^{n/2} "shifts" it by n/2 positions.
The number "a" by itself is a n/2 bit number. After the
multiplication by 2^{n/2}, the bits of "a" have been "promoted" so
that we now have a n-bit number in which the first n/2 bits are the
bits of "a" and the last n/2 bits are all 0's. Now, adding "b"
to this gives us a n-bit number in which the first n/2 bits are "a"
and the second n/2 bits are "b". That is, we now have written x in
terms of "a" and "b". We do the analogous thing for expressing y in
terms of "c" and "d".
So, back to Professor Videanconker's algorithm! The algorithm works like this: Divide x into two n/2-bit numbers "a" and "b" and similarly divide y into two n/2 bit-numbers "c" and "d". Now, xy = (2^{n/2}a + b)(2^{n/2}c + d) = 2^{n}ac + 2^{n/2}(ad + bc) + bd. We compute "ab" using a recursive call (now on numbers that are n/2 bits long!) and similarly we compute "ad", "bc", and "bd". After the recursive calls are complete we do the multiplications by 2^{n} and 2^{n/2} followed by some addition.
Notice that multiplying by 2^{k} is just the same as adding k 0's to the end of the number. This can be done in k time steps. Similarly, adding two j-bit numbers can be done in j time steps.
temp1 = (a+b)(c+d)
temp2 = ac
temp3 = bd
z = temp2 2^{n} + (temp1 - temp2 - temp3) 2^{n/2} + temp3
Nothing for you to do here! This is just the description of the
algorithm.
The problem
Wally is unicycling back to Mudd (@ the southwest corner of the map) after spending the
summer in New England (@ the northeast corner). A square grid has been overlaid on the
map, with each cell containing a positive number -- namely, the number of
strawberry-filled DonutMan donuts that Wally will need to consume in order
to have enough energy to pass through that cell.
Your task is to minimize donut consumption. And for this problem specifically, you should write
public static int minDonuts(int[][] map)
This method should return the minimum number of donuts needed to get from the
NE corner of the input map to the SW corner.
The rest is up to you... the algorithm, the software structure (other classes, other methods), and the testing you do.
Example
For example, here is an image of a 5x5 donut-consumption grid and the correct minimum answer. Note that the start cell will be in the final spot in the first row, or
map[0][N-1], and that the destination cell is in the final
spot of the first column, or map[N-1][0], where N is the
length of a side of the square map.
Below it is the path that produced that answer:
The write-up
You should comment your code in order to explain how your algorithm works, as usual.
However, you should also include a comment at the top of your file that includes
Submission
Submit Wally.java in the usual way, under assignment #12.
Scoring
Your solution will receive full correctness credit if it runs in "reasonable" time (under ten seconds on a 6x6 grid) and it works correctly, i.e., it computes the true minimum donut cost. Slow correct solutions will receive almost all of the points (unless they are bogosort-slow). Incorrect solutions, fast or slow, will receive almost no points.
The other half of the credit will be for your algorithm deisgn (not speed, just thoughtfulness), explanation, program design, and commenting.
Extra Credit Options