404_Review

advertisement
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
I : Function Order of Growth (20 points)
1. (5 points) Given the following list of 3 functions:
3 (n 2 )
25 + ( 8
n)
(n lg n) - 6
Circle the one ordering below in which the 3 functions appear in increasing asymptotic
growth order. That is, find the ordering f1, f2, f3, such that f1= O(f2) and f2= O(f3).
(a)
3 (n 2 )
(b)
25 + ( 8
(c)
(n lg n) - 6
25 + ( 8
n)
n)
(n lg n) - 6
(n lg n) - 6
3 (n 2 )
3 (n 2 )
25 + ( 8
n)
2. (5 points) Given the following list of 3 functions:
(1/6)n!
(2 lg n) + 5
9 (lg( lg n))
Circle the one ordering below in which the 3 functions appear in increasing asymptotic
growth order. That is, find the ordering f1, f2, f3, such that f1= O(f2) and f2= O(f3).
(a)
(1/6)n!
(2 lg n) + 5
9 (lg( lg n))
(b)
(2 lg n) + 5
9 (lg( lg n))
(1/6)n!
(c)
9 (lg( lg n))
(2 lg n) + 5
(1/6)n!
(page 1 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
For problems 3 and 4, assume that:
f1= (n lgn)
f2= ( n2)
f3= (n)
f4= (1)
For each statement:
Circle TRUE if the statement is true.
Circle FALSE if the statement is false.
Circle only one choice.
3. (5 points)
f1= ( f3)
TRUE
FALSE
Explanation:
f1= (nlgn) and nlgn = (n) implies that f1= (n).
f1= (n) and f3= (n) imply that f1= ( f3).
f1
n2
n lg n
f4
n
1
4. (5 points)
f3= ( f2)
TRUE
Explanation:
Counter-example: f2= 1
(page 2 of 43)
FALSE
f2
f3
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
II: Solving a Recurrence (10 points)
In each of the 3 problems below, solve the recurrence by finding a closed-form
function f(n) that represents a tight bound on the asymptotic running time of T(n).
That is, find f(n) such that T(n) =  (f(n)).
1. (5 points) Solve the recurrence : T(n) = T(n/4) + n/2
[You may assume that T(1) = 1 and that n is a power of 2.]
Circle the one answer that gives a correct closed-form solution for T(n)
(a)
(n)
(b)
(n2)
(c)
(n lg n)
Explanation : Use the Master Theorem with a=1, b=4, f(n) = n/2
n log41 = n 0 = 1
Ratio test yields: f(n)/n = (n/2)/1 = n/2
This is case 3, so the solution is (f(n)) = (n/2) = (n)
2. (5 points) Solve the recurrence : T(n) = n T( n ) + n
k
[You may assume that T(2) = 1 and that n is of the form 2 2 .]
Circle the one answer that gives a correct closed-form solution for T(n)
(a)
(n2 lg n)
(b)
(n lg2 n)
(c)
(n lg(lg n))
Explanation : Some ways to solve this: recursion tree, expand to
build a summation, or induction.
Recursion tree has (lglgn) levels and a total of (n) work is
done at each level, for a total of (n lg(lg n)).
Induction Hypothesis: T(k) = k lglg k + k/2 Base Case: T(4) = 6
Inductive Step: T(k2) = k T(k) + k2
= k (k lglg k + k/2) + k2 = k2 lglg k + k2 + k2/2
= k2 ( lglg k + 1 ) + k2/2 = k2 ( lglg k + lg2 ) + k2/2
= k2 ( lg(2lg k)) + k2/2 = k2 lglg k2 + k2/2
(page 3 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
III: PseudoCode Analysis (30 points)
Here you’ll use the pseudocode below for two functions Mystery1 and Mystery3.
Mystery1 has three arguments: A : an array of integers; p, r : indices into A
Mystery1(A, p, r)
if p is equal to r
then return 0
q
(p+r)/2
g1
Mystery1(A, p, q)
print "Mystery1 result1= ", g1
print contents of A[p]..A[q]
g2
Mystery1(A, q+1, r)
print "Mystery1 result2= ", g2
print contents of A[q+1]..A[r]
g3
Mystery3(A, p, q, r)
print "Mystery3 result= ", g3
print contents of A[p]..A[r]
return g3
Mystery3(A, p, q, r)
initialize integer array B to be
empty
bb
p
pp
p
qq
q+1
while pp <= q and qq <= r
do if A[pp] <= A[qq]
then B[bb]
A[pp]
pp
pp + 1
bb
bb + 1
else B[bb]
A[qq]
qq
qq + 1
bb
bb + 1
while qq <= r
do B[bb]
A[qq]
qq
qq + 1
bb
bb + 1
while pp <= q
do B[bb]
A[pp]
pp
pp + 1
bb
bb + 1
a
0
for i
p to r
do A[i]
B[i]
if i>p
then d
|A[i] - A[i-1]|
if d > a
then a
d
return a
(page 4 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(a) (10 points) For A = <5,2,16,9,25>, what output is generated by the call
Mystery1(A, 1, 5)?
Solution:
A[1..5]= 5 2 16 9 25
Mystery1 result1= 0
A[1..1]= 5
Mystery1 result2= 0
A[2..2]= 2
Mystery3 result= 3
A[1..2]= 2 5
Mystery1 result1= 3
A[1..2]= 2 5
Mystery1 result2= 0
A[3..3]= 16
Mystery3 result= 11
A[1..3]= 2 5 16
Mystery1 result1= 11
A[1..3]= 2 5 16
Mystery1 result1= 0
A[4..4]= 9
Mystery1 result2= 0
A[5..5]= 25
Mystery3 result= 16
A[4..5]= 9 25
Mystery1 result2= 16
A[4..5]= 9 25
Mystery3 result= 9
A[1..5]= 2 5 9 16 25
(b) (5 points) Describe in one or two sentences the mathematical meaning of the
result of a call Mystery1(A, 1, length[A]) on an arbitrary integer array A.
Answer: Let A’ be the array A sorted in nondecreasing order. Mystery1 returns
the maximum difference between two elements that are adjacent in A’. This is
the maximum integer gap between two elements in sorted order.
(page 5 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(c) (10 points) Find a function f(n) that can be used to describe a tight bound on
the worst-case asymptotic running time T(n) of Mystery1, where n is the number
of elements in array A.
That is, find f(n) such that T(n) =  (f(n)).
(You may ignore floors and ceilings in your analysis.)
Solution: Let T1(n) = time for Mystery 3
Then T(n) = T( n/2 ) + T( n/2 ) + T1(n)
Ignoring ceilings and floors yields: T(n) = 2T(n/2) + T1(n)
T1(n) =  (n)
[ Mystery3 code is essentially the same as the
Merge code from MergeSort. The only difference
is the calculation of the maximum gap in the
sorted sequence. This does not change the
asymptotic running time.]
Thus, T(n) = 2T(n/2) + (n)
Now use the Master Theorem to solve the recurrence.
The ratio test yields (n)/n, which is case 2.
Thus, T(n) =  (n lg n)
(d) (5 points) Does the time bound from (c) also describe the best-case and
average-case asymptotic running time T(n) of Mystery1? Briefly explain.
Solution: Yes, just as in MergeSort the running time for best-case and
average-case inputs is the same as for worst-case inputs: T(n) =  (n lg n).
This is because:
- the depth of the recursion depends only on the number of elements in
the array A and not on the values stored in it;
- the temporary array B must be copied back into array A in the merge
step; this operation take time that is linear in the number of elements in
the part of A being considered and is independent of the values stored
in it.
(page 6 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
IV: Patriotic Trees (40 points)
This problem is based on the definition of a PatrioticTree in the Final
Exam Handout (see attachment).
1. (10 points) Consider a PatrioticTree TreeNode t and the two
TreeNodes t.left and t.right representing its two children. How many
different colorings of the 3 TreeNodes t, t.left and t.right are
possible? (circle the one correct answer below)
27
13
23
9
17
Explanation: If all possible combinations were possible, the number
would be 27. However, the color of t depends on the relationship
between t.left.r + t.right.r and t.left.b + t.right.b. Based on this, we
make the following observations:
- Case 1: Both children have the same color: If t.left.color is W and
t.right.color is W, then t.left.r + t.right.r = t.left.b + t.right.b so t.color
is W. This type case contributes 3 possibilities, because similar
arguments show that whenever both children are the same color
the parent has that same color; this excludes 6 possibilities in
which the parent has a different color.
- Case 2: Children have different colors, but one child is W. If
t.left.color is R and t.right.color is W, then t.left.r > t.left.b and
t.right.r = t.right.b, so t.left.r + t.right.r > t.left.b + t.right.b. In this
case, t.color is R. This case contributes 4 possibilities because
the case of left subtree’s color W and right subtree’s color R is
symmetric and the R/W argument also holds when one subtree is
B and one is W. Note that we cannot have one W subtree and a
W parent; this excludes 4 possibilities. We also cannot have a
non-W parent be a different color than its non-W child; this
excludes 4 more possibilities.
- Case 3: Children have different colors and neither child is W. The
parent may have any color in this case. This contributes 6
possibilities and excludes none.
The total number of possibilities is therefore 13. Note that of the 27,
14 are excluded.
(page 7 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
2. (5 points) An algorithm MAX_R_COLORING accepts as input a
list of initialized TreeNodes representing leaves of a PatrioticTree
(initialization provides weight and color for each leaf). Assume that
the sum of the RedWeights of the leaves exceeds the sum of the
BlueWeights of the leaves. Assume also that MAX_R_COLORING
always returns a PatrioticTree (built from the leaves up) in which the
number of R-colored internal nodes is maximized. Then the number
of R-colored internal nodes in the tree built by MAX_R_COLORING
is always: (circle the one correct answer below)
2f
2f - 1
f +1
f-1
Explanation: A PatrioticTree that has f leaf nodes has
f-1 internal nodes. If the
RedWeight total exceeds the BlueWeight total, then all of them can have color R if:
- No internal node has two children that are both internal nodes.
- The only internal node that has 2 leaves as children has two R-colored children. As
many direct ancestors as possible of this internal node have R-colored parents that
have two R-colored children. As soon as having two R-colored children becomes
impossible, as many direct ancestors as possible have R-colored parents that have
one W-colored child and one R-colored child.
Descending from the root, let a be the first internal node that is either W or R-colored.
Then, all ancestors of a must be R-colored. This is because a’s Red Weight is the sum
of the Red Weights of all R-colored leaves. If the root is R-colored, then this sum
exceeds the sum of the Blue Weights of all B-colored leaves. Thus, for each ancestor of
a (which has one B-colored child), the Red Weight is guaranteed to exceed the Blue
Weight.
[Note that similar logic shows that if the root is B-colored, then the tree can be
constructed so that all f-1 internal nodes are B-colored. However, this is not necessarily
true when the root is W-colored.]
(page 8 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
3. (25 points) Given a collection of f leaf nodes, develop the
algorithm MAX_R_COLORING as described in Problem 2.
In addition to all assumptions listed in the statement of Problem 2,
you may assume that:
- There are w white leaves and they are stored in an array W of
TreeNodes
- There are r red leaves and they are stored in an array R of
TreeNodes
- There are b blue leaves and they are stored in an array B of
TreeNodes
a) (15 points) Provide pseudo-code for the algorithm MAX_R_COLORING
public static TreeNode MAX_R_COLORING (TreeNode[] R,
TreeNode[] W, TreeNode[] B) {
TreeNode parent = R[0];
int i;
for (i=1; i<R.length; i++) {
parent = makeParent(parent, R[i]);
}
for (i=0; i<W.length; i++) {
parent = makeParent(parent, W[i]);
}
for (i=0; i<B.length; i++) {
parent = makeParent(parent, B[i]);
}
return parent;
public static TreeNode makeParent(TreeNode leftChild,
TreeNode rightChild) {
String parentColor = "R";
int parentRedWeight = leftChild.r + rightChild.r;
int parentBlueWeight = leftChild.b + rightChild.b;
TreeNode parent = new TreeNode(parentColor, parentRedWeight,
parentBlueWeight, null, leftChild, rightChild);
leftChild.parent = parent;
rightChild.parent = parent;
(page 9 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
return parent;
}
b) (5 points) Justify the correctness of the algorithm
Solution: See problem 2’s answer above.
c) (5 points) Establish an upper bound on the algorithm’s worst-case
running time in terms of the total number of nodes n.
T(n) =  (n).
Explanation: makeParent is O(1). MAX_R_COLORING takes
time in O(n)O(1) = O(n) for each of its 3 loops. This gives O(n)
overall.
(page 10 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
FINAL EXAM HANDOUT
40% of the points on the Final Exam will be based on the
representation described in this handout.
We define a new type of tree as follows:
A PatrioticTree is a binary tree of n nodes in which:
- Every internal node has both a left and a right child.
- Each node is labeled with a single color (RED, WHITE or BLUE,
which we abbreviate as R, W, or B).
- Each node has a weight. The weight is a pair of integer values
and is of the form <r,b>. The first integer value in the pair is the
Red Weight of the node = Red Weight of the node’s left child +
Red Weight of the node’s right child. The second integer value in
the pair is the Blue Weight of the node = Blue Weight of the node’s
left child + Blue Weight of the node’s right child. The color of a
node is determined as follows:
- R if its Red Weight exceeds its Blue Weight
- B if its Blue Weight exceeds its Red Weight
- W if its Blue Weight equals its Red Weight
- The weight of each W leaf is <0,0>.
- The weight of each R leaf is of the form <r,0>.
- The weight of each B leaf is of the form <0,b>.
B
<9,14>
EXAMPLE of a PatrioticTree:
In this example:
B
<7,8>
B
<2,6>
- there are 8 leaf nodes
- there are 7 internal nodes
(including the root)
- the total number of
nodes is 15
R
<2,0>
B
<0,6>
W
<0,0>
B
<0,5>
B
<0,5>
(page 11 of 43)
B
<3,8>
R
<4,0>
B
<2,6>
W
<0,0>
W
<3,3>
B
<0,3>
R
<3,0>
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
FINAL EXAM HANDOUT
(continued)
For algorithmic purposes, we represent a PatrioticTree as follows:
A TreeNode represents a node of a PatrioticTree. It contains the attributes:
-
color: a character representing its color: “R”, “B” or “W”
-
r: an integer representing its Red Weight
parent
-
b: an integer representing its Blue Weight
color
-
parent: a pointer to the parent of this node
-
left: a pointer to the left subtree of this node
-
right: a pointer to the right subtree of this node
r
b
left
right
You can access a node’s attributes using the “.” notation (e.g. for a TreeNode t,
t.color gives its color and t.left accesses its left subtree).
EXAMPLE of how to
represent the PatrioticTree
on the previous page:
B
9
14
B
B
2
6
B
2
8
R
W
6
0
B
4
0
0
3
B
R
2
7
0
0
8
W
B
6
0
5
B
0
(page 12 of 43)
3
3
5
0
R
B
W
0
0
3
3
0
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
1: (20 points) Given the recurrence:
T(n) = T(n/2) + 3T(n/3) + 4T(n/4) + 7n2
(a) (10 points) Show that T(n) = O(n3)
Solution: T(n/4) <= T(n/3) <= T(n/2)
T(n) <= T(n/2) + 3T(n/2) + 4T(n/2) + 7n2
= 8T(n/2) + 7n2
Now use the Master Theorem with a=8, b=2, f(n) = 7n2
n log28 = n 3
Ratio test yields: f(n)/n = 7n2/n = 7/n
This is case 1, so the solution is (n 3)
Since T(n) <= (n 3), we obtain T(n) = O(n 3)
(b) (10 points) Show that T(n) = (n3/2)
Solution1: T(n/2) >= T(n/3) >= T(n/4)
T(n) >= T(n/4) + 3T(n/4) + 4T(n/4) + 7n2
= 8T(n/4) + 7n2
Now use the Master Theorem with a=8, b=4, f(n) = 7n2
n log48 = n 3/2
Ratio test yields: f(n)/n = 7n2/n3/2 = 7 n1/2
This is case 3, so the solution is ( n2 )
Since T(n) = ( n2 ) which is in ( n3/2 ), we obtain via
transitivity T(n) is in ( n3/2 ).
Solution2: Observe that T(n) >= 7n2 since
(T (n/4) + 3T(n/4) + 4T(n/4)) is positive.
Now, since n2 is in ( n3/2 ),
by transitivity we can conclude that T(n) = ( n3/2 ).
(page 13 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
2: True/False (30 points) Circle TRUE if the statement is true. Circle
FALSE if the statement is false. Circle only one choice.
(a) (15 points)
If f1= n 2n , f2 = 2n+1 then f1= ( f2 )
TRUE
FALSE
Explanation:
f1= ( f2 ) would imply f1 <= c f2 for all n >= n0 which would imply
n 2n <= c 2(2n) and therefore n <= 2 c . This, however, would
require c to depend on n, which violates the conditions for the
definition of O( ).
(b) (15 points) Clustering for dynamically resizable hash tables
Define a dynamically resizable hash table to be one in which, whenever the hash
table becomes full, the size of the hash table is doubled, and all entries are
removed from the small hash table and inserted into the new, larger hash table.
Consider a hash table H1 of size m and a hashing function
h1(k) = k mod
m. Let H2 be of size 2m. Suppose that open addressing with linear probing is
used for both H1 and H2. Let the hashing function for H2 be h2(k) = k mod 2m.
If, for the keys k1, k2, ..., kb it is the case that
k1 mod m = k2 mod m = ... = kb mod m
and when these keys are inserted (in the order given) into H1 they
occupy consecutive slots in H1, then if only k1, k2, ..., kb are inserted
into H2 (in the same order) they also occupy consecutive slots in H2.
TRUE
FALSE
Explanation:
k1 mod m = k2 mod m = ... = kb mod m does not imply
k1 mod 2m = k2 mod 2m = ... = kb mod 2m
As a counter-example, consider m = 7 , k1 = 10 , k2 = 17 , k3 = 24. All 3 keys
hash (under h1 ) to the value 3. However, under h2 they do not hash to the same
value: 10 mod 14 = 10, 17 mod 14 = 3 and 24 mod 14 = 10.
(page 14 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
3: (50 points) This is based on the Exam Handout.
distributed on 11/17 (see attachment).
(a) (5 points) Here we examine the amount of storage required for this
representation. If each Key Node counts as one unit of storage and each
Neighbor Ring Node also counts as one unit, give (and briefly justify) a tight
(upper and lower) bound on the worst-case number of storage units needed for
this representation as a function of n, the total number of Key Nodes.
Solution:
One for each KeyNode yields n.
There can be
n neighbors in the worst case; for each there are
2
2 NeighborRingNodes.
This yields a worst-case total of

n+2
n
2
=
n

= (n2)
2
(b) (5 points) A neighbor triple is a triple of Key Nodes <k1, k2, k3 > such that
each pair within the triple has the neighbor relation. (In the example in the exam
handout, the nodes with keys a, b, and c are in a neighbor triple.) In the worst
case, how many neighbor triples can there be (as a function of n, the total
number of Key Nodes)?
Clarification: We consider all neighbor triples containing k1, k2, and k3 to be the same.
Solution:
n = (n3)

3
(page 15 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(c) (40 points) Using the neighbor triple definition in (b):
I.
Given a reference (pointer) to a Key Node k, give pseudo-code to decide if
k is part of a neighbor triple. If k is part of a neighbor triple, the pseudocode should print out “yes” plus the 3 key values of a neighbor triple. (You
need not find all triples; you may stop after finding the first one.) If k is not
part of any neighbor triple, the pseudo-code should print out “no”.
II.
Justify the correctness of your algorithm.
III.
Give (and justify) a tight (upper and lower) bound on your algorithm’s
worst-case asymptotic running time as a function of n, the total number of
Key Nodes.
A Solution:
I. findTriple(k1) [Comment: In this pseudocode we rename k to be k1]
tripleFound
false
k1Current
k1.ring
if k1Current = nil
then do [Comment: check each neighbor of k1 to build a < k1, k2> pair]
k2
k1Current.neighbor.keyNode
k2Current
k2.ring
if k2Current = nil
then do [Comment: check each neighbor of k2 to build a < k2, k3> pair]
k3
k2Current.neighbor.keyNode
k3Current
k3.ring
if k3Current = null and k3 = k2
then do [Comment: check each neighbor of k3
to build a < k3, k1> pair. 3 pairs yield a triple < k1, k2, k3>]
if k3Current.neighbor.keyNode = k1
then print "yes”, k1.key, k2.key, k3.key
tripleFound
true
k3Current
k3Current.neighborCW
while k3Current = k3.ring and tripleFound = false
k2Current
k2Current.neighborCW
while k2Current = k2.ring and tripleFound = false
k1Current
k1Current.neighborCW
while k1Current = k1.ring and tripleFound = false
if tripleFound = false
then print "no"
(page 16 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
II. Correctness:
A KeyNode k1 is part of a neighbor triple if and only if we can find
KeyNodes k2 and k3 such that:
-
<k1, k2> is a neighbor pair
-
<k2, k3> is a neighbor pair
-
<k1, k3> is a neighbor pair
In terms of NeighborRingNodes, we seek k1Current, k2Current and k3Current such that:
-
k1Current.neighbor = k2Current and k2Current.neighbor = k1Current
-
k2Current.neighbor = k3Current and k3Current.neighbor = k2Current
-
k1Current.neighbor = k3Current and k3Current.neighbor = k1Current
If the underlying data structure is correctly represented, then whenever
kiCurrent.neighbor = kjCurrent, kjCurrent.neighbor = kiCurrent. Therefore, we need only
check that: k1Current.neighbor = k2Current, k2Current.neighbor = k3Current, and
k3Current.neighbor = k1Current.
To accomplish this the algorithm contains 3 nested while loops. Each loop
traverses a neighbor ring and identifies one of three neighbor pairs. The outer loop
traverses k1’s neighbor ring using k1Current to point to the current NeighborRingNode;
each ring node points to a neighbor of k1 and therefore yields a pair <k1,k2>. The
algorithm uses k2Current to traverse k2’s ring; each ring node points to a neighbor of k2
and therefore yields a pair <k2,k3>. The algorithm uses k3Current to traverse k3’s ring;
each ring node points to a neighbor of k3 and therefore yields a pair involving k3. If the
neighbor in this pair is k1, this yields a pair <k1,k3> which completes the triple
<k1,k2,k3>.
The algorithm checks for special cases such as nil pointers. It also uses a boolean
flag tripleFound to short-circuit if it finds a neighbor triple.
III. Running Time: The algorithm’s running time is dominated by the 3 nested while
loops, so its time is in  (n3). For the lower bound, we can construct a situation in which:
-
the n KeyNodes are in 3 groups A, B, C of n/3 each
-
each node in A is a neighbor of each node in B
-
each node in B is a neighbor of each node in C
-
no other nodes are neighbors.
(page 17 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
In this situation, there are no neighbor triples but the algorithm must do (n/3)(n/3)(n/3)
work to prove this. This worst-case example gives us a worst-case lower bound of 
(n3), which allows us to conclude that the worst-case running time is  (n3).
Note that having all key nodes be neighbors of each other does not force the algorithm to
do  (n3) work. This is because, for all choices of k1, k2 and k3, the triple <k1,k2,k3>
exists. So, if we start with k1, then the first neighbor ring node of k1 that we examine
leads us to a neighbor k2 that is definitely part of a triple that includes the pair <k1,k2>.
We therefore need not examine the remaining neighbor ring nodes of k1’s ring. Similarly,
once we enter k2’s neighbor ring node, we need only examine one other k2 neighbor ring
node to find k3 that is part of a <k2,k3> pair. Since the triple <k1,k2,k3> exists, we can
find a pointer to k1’s neighbor ring via k3’s neighbor ring. However, we may have to
search all of k3’s ring in order to find that pointer. This causes O(n) work. Thus, the first
2 loops degenerate to constant-time operations and the total work is O(n) in the “all
neighbors” case.
(page 18 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
Exam Handout
This reinforces material from Chapters 1-12.
Date Distributed: Friday, 11/17
The open-book exam on Monday, 11/20 will contain a 50-point question
based on the scenario described below.
In this problem, there are two types of list nodes:
1) a Key Node that contains the attributes:
key
-
key: a key value
info
-
info: satellite data
ring
-
ring: a pointer to a linked list of Neighbor Ring Nodes
neighborCCW
neighborCW
neighbor
We call this list a Neighbor Ring.
keyNode
2) a Neighbor Ring Node that contains the attributes:
-
keyNode: a pointer to the Key Node for this Neighbor Ring
-
neighbor: a pointer to a Neighbor Ring Node for a neighbor of this Key
Node’s Neighbor Ring
-
neighborCCW: a pointer to the next Neighbor Ring Node in this Key
Node’s Neighbor Ring (where next is in the counter-clockwise direction)
-
neighborCW: a pointer to the next Neighbor Ring Node in this Key Node’s
Neighbor Ring (where next is in the clockwise direction)
You can access a node’s attributes using the “.” notation (e.g. for a Key Node k,
k.key gives its key and for a Neighbor Ring Node n, n.neighbor gives its
neighbor).
The neighbor relation is symmetric: if a is a neighbor of b, then b is a neighbor of
a.
You may assume that there is a total of n Key Nodes.
(See Example on Next Page)
[Note: Representations that use neighbor rings are often useful in graphics and
geometric modeling applications.]
(page 19 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
Example:
-
Nodes with key values a and b are neighbors
-
Nodes with key values a and c are neighbors
-
Nodes with key values b and c are neighbors
-
Nodes with key values c and d are neighbors
neighborCCW
a key
info
ring
b key
neighborCCW
neighborCW
neighborCW
neighbor
info
neighbor
keyNode
ring
keyNode
neighborCCW
neighborCCW
neighborCW
neighborCW
neighbor
neighbor
keyNode
keyNode
neighborCCW
neighborCCW
neighborCW
neighborCW
neighbor
neighbor
keyNode
keyNode
d key
info
c key
neighborCCW
info
ring
neighborCW
neighborCCW
neighbor
neighborCW
keyNode
neighbor
keyNode
(page 20 of 43)
ring
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
3. (10 points) Minimum Spanning Trees: For the undirected, weighted graph
below:
2
4
A
3
B
G
6
5
1
E
6
8
1
7
F
4
D
C
2
Show 2 different Minimum Spanning Trees. Draw each using one of the
2 graph copies below. Thicken an edge to make it part of a spanning
tree.
What is the sum of the edge weights for each of your Minimum
Spanning Trees?
Answer: 16
2
4
A
3
6
5
E
6
2
4
A
3
G
1
D
B
8
1
C
D
(page 21 of 43)
6
5
E
6
F
4
2
G
1
7
B
8
1
7
F
4
2
C
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
4. (5 points) For the directed, weighted graph below, find the shortest
path that begins at vertex A and ends at vertex F.
2
4
A
3
G
6
5
1
E
6
D
B
8
1
7
F
4
2
C
a) (4 points) List the vertices in the order that they appear on that path
Answer: A, D, F
Dijkstra: A D B C E F G vertex
- A A B D D B predecessor
1 1 2 3 7 9 6 distance
b) (1 point) What is the sum of the edge weights of that path?
Answer: 9
(page 22 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
I : Multiple Choice (10 points)
1. (5 points) Function Order of Growth
Given the following list of 3 functions:
(n2 lg n) - 6
(n lg3 n) + 12n
(5n/6)2
Circle the one ordering below in which the 3 functions appear in increasing asymptotic
growth order. That is, find the ordering f1, f2, f3, such that f1= O(f2) and f2= O(f3).
(a)
(n lg3 n) + 12n
(5n/6)2
(n2 lg n) - 6
(b)
(5n/6)2
(n lg3 n) + 12n
(n2 lg n) - 6
(c)
(5n/6)2
(n2 lg n) - 6
(n lg3 n) + 12n
2. (5 points) Solve a Recurrence
In the problem below, solve the recurrence by finding a closed-form function f(n)
that represents a tight bound on the asymptotic running time of T(n).
That is, find f(n) such that T(n) =  (f(n)).
Solve the recurrence:
T(n) = 9T(n/3) + (n+2)(n-2)
[You may assume that T(1) = 1 and that n is a power of 3.]
Circle the one answer that gives a correct closed-form solution for T(n)
(n)
(b)
(n2)
(c) (n lg n)
(d) (n2 lg n)
Explanation : Use the Master Theorem with a=9, b=3, f(n) =
(n+2)(n-2) = n2 – 4
(a)
n logba = n log39 = n 2
Ratio test shows that : f(n) = (n logba)
This is case 2, so the solution is T(n) = ( n logba lg n) = ( n2 lg n)
(page 23 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
II: True/False (15 points)
For each problem in this section:
Circle TRUE if the statement is true.
Circle FALSE if the statement is false.
Circle only one choice.
1. (5 points) Function Order of Growth: What can you conclude?
Assume that:
f1(n)= (2n)
f3(n)= (f2(n))
f 3(n)= (n)
f4(n)= (n3)
If we also know that f3(n) = ( f4(n)), then we can always
conclude that f2(n) = ( n3).
TRUE
FALSE
Explanation:
f3(n)= (f2(n))
(by symmetry) f2(n)= (f3(n))
(by definition) f2(n)= ( f3(n))
f3(n) = ( f4(n)) and f4(n)= (n3)
(by transitivity) f3(n)= (n3)
f2(n)= ( f3(n)) and f3(n)= (n3)
(by transitivity) f2(n)= (n3)
f4(n)
2n
f2(n) f3(n)
f1(n)
n3
n
(page 24 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
2. (5 points) Bounds on Alg vs Problem/ Types of input
Given a problem P, suppose that:
-
someone designs algorithm A1 to solve P
someone designs algorithm A2 to solve P
T(Ai) represents the asymptotic running time of algorithm Ai
someone constructs a proof that solving an arbitrary instance of P
requires at least time in (n).
the designer of algorithm A1 claims that algorithm A1 is in O(2n) for
worst-case inputs
the designer of algorithm A2 claims that algorithm A2 is in O(n2) for
worst-case inputs
If all these proofs and claims are correct, then we know that
T(A2) is in (n)
and T(A2) is in (n3)
TRUE
FALSE
Explanation:
-
If an arbitrary instance of P requires at least time in (n), then T(A2) is in
(n). The worst-case running time of A2 is O(n2) implies that T(A2) is in
(n2). Since n2 is in (n3), by transitivity of O(), we therefore obtain T(A2)
is in (n3).
(page 25 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
3. (5 points) The tree shown below on the right can be a DFS tree for some
adjacency list representation of the graph shown below on the left.
TRUE
A
FALSE
B
A
Tree Edge
F
Back
Edge
C
Tree Edge
E
B
C
D
Tree Edge
Tree Edge
Tree Edge
F
ECross Edge
D
Answer: FALSE. Although it is possible for C to be visited before B if the
adjacency list has C listed before B, the tree shown has a cross edge, which is
illegal for a DFS tree of an undirected graph.
(page 26 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
1: True/False (20 points = 10 points each)
For each statement:
Circle TRUE if the statement is true.
Circle FALSE if the statement is false.
Circle only one choice.
(a) if f1= (f2) then f1= (f2) and f2= (f1)
TRUE
FALSE
Explanation:
f1= (f2) implies f1= (f2) and f1= (f2).
f1= (f2) implies that: f1 >= c f2 for all n >= n0, which implies that:
(1/c )f1 >= f2 for all n >= n0 (since c is positive).
(1/c )f1 >= f2 implies that f2= (f1).
(b) if f1=  (n2) and f2= (n) and f3= n lgn
TRUE
FALSE
then f3=  (f1) and f3= (f2)
Explanation:
n2
f1
2
Counter-example: f1 = n and f2= n
Note: A common mistake was to assume
that f3=  (n2) implies f3=  (f1)
and f3=  (n) implies f3=  (f2)
(page 27 of 43)
n lg n
n
f2
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
2: Multiple Choice (30 points)
For each statement:
circle the one answer that is true.
M
We define a new type of tree as follows: An i/3-tree is one in which:
- Each node may have children of the following types: left-child,
middle-child, or right-child. Each node is labeled with its type as
follows: L for left-child, M for middle-child and R for right-child
L
(we consider the root to be a middle child).
- Each node has at most i children (see picture on the right for an
example of a 2/3-tree).
A complete i/3-tree is an i/3-tree in which each node has exactly i
children (clarification: at all levels except the lowest).
3
Solution
to extra
credit:
At each level, each node has i children, and there are i different possibilities
for those i children. In general, the number of such trees that have j levels is:
total number of nodes in
3
a i/3 tree that has j-1 levels
i
j-2
3
=
i
k=0 i k

5 points (a) The number of different complete 3/3-trees with 13 nodes is:
(i) 27
(ii) 1
(iii) 3
(iv) 18
(v)
9
Explanation:
For n=13 and i=3, there are 3 levels. The formula above yields 1 in this case.
10 points (b) The number of different complete 1/3-trees with 3 nodes is:
(i) 27
(ii) 16
(iii) 3
(iv) 18
(v)
9
Explanation:
For n=3 and i=1, there are 3 levels. The formula above yields 9 in this case.
(page 28 of 43)
L
R
M
M
R
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
15 points
(c) If we construct a 1/3-tree randomly so that, for each node, the
probability that it has a left child is the same as the probability that it
has a right child and the same as the probability that it has a middle
child, then the expected number of middle children (including the
root node) in a complete 1/3-tree with 3 total nodes is:
(i) 9
(ii) 18/16
(iii) 3
(iv) 15/9
(v)
2
Explanation:
The expected number of middle children is
3
k=1
k (Prob( k middle children))
= 1(Prob of 1 middle child) + 2(Prob of 2 middle children) + 3(Prob of 3 middle children)
Now, the probability of having k middle children = (number of outcomes
containing k middle children)/(total number of outcomes).
Total number of outcomes is the answer to (c), which is 9.
Number of outcomes containing 1 middle child = 4
Number of outcomes containing 2 middle children = 4
Number of outcomes containing 3 middle children = 1
Prob of 1 middle child = 4/9
Prob of 2 middle children = 4/9
Prob of 3 middle children = 1/9
The expected number of middle children = 1(4/9) + 2(4/9) + 3(1/9) = 15/9
Extra Credit: 20 points
The number of different complete 2/3-trees with 7 nodes is:
(i) 27
(ii) 16
(iii) 3
(iv) 18
(v)
9
Explanation:
For n=7 and i=2, there are 3 levels. The formula above yields 27 in this case.
Derive a general formula for the number of different complete i/3trees, where the tree has j levels.
Answer: See the formula above on previous page.
(page 29 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 30 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 31 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 32 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 33 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 34 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 35 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 36 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(page 37 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
3. (50 points) This question is based on the Exam Handout distributed on 10/27 (see
attachment to exam).
Clarifications:
-
The delivery schedule Si only needs to contain the messageID, not the entire
message.
-
Message processing algorithm operates on entire queue to produce Delivery
Schedule; no new messages added during this processing.
For a single node vi:
(a) (5 points) Where in the message processing algorithm is sorting required in
order to produce Si ? Explain.
Explanation:
Ordering in delivery schedule depends on: (1) message priority and (2) for equal
priorities, the arrival order in the queue. To satisfy (1), messages must be sorted
by (non-increasing) delivery priority. Arrival order in the queue is given and does
not require a sort as long as the sort on priorities is stable. Priority sort must
occur before or during formation of the delivery schedule.
(b) (30 points) Assuming that fast asymptotic running time is the most important
judgment criterion, what type of sorting algorithm(s) is (are) appropriate for use
within the message processing algorithm?
Justify your answer. For each sorting algorithm that you select, your answer
should describe the algorithm by writing pseudo-code and stating:
(i) whether it is a comparison-based sort or a non-comparison-based sort
(ii) in more detail what type of sorting algorithm it is. For example, is it a
MergeSort? an InsertionSort? a HeapSort? a BucketSort? a RadixSort? A
variation on one of these types?
(iii) the worst-case asymptotic running time
(iv) the worst-case size of the delivery list (clarification: this is Si)
(v) the amount of extra storage you use (for data structures other than Ci, Qi, Si)
(page 38 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
Note: You need not write out pseudo-code that appears in our textbook; simply cite the page
reference. However, if you modify pseudo-code that appears in our textbook, you must write
pseudo-code for your modifications. You may assume the existence of a queue data structure
that has the following O(1) time operations: enQueue(Q, x), deQueue(Q,x), (clarification: and
deQueue(Q)) isEmpty(Q). You may also assume that the operations dynamically allocate
storage as needed.
Pseudo-Code for Delivery Schedule Construction for a node:
Note: assumptions:
-
Qi is initialized to contain the messages for node vi.
-
10 buckets (queues) B[1]....B[10] are initialized to empty
-
We use an Object-Oriented notation below
buildS() {
// Put each message in a priority bucket
while(not (Qi.isEmpty()))
do m
Qi.deQueue()
B[m.priority].enQueue(m);
// Transfer buckets into Delivery Schedule
for i
10 downto 1 by -1
do while(not (B[i].isEmpty()))
do m
B[i].deQueue()
Si.enQueue( m.messageID )
(i) We know ahead of time that each delivery priority is an integer between 1 and
10, so we can use a non-comparison-based sort to determine the order in which
messages appear in the delivery schedule. We create 10 buckets – one for each
priority. For each priority value, we add messages to the bucket’s queue in the
order in which they appear in the message queue; this guarantees that the
(page 39 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
correct ordering will be enforced in the event of priority ties and removes the
need to sort within each bucket. Because there is no comparison-based sorting
within each bucket, this bucket sort is a purely non-comparison-based sort.
(ii) This sort is a bucket sort, with one bucket per priority value. We do not need
to sort inside each bucket because the queue ordering suffices and our sort is
stable. Note that our bucket sort differs from the bucket sort in our text. In the
text all the key values are mapped onto a unit-interval, put into n buckets (if n is
the number of elements being sorted), and then each bucket is sorted. In our
bucket sort here, we use one bucket for each priority value; our number of
buckets is independent of the number of messages.
(iii) Assuming that the message queue stores messages in the order in which
they were received, in the worst case our bucket sort requires (mi) time. This is
because each message is examined once as it is put onto a queue B[i]. Each
message is also processed once to add it to the delivery list. Note that the text’s
bucket sort requires (n) expected time; it is not linear worst case time because
each bucket must be sorted. We do not need to sort within each bucket, so we
can achieve (mi) time.
(iv) In the worst case our bucket sort creates a delivery schedule that contains
(mi) messages.
(v) In the worst case our bucket sort uses (mi) extra storage
Note: There are other good ways to achieve a linear-time (worst-case) result
here (e.g. counting sort). A purely comparison-based sort requires (n lg n)
time and is therefore too slow for this problem in which we specified that fast
asymptotic running time is the most important consideration.
(page 40 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
(c) (15 points) Describe how your results for (b) change if we change our
assumptions so that a message priority may have any integer value larger than 0.
Revised Pseudo-Code for Delivery Schedule Construction for a node:
Note: Assume: Qi is initialized to contain the messages for node vi.
buildS() {
MergeSort(Q, 1, mi )
while(not (Qi.isEmpty()))
do m
Qi.deQueue()
Si.enQueue( m.messageID )
(i) Because we cannot any longer make a strong assumption about the priority
values, a linear-time (worst-case) sort is now not appropriate. We switch to a
comparison-based sort with (mi lg mi) running time. It must be a stable sort
because it must preserve the ordering of elements in the queue that have the
same priority value.
(ii) Because MergeSort is stable, we use MergeSort.
(iii) Assuming that the message queue stores messages in the order in which
they were received, in the worst case our bucket sort requires  (mi lg mi) time.
(iv) In the worst case the delivery schedule still contains (mi) messages.
(v) In the worst case our revised algorithm uses (mi) extra storage if MergeSort
does not sort in place.
Note: Alternatively, we could still use a bucket sort, but instead of using one
bucket per priority we could use the text’s bucket sort approach. If we do this, we
must use a stable sort within each bucket. We only get a linear-time expected
running time result in this case.
(page 41 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
Exam Handout
This reinforces material from Chapters 1-11.
Date Distributed: Friday, 10/27
The open-book exam on Wednesday, 11/1 will contain a 50-point question
based on the scenario described below.
Suppose that you are working for a company that designs message processing
algorithms for managing message deliveries on a network. For the purposes of the
algorithm, the network is modeled as a collection of vertices in an undirected graph G
(refer to Chapter 5 if necessary for graph definitions). Each vertex represents a node in
the network. If there is a direct network connection between two nodes in the network,
then the vertices representing those two nodes have an edge in the graph G. (Note: We
assume that two nodes have a direct network connection if and only if a message sent
from one node to the other does not need to pass through any additional nodes in the
network.) Each node can only send messages to nodes to which it is directly connected.
Each message consists of a header and a body. The header consists of metadata that
describes the message: a message ID, its destination list and its delivery priority. A
destination is another node in the network to which the message must be sent; we
represent as an integer (e.g. 0 for vertex v0). A destination list is an ordered list of
destinations. The message must visit its destinations in the order in which they appear in
its destination list: i.e. visit the first, then the second, and, ultimately, the last. The
message’s delivery priority is an integer in between 1 and 10, where 10 is the highest
priority value. The message ID is a unique integer for each message.
Each node/vertex vi has:
-
a message processor Pi that can execute algorithms;
-
a FIFO message queue Qi containing mi messages;
-
a list Ci of the nodes to which it is directly connected.
When a message arrives at vi, the first destination in its destination list (which is vi) is
removed from the message header. If vi is the final destination (that is, the destination
list is now empty), the message is not added to Qi. If the next destination (which is now
first in the destination list) is not in Ci, the message is not added to Qi. Otherwise, the
message is added to Qi.
(page 42 of 43)
UMass Lowell CS
Fall, 2001
Sample Review Questions & Answers for 91.404 Material
The message processing algorithm that executes on Pi must examine Qi and produce a
delivery schedule Si. The schedule Si lists the messages that vi will send. In Si, the
messages appear in the order in which they will be sent. This order is determined based
on the delivery priority and the order in which the messages appear in the message
queue Qi. The order is consistent with the delivery priorities and the arrival order in Qi is
used to break ties.
v1
C1
Example:
C2
2
1
3
v3
G
v2
These will remain the same as algorithm(s) execute
These will change as algorithm(s) execute.
Here we show an example for one time period.
v1
Q1
S1
v2
Q2
S2
messageID: 1
messageID: 2
destinationList: 2, 3
destinationList: 2
priority: 5
priority: 10
messageID: 2
messageID: 1
messageID: 4
messageID: 5
messageID: 6
destinationList: 1
destinationList: 3
destinationList: 1
priority: 6
priority: 8
priority: 8
messageID: 5
messageID: 6
messageID: 4
messageID: 3
v3
Q3
destinationList: 2
priority: 1
(page 43 of 43)
S3
messageID: 3
C3
2
Download