Finish Red Black Trees, Dynamic Programming




CS146

Chris Pollett

Apr 21, 2014

Outline

Introduction

RB-tree Transplant

RB-DELETE

RB-DELETE-FIXUP(T,x)

How Fixup code works

Different cases of Red Black Tree Delete

Quiz (Sec 5)

Which of the following statements is true?

  1. The black height of a red-black tree might be larger than the height of the tree.
  2. The SEARCH procedure for a red-black tree is the same as for a binary search tree.
  3. At the start of the loop of our red-black insert fix-up procedure, property 3 of red black trees might be violated.

Quiz (Sec 6)

Which of the following statements is true?

  1. We had to make non-trivial modification to the BST MINIMUM procedure to get it to work for red-black trees.
  2. The black height of a red-black tree is at least half of the height of a red-black tree.
  3. At the start of the loop of our red-black insert fix-up procedure, property 5 of red black trees might be violated.

Dynamic Programming

Developing a Dynamic Programming Algorithm

In developing a dynamic-programming algorithm, we typically follow a sequence of four steps:

  1. Characterize the structure of an optimal solution.
  2. Recursively define the value of an optimal solution.
  3. Compute the value of an optimal solution, typically in a bottom-up fashion.
  4. Construct an optimal solution from the computed information.

Rod Cutting

Example Rod Cutting

Rod Cutting Example

Rod Cutting Discussion

  • In general we can cut up a rod of length `n` in `2^(n-1)` ways as there are `n-1` places we could cut or not.
  • We denote a decomposition into pieces using ordinary additive notation. So 7 = 2 + 2 +3 indicates that a rod of length 7 was cut into three pieces, two of length 2 and one of length 3.
  • If an optimal solution cuts the rod into `k` pieces, the an optimal decomposition
    `n = i_1 + i_2 + cdots + i_k`
    of the rod into pieces of lengths `i_j` provides maximum corresponding revenue
    `r_n = p_(i_1) + cdots + p_(i_k)`.
  • We can frame the values `r_n` in terms of optimal values from shorter rods:
    `r_n = max(p_n, r_1 + r_(n-1), r_2+r_(n-2), ... r_(n-1) +r_1).`
  • Notice that to solve the original problem of size `n`, we solve smaller problems of the same type, but of smaller sizes.
  • We say that the rod-cutting problem exhibits optimal substructure: optimal solutions to a problem incorporate optimal solution to related subproblems, which can be solved independently.
  • Any solution will have a smallest cut `i`. For that cut `i`, the revenue will be `p_i + r_(n-i)`. So we can simplify our equation above to: `r_n = max_(1 le i le n)(p_i + r_(n - i))`
  • Top-down Rod Cutting