Professional Documents
Culture Documents
Name:
Student Number:
cats
1 Induction Proofs
Following is a claimed theorem and a possible induction proof. In the questions following it, decide whether it is
correct, and explain your reasons.
Theorem In any nonempty list of positive integers, the sum of the elements is less than or equal to the product of the
elements.
Proof
The base case is one element. Then the sum = the product.
For lists having n > 1 elements, let E be the first element. Let Srest and Prest be the sum and product of the remaining
elements of the list, respectively. By the inductive hypothesis, Srest Prest . Since E 1, E + Srest E Prest .
QED
A.
B.
C.
Grading note: The grade is based on the complete answer, so a correct yes/no for a totally bogus reason doesnt get
any points, while a wrong yes/no for an almost correct reason would get some part credit.
What are the three major categories of ADT operations? Briefly explain the purpose of each category.
B. You are about to write a C program that uses a SymbolTable abstract data type. Another programmer
has written symtab.h, symtab.c, and symtabtester.c. Rank these files from best to worst as sources of
information about how to use this ADT.
C (2 pts). In reading the specification for a Stack ADT, you see
void pop(Stack s) Precondition: s is not empty. : : :
How would you normally expect to find out within a client program whether this precondition is satisfied?
CMPS 101 Fall, 2013
Handout 07
SYNTAX
4 Big-
Give Big- expressions for each problem; for full credit they must be fully simplified.
n
X
k
=1
n
X
=1
lg(k) =
n=2
X
i i
=1
1) =
5 log
n
X2
j
=1
2j =
log(n) 2 O(n2 )
If f (n) 2 (nE ); then f (n) 2
(nE ):
f (n)
! 0; then f (n) is in O(g(n)):
If
g (n)
n
log n 2
(n2 )
If f (n) 2
(nE ); then f (n) 2 (nE ):
f (n)
! 0; then f (n) isnt in O(g(n)):
If
g (n)
n
Handout 07
Multiple Choice 10 to 20 questions, all topics may be asked: Circle the correct choice out of four to complete
each statement for which you know the answer.
2 points per correct answer, 0 points if blank, and 1 points if wrong answer.
1 pn and 1 n
2
100
2pn log n and 2pn
1 pn log n and 1 pn
2
2
2pn and 10 log n
(b) f (n) is in
(g (n)) but not in O(g (n)).
Let b 1 and c > 1 be constants. Let T (n) be defined on the positive integers by
( ) = b T (n=c) + f (n)
log2 b . Then T (n) can be bounded asymptotically as
with T (0) = 1. Further, define the critical exponent, E =
log2 c
T n
follows:
5 Recurrence Equations
Give Big- expressions for each problem; for full credit they must be fully simplified. (Show work to ensure part
credit.)
( ) = 1 + T n2
T n
T n
(1) = 1
T n
( ) = n + T n2
T n
( ) = n + 2 T n3
T n
(1) = 10
( ) = 1 + 2 T n2
(1) = 10
(1) = 1
( ) = n + 2 T n2
(1) = 1
( ) = lg(n) + 2 T n3
T n
(1) = 1
Handout 07
6 Recursion Trees
Show the top three levels of the recursion tree for each recurrence equation. The top level should have one node that
represents T (n). Do not solve.
(A)
(C)
(B)
( ) = T k2 + T k3 + C k
T k
( ) = 2T
T k
( ) = T (k 1) + T (k 2) + B
T k
+Ck
8 Java and C
Some symbols almost always mean the same in Java and C, while a few often mean something different. Among these
symbols, a few will be selected for you to decide whether they are usually the same meaning or not.
! % & * ( - + = { | [ : " ; < ? , . /
The selections will be common symbols, not obscure ones.
9 Other problems
Some problems might be similar to Exercises 3.7, 3.8, 3.10, 3.12. However, exact solutions of recurrences will not be
asked, only asymptotic orders.
Handout 07
1 2 3 4 5 6 7 8 9 10 11 12
32 56 30 70 81 31 11 21 12 41
B.
Give the asymptotic growths rates (big ) for Quicksort in worst case? average case? best case?
Handout 07
1 2 3 4 5 6 7 8 9 10 11 12
41 56 32 30 70 81 31 11
B.
Give the asymptotic growths rates (big ) for Mergesort in worst case? average case? best case:?
C. Describe in correct sequence the compares and moves that merge does on the two arrays below. (Just showing
the result is not enough!)
1 2 3 4
01 04 07 20
1 2 3 4
02 21 23 26
1 2 3 4 5 6 7 8 9
13 Heap Operations
A.
Illustrate the operation of a single deleteMax by showing successive pictures. Do not use accelerated fixHeap().
Reread the above line. Reread the above line. Reread the above line.
Handout 07
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
75 45 70 40 30 15 10 20 35 25
B.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
75 45 70 40 30 15 10 20 35 25
C.
Suppose a heap holds n elements. What are the worst-case big running times of deleteMax? heapInsert?
Handout 07
14 Sorting Debate
To prepare for a debate in which you do not know which side you will need to argue, prepare a 2D table that gives one
or two significant advantages (not an exhaustive comparison) of the sorting method (in each row, leftmost column)
over its competitors (listed in subsequent columns).
1. The four methods to compare are heap, insertion, merge, and quick sorts.
2. The advantages may be in time and/or space.
3. You may consider best, average, and worst cases to make your debating point.
4. Be specific about growth rates (()) involved for the compared methods.
5. Constant-factor advantages are not considered significant for this question.
6. The correct answer might be none for certain cases. None is acceptable if the only differences are in constant
factors.
There should be a total of 15 entries among the 12 nondiagonal cases, counting nones, if any.
Example: in bubble sorts row and wander sorts column the entry might be (n2 ) worst case time vs. (n!).
over
heap sort
over
insertion sort
over
merge sort
over
quick sort
advantage of
heap sort
advantage of
insertion sort
advantage of
merge sort
advantage of
quick sort
Handout 07