*This preview shows
pages
1–3. Sign up to
view the full content.*

COP 3530
Fall 1999
Diagnostic Exam#1 Key
Name:_____________________
1.
The algorithmic (programming) techniques of
recursion
and
iteration
can be related to the mathematical proof
technique of
induction
in a manner that allows inductive proofs of
correctness
and
run-time complexity
.
Show this
relationship by proving that the first of the following two code segments correctly computes N
2
, for N
≥
0, and that the
second has run time complexity 2
N
–1, N
≥
1.
In this latter case, we base complexity on the number of recursive calls
made.
function
sq(N :
integer) : integer;
begin
if
N<=0
then
sq := 0
else
sq := 2*N – 1 + sq(N-1)
end
;
{ sq }
HINT
: Prove S(N): sq(N) = N
2
, N
≥
0
Basis: S(0): sq(0) = 0 by definition of function and fact that 0 <= 0. But, 0
2
= 0 and thus sq(0) = 0
2
✔
IH: Assume, for some N>0, that S(k), for all k <N.
IS: Show S(N). sq(N) = 2*N – 1 + sq(N – 1) by definition of function and fact that N>0.
But, sq(N-1) = (N – 1)
2
by inductive hypothesis.
Hence sq(N)= 2* N – 1 + N
2
– 2*N + 1 = N
2
procedure
Move (n:integer; X, Y, Z:char);
begin
if
n = 1
then
writeln('Move ', X, ' to ', Y)
else
begin
Move (n-1, X, Z, Y);
writeln('Move ', X, ' to ', Y);
Move (n-1, Z, Y, X)
end
end
;
{ Move }
HINT
: Prove S(N): T(N) = 2
N
–1, N
≥
1, where T(1) = 1; T(N) = 2
*
T(N–1) + 1, N>1.
Basis: S(1): T(1) = 1 by definition of function and fact that 1 = 1. For this case, there is only the initial call to Move,
with no subsequent recursive calls.
IH: Assume, for some N>1, that S(k), for all k <N.
IS: Show S(N). T(N) = 2*T(N–1) + 1 by definition of function and fact that N>1. For this case we recursively call Move
twice, each time with its first parameter set to N–1.
But, T(N–1) = 2
N-1
–1 by inductive hypothesis.
Hence T(N)= 2*(2
N-1
–1) + 1 = 2
N
– 2 + 1 = 2
N
– 1

This ** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*C. E. Hughes, UCF Computer Science
– 2 –
COP 3530 Spring ‘99
2.
A
dictionary
is an ADT that responds to the messages
insert
(newWord),
lookup
(oldWord) and
delete
(oldWord).
There are many competing abstract implementations for a dictionary, three of which are a
sorted linear list
(not a
linked list), a
balanced
binary search tree
and a
trie
.
Focusing on the lookup only, I have given informal algorithms
and analyses of the costs of looking up a word.
Discuss the
pros and cons
of each abstract implementation. Be sure to
specify the complexity of the other operations
(insert and delete) for each implementation.
lookup
sorted linear list
Start in the middle of the list. If the word is found, report success.
If not, and the new word is less than the one in the
middle, ignore the bottom half, focusing on the top half of list only. Otherwise, ignore the top half, focusing on the
bottom only. If you run out of words in the list, report failure. The search takes O(logN) operations.
balanced binary search tree

This is the end of the preview. Sign up
to
access the rest of the document.