Online Algorithms Homework Help
Online Algorithms Homework Help
Solution:
1 T.insert(2)
2 T.delete(49)
3 T.delete(35)
4 T.insert(85)
5 T.delete(84)
Solution:
insert(2)
delete(49)
Programminghomeworkhelp.com
delete(35)
insert(85)
Programminghomeworkhelp.com
delete(84) (Two solutions, swap down predecessor/successor)
(c) For each unbalanced node identified in part (a), draw the two
trees that result from rotating the node in the original tree left and
right (when possible). For each tree drawn, specify whether it is
height-balanced, i.e., all nodes satisfy the AVL property.
Programminghomeworkhelp.com
Solution:
Programminghomeworkhelp.com
Node containing 37 is not height-balanced.
For each array below, draw it as a complete1 binary tree and state
whether the tree is a max-heap, a min-heap, or neither. If the tree is
neither, turn the tree into a min-heap by repeatedly swapping items
that are adjacent in the tree. Communicate your swaps by drawing a
sequence of trees, marking on each tree the pair that was swapped.
Programminghomeworkhelp.com
(a) [4, 12, 8, 21, 14, 9, 17]
Solution: Min-heap 4
Solution: Max-heap
Programminghomeworkhelp.com
(c) [2, 9, 13, 8, 0, 2]
(d) [1, 3, 6, 5, 4, 9, 7]
Solution: Min-heap
Programminghomeworkhelp.com
Problem 3.
Solution:
Programminghomeworkhelp.com
(b) Wonder-Grow decides to be more objective and award a
trophy to every garden receiving a score strictly greater than a
reference score x. Given a max-heap A of garden pairs, describe an
O(nx)-time algorithm to return the registration numbers of all
gardens with score larger than x, where nx is the number of
gardens returned.
Solution:
Programminghomeworkhelp.com
This procedure visits at most 3nx nodes (the nodes containing the nx
items reported and possibly each such node’s two children), so this
procedure runs in O(nx) time.
Problem-4.
Programminghomeworkhelp.com
initialize(S) Initialize database with a list S = ((s0, c0), . . . ,
(sn1, cn1)) corresponding to n solar farms in
O(n) time.
Solution:
• a Priority Queue P on the solar farms, storing for each solar farm
its address si, capacity ci, and its available capacity ai (initially ai =
ci), keyed on available capacity;
• a Set data structure B mapping each powered building’s name bj
to the address of the solar farm si that it is connected to and its
demand dj ; and
Programminghomeworkhelp.com
• a Set data structure F mapping the address of each solar farm si
to: (1) its own Set data structure Bi containing the buildings
associated with that farm, and (2) a pointer to the location of si in
P.
– dj > ci, so reinsert the solar farm back into P (relinking a pointer
from F to a location in P) and return that no solar farm can
currently support the building.
– dj ≤ ci, so subtract dj from ci and reinsert it back into P (relinking
a pointer). Then, add bj to B mapping to si, and then find the Bi in F
associated with si and add bj to Bi.
Programminghomeworkhelp.com
This operation directly maintains the invariants of our database
and takes time asymptotically upperbounded by the sum of: one
delete max and one insert operation on P, an insert operation on
B, a find on F, an insertion into Bi, and constant additional work
(to maintain pointers and perform arithmetic).
Programminghomeworkhelp.com
with O(log n) lookups, so we must use hash tables for these,
leading to expected bounds on all operations, and amortized
bounds on power on and power off. For each Bi, we need O(log n)
lookup, insert, and delete, so can implement with either a Set AVL
Tree or a hash table. Lastly, P requires O(n) build, and O(log n)
insert and delete max, so either a Max-Heap or a Sequence AVL
Tree augmented with subtree max items can be used. Note that
removing an item from a Sequence AVL Tree via a pointer to its
node is exactly the same as deleting the item after being found by
index. Removing an item from a Max-Heap by index is not natively
supported, but uses the same technique as removing the root:
swap the item with the last leaf (the last item in the array), remove
the item, and then swap the moved item up or down the tree to
restore the Max-Heap property.
Dr. Squid has built a robotic arm from n+1 rigid bars called links,
each connected to the one before it with a rotating joint (n joints in
total). Following standard practice in robotics3, the orientation of
each link is specified locally relative to the orientation of the
previous link. In mathematical notation, the change in orientation
at a joint can be specified using a 4 × 4 transformation matrix. Let
M = (M0, . . . , Mn1) be an array of transformation matrices
associated with the arm, where matrix Mk is the change in
orientation at joint k, between links k and k + 1.
Programminghomeworkhelp.com
To compute the position of the end effector4, Dr. Squid will need
the arm’s Q full transformation: n-1 the ordered matrix product of
the arm’s transformation matrices, k=0 Mk = M0 ·M1 · . . . ·Mn-1.
Assume Dr. Squid has a function matrix multiply(M1,M2) that
returns the matrix product5 M1 × M2 of any two 4 × 4
transformation matrices in O(1) time. While tinkering with the arm
changing one joint at a time, Dr. Squid will need to re-evaluate this
matrix product quickly. Describe a database to support the
following worst-case operations to accelerate Dr. Squid’s workflow:
initialize(M) Initialize from an initial input configuration M in
O(n) time.
update joint(k,M) Replace joint k’s matrix Mk with matrix M in
O(log n) time.
full Return the arm’s current full transformation in
transformation() O(1) time.
Solution:
Programminghomeworkhelp.com
and PR be v.left.P and v.right.P respectively (or the 4 × 4 identity
matrix if the respective left or right child is missing); then v.P = PL ·
v.M · PR which can be computed in O(1) time using the provided
matrix multiply function, so this augmentation can be maintained.
(Note that, because the number of items and traversal order
never changes, AVL behavior is not needed here, since no
operation will change the structure of the tree. So a static binary
tree or even an implicitly represented complete binary tree stored
in an array, as in a binary heap, would suffice.)
Programminghomeworkhelp.com
Liza Pover has found a Monominos pizza left over from some big-
TEX recruiting event. The pizza is a disc6 with radius z, having n
toppings labeled 0, . . . , n � 1. Assume z fits in a single machine
word, so integer arithmetic on O(1) such integers can be done in
O(1) time. Each topping i:
Liza wants to pick a point (x0 , y0 ) and make a pair of cuts from
that point, one going straight down and one going straight left, and
take the resulting slice, i.e., the intersection of the pizza with the
two half-planes x ≤ x0 and y ≤ y0 . The tastiness of this slice is the
sum of all ti such that xi ≤ x0 and yi ≤ y0 . Liza wants to find a
tastiest slice, that is, a slice of maximum tastiness. Assume there
exists a slice with positive tastiness.
Programminghomeworkhelp.com
Solution:
(b) To make finding a tastiest slice easier, show how to modify a Set
AVL Tree so that:
• it stores key–value items, where each item x contains a value x.val
(in addition to its key x.key on which the Set AVL is ordered);
• it supports a new tree-level operation max prefix() which returns in
worstcase O(1) time a pair (k∗, prefix(k∗)), where k∗ P is any key
stored in the tree T that maximizes the prefix sum, prefix(k) = {x.val |
x ∈ T and x.key ≤ k} (that is, the sum of all values of items whose
keys are ≤ k); and
• all other Set AVL Tree operations maintain their running times.
Solution:
We augment the Set AVL Tree so that each node v stores three
additional subtree properties: • v.sum: the sum of all item values
stored in v’s subtree, which can be
Programminghomeworkhelp.com
computed in O(1) time by: v.sum = v.left.sum + v.item.val +
v.right.sum, or with zeros for the left and right sums if the
respective children do not exist. • v.max prefix: max{prefix(k) | k ∈
subtree(v)}. We can compute the max prefix in the subtree in O(1)
time by comparing three values:
v.max_prefix = max(
v.left.max_prefix,
# left
v.left.sum + v.item.val,
# middle
v.left.sum + v.item.val + v.right.max_prefix)
# right
Programminghomeworkhelp.com
maintained without effecting the running times of the normal Set
AVL Tree operations.
(c) Using the data structure from part (b) as a black box,
describe a worst-case O(n log n)-time algorithm to return a
triple (x, y,t), where point (x, y) corresponds to a slice of
maximum tastiness t.
Solution:
Programminghomeworkhelp.com
coordinate at some xi for i ∈ {0, . . . , n � 1}, as argued in part (a),
then taking the maximum of all slices found in O(n) time will
correctly return a tastiest slice possible.
Solution:
1 class Part_B_Node(BST_Node):
2 def subtree_update(A):
3 super().subtree_update()
4 A.sum = A.item.val # sum
5 if A.left: A.sum += A.left.sum
6 if A.right: A.sum += A.right.sum
7 left = -float(’inf’) # max prefix
8 right = -float(’inf’)
9 middle = A.item.val
10 if A.left:
Programminghomeworkhelp.com
11 left = A.left.max_prefix
12 middle += A.left.sum
13 if A.right:
14 right = middle + A.right.max_prefix
15 A.max_prefix = max(left, middle, right)
16 if left == A.max_prefix: # max prefix key
17 A.max_prefix_key = A.left.max_prefix_key
18 elif middle == A.max_prefix:
19 A.max_prefix_key = A.item.key
20 else:
21 A.max_prefix_key = A.right.max_prefix_key
22
23 class Part_B_Tree(Set_AVL_Tree):
24 def __init__(self):
25 super().__init__(Part_B_Node)
26
27 def max_prefix(self):
28 k = self.root.max_prefix_key
29 s = self.root.max_prefix
30 return (k, s)
31
Programminghomeworkhelp.com
32 def tastiest_slice(toppings):
33 B = Part_B_Tree() # use data structure from part (b)
34 X, Y, T = 0, 0, 0
35 n = len(toppings)
36 toppings.sort(key = lambda topping: topping[0])
37 for (x, y, t) in toppings:
38 B.insert(Key_Val_Item(y, t))
39 (Y_, T_) = B.max_prefix()
40 if T < T_:
41 X, Y, T = x, Y_, T_
42 return (X, Y, T)
Programminghomeworkhelp.com