Algorithm Homework Help
Algorithm Homework Help
Suppose that we have a binary tree augmented with subtree sizes. Define
the imbalance of a node x to be the difference between the number of
descendants of the left child of x and the number of descendants of the
right child of x. More formally, we define:
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
Solution: We will first turn the elements in the tree rooted at x into a
sorted array in Θ(ℓ) time, and then convert the sorted array into a new BST
with imbalances of 0 or 1 on all its nodes, again taking Θ(ℓ) time.
(b) Suppose that you pick some constant c and modify your code for
INSERT and DELETE so that you rebalance a node x as soon as imbalance(x)
> c. Show that if you start with a tree of size n such that every node has
imbalance 0, there exists a sequence of INSERT and DELETE operations
such that the total runtime is not O((# of ops)· lg n). Explain why this
implies that this rebalancing scheme is bad.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
(Even if other rebalances occurred beforehand, none of them would affect
the root — the rebalance operation only affects descendants, and the root is
only a descendant of itself.) Rebalancing the root takes Θ(n) time. Hence we
have c + 1 operations which take time Ω(n). This means that the amortized
time per operation is Ω(n/(c + 1)). Because c is constant, this means that the
amortized time for each operation is Ω(n), which is not O(lg n)
(c) Show that any binary search tree in which every node has imbalance
ratio at most 1/2 has depth O(lg n).
Solution: We want to know the relationship between the size of some node
x and the size of its children. WLOG, assume that the left child has more
descendants. Because of the definition of subtree sizes, we have x.size = 1 +
x.left.size + x.right.size. Hence, we can do the following math:
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
So the larger child cannot have more than 3/4 of the descendants of the
parent. This d means that at depth d, the number of descendants is ≤
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
Suppose that you modify your code for INSERT and DELETE so that you
rebalance a node x as soon as imb-ratio(x) > 1/2. In the next two parts of this
problem, you will show that under this rebalancing scheme the amortized
runtime of INSERT and DELETE is O(lg n).
(d) Suppose that you have a BST of size n such that all nodes have imbalance
at most 1. Use the accounting method to find the amortized runtime of a
sequence of k INSERT and DELETE operations on this BST.
Hint: A rebalance of x occurs when imb-ratio(x) > 1/2. We can also write this
condition as imbalance(x) > x.size/2. How many descendants of x must be
inserted or deleted in order to cause the imbalance to become that high?
And what is the cost of rebalancing x?
(e) Suppose that you have a BST of size n such that all nodes have
imbalance at most 1. Use the potential method to find the amortized
runtime of a sequence of k INSERT and DELETE operations on this BST.
Hint: Remember that when using the potential method, the final potential
should always be at least as large as the initial potential, no matter what
the sequence of operations is. Keeping in mind that an imbalance of 1 is,
in some sense, as balanced as possible, is there a way to define your
potential function so that the initial potential is 0, but the potential
function is always non-negative?
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
We then define the potential for the entire BST D to be Φ(D) = P x φ(x).
First, consider how insertion or deletion affects the potential of the data
structure, without considering rebalancing. When we insert or delete a
node, it can possibly increase the imbalance of all of its ancestors in the
tree. In the worst case, it will increase the imbalance of all of its ancestors.
Because we start with n nodes and the number of operations k is
polynomial in n, the number of ancestors of any node is O(lg n). So the
total potential increase caused by insertion or deletion is at most O(lg n).
This means that the amortized cost of insertion or deletion is still O(lg n),
barring rebalancing.
Note that unlike regular AVL or red-black trees, this method of rebalancing
BSTs does not require any rotations. As a result, this type of BST (which is a
variant on what is known as a “weight-balanced tree”) can be easier to
implement in certain contexts. However, AVL trees and red-black trees
achieve worst-case O(lg n) time, while weight-balanced BSTs only achieve
amortized O(lg n) time. So there is something of a trade-off between
runtime and simplicity
You would like to sell your house, but there are very few buyers due to
the housing market crash. Assume that at any given time there is at most
one buyer interested in your house. You know with certainty that the
range of offer from any buyer will be from 1 to M dollars. After getting an
offer from each buyer, you make a decision to either take it or leave it. If
you leave it, the buyer will buy some other house, so you will lose the
offer forever. Note that the list of buyers is finite, and you do NOT know
how many there will be. If you reject all of the offers, you will end up
selling your house to the bank for 1 dollar. You want to maximize the
return from selling your house. To this end, your knowledge of
competitive analysis from 6.046 comes in very handy. Note that a
clairvoyant algorithm will always pick the best offer.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
(a) Give a deterministic algorithm that is 1 √ -competitive. In other words,
for any input M sequence where x is the best offer, your algorithm
should always choose an offer of at least x√ dollars.
• Case 1: The b √ est offer has value at least √ M. Then there exists at least
one bid with value ≥ M, so our algorithm will accept it. How will that bid
compare to the best offer? Well, in th √ is case, the best offer could be as high
as M. The offer we accept will be at least M, so:
• Case 2: The best offer has value strictly less than √ M. Then we’ll never
accept any offers, and we’ll end up selling to the bank for $1. Hence, we get
the following equation:
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
(b) Devise a randomized algorithm that is expected 1 -competitive. In
other words, for 2 lg M any input sequence where x is the best offer, the
expected value of the offer selected by your algorithm should be at least x
dollars. 2 lg M
Solution: Let ℓ = ⌊lg M⌋. At the start of the algorithm, we pick a value i
uniformly at random from {1, . . . , ℓ}. We then accept any bid that is
greater than M 2 i . We wish to discover how this algorithm does when
compared with the clairvoyant algorithm. Suppose that the algorithm
receives as input a sequence of bids such that the best bid is x. Let k be
the integer satisfying the equation
Because x lies in the range [1, M], k must belong to the set {1, . . . , ℓ+1}.
We consider two cases:
What does this mean for the expected value of our bid? We get the following:
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers