30 points; to be done individually
Submitting these answers Include your answers to these problems in a plain-text part1.txt file.
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!
40 points; may be done in a pair or individually
Suppose we have two files on disk. Each file contains n words (one per line), each file may or may not be in alphabetical order, and each file may or may not contain duplicates. We want to write a single file containing the "union" of the words in the input files. That is, the output file should contain the words that were in one or both input files, but it does not have to be in order. In the following, you may assume that reading or writing a single word in a text file takes constant time. All the collections discussed support iterating through the elements in O(1) time per element.