Algorithms Homework Help
Algorithms Homework Help
(a) (b)
def frequentest_a(X): def frequentest_b(X):
k = max(X) k = max(X)
H = {} A = []
for x in X: for i in range(k + 1):
H[x] = 0 A.append(0)
best = X[0] best = X[0]
for x in X: for x in X:
H[x] += 1 A[x] += 1
if H[x] > H[best]: if A[x] > A[best]:
best = x best = x
return best return best
i. Worst-case: i. Worst-case
Solution: O(n + k)
Solution: O(n2)
Solution: A
Solution: B, C, D, E, F, G
a) Suppose the tree drawn above is the implicit tree of a binary max-
heap H. State the array representation of H, first before and then
after performing the operation H.delete max().
Solution:
Common Mistakes:
Thus we can use radix sort to sort the students by their magical
lineage in worst-case O(n) time, which is again optimal.
Common Mistakes:
• Claiming ancestors instead of
• Saying that and using counting sort
Problem 6. Triple Sum
Common Mistakes:
• Using counting/radix sort or creating a direct access array (no
bound on u so not efficient)
• Saying n insertions into a hash table gives an amortized bound
• Checking all triples in Ω(n3) time
Problem 7. Where Am i?
Solution: Our algorithm will be to walk up the tree from v to the root
r of the Sequence AVL Tree, counting the nodes preceding v in the
traversal order along the way, since the number of nodes preceding v
in the tree is equivalent to v’s (zero-indexed) index.
• Case 2, u is the right child of u.parent: then all nodes in the left
subtree of u.parent precede v (as does u), so set #v(u.parent) = 1 +
u.parent.left.size + #v(u).
Then return #v(r), since this is the number of nodes preceding v in
r’s subtree (i.e., the entire tree). Correctness is argued within the
algorithm description. This algorithm spends worst-case O(1) work
for each ancestor of v, so since the Sequence AVL Tree is balanced,
the number of ancestors is bounded by O(log n), and the algorithm
runs in worst-case O(log n) time.
Common Mistakes:
You can use this paper to write a longer solution if you run out of
space, but be sure to write “Continued on S1” on the problem
statement’s page.
Solution: (Problem 6 continued...)
Worst-case Solution
You can use this paper to write a longer solution if you run out
of space, but be sure to write “Continued on S2” on the problem
statement’s page.
You can use this paper to write a longer solution if you run out of
space, but be sure to write “Continued on S3” on the problem
statement’s page.
Common Mistakes:
• Taking a minimum in some pipe rather than a minimum over all
pipes
• Not prioritizing pipes having more than one hole over those
with only one
• Storing an AVL or heap on all pipes which cannot be maintained
within the time bounds
• Attempting an augmentation-based solution incorrectly
• Claiming O(log n)-time insertion into a sorted array
• Initializing a direct access array of non-polynomially bounded size
• Using Sequence AVL Trees instead of Set AVL Trees
Common
Mistakes:
• Assuming the max range always goes through the root
• Claiming a substring augmentation without showing how to
maintain it in O(1) time
• Using 2 AVL Trees, one of active and one of inactive (doesn’t help
to find largest range).
• Substring augmentation maintenance doesn’t consider substrings
containing the root