1 / 47

Minimize average access time

. p i. Minimize average access time. Items have weights: Item i has weight w i Let W =  w i be the total weight of the items Want the search to heavy items to be faster If p i = w i /W represents the access frequency to item i then the average access time is. d i.

halpern
Download Presentation

Minimize average access time

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. pi Minimize average access time • Items have weights: Item i has weight wi • Let W = wi be the total weight of the items • Want the search to heavy items to be faster • If pi = wi/W represents the access frequency to item i then the average access time is di where di is the depth of item i

  2. There is a lower bound  pi di   pi log b (1/ pi ) for every tree with maximum degree b So we will be looking for trees for which di = O(log (W/wi)) In particular if all weights are equal the regular search trees which we have studied, will do the job.

  3. Static setup: we know the access freq. • You can find the best tree in O(nlog(n)) time (homework)

  4. 0.1 0.26 0.2 0.1 Approximation (Mehlhorn) .04 0.2 0.1 0.26 0.1 0.2 0.1

  5. Approximation (Mehlhorn) .04 0.2 0.1 0.26 0.1 0.2 0.1 0.1 0.26 0.2 0.1

  6. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  7. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  8. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  9. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  10. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  11. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  12. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  13. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  14. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  15. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  16. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  17. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  18. .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  19. Analysis .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1 An internal node at level i corresponds to an interval of length 1/2i The sum of the weights of the pieces that correspond to an internal node is no larger than the length of the corresponding interval

  20. Analysis .04 0.2 0.1 0.26 0.1 0.2 0.1 0.26 0.2 0.1

  21. Biased 2-b trees(Bent, Sleator, Tarjan 1980)

  22. Biased 2-b trees definition Internal nodes have degree between 2 and b. We also need an additional property: Define the rank of a node x in a 2-b tree recursively as follows. If x is a leaf containing item i then r(x) = log2wi If x is an internal node r(x) = 1 + max {r(y) | y is a child of x }

  23. 10 9 9 4 8 4 8 6 3 3 3 5 5 1 -1 0 Biased 2-3 tree (example) 25 500 350 10 12 8 40 50 .5 1

  24. Biased 2-b trees definition (cont) Call x major if r(x) = r(p(x)) - 1 Otherwise x is minor Here is the additional property: Local bias:Any neighboring sibling of a minor node is a major leaf. In case all weights are the same this implies that all leaves should be at the same level and we get regular 2-b trees.

  25. 25 500 350 10 12 8 40 50 .5 1 Biased 2-3 trees example revisited 10 9 9 4 8 4 8 6 3 3 3 5 5 1 -1 0

  26. Are the access times ok ? Define the size of a node x in a 2-b tree recursively as follows. If x is a leaf containing item i s(x) = wi If x is an internal node s(x) =  y is a child of xs(y) Lemma: For any node x, 2r(x)-1  s(x), For a leaf x, 2r(x)  s(x) < 2r(x) +1 ==> if x is a leaf of depth d then d < log(W/ wi) + 2 proof. D  r(root) - r(x) < log (s(r)) + 1 - (log(s(x)) - 1)

  27. Are the access times ok ? (cont.) Lemma: For any node x, 2r(x)-1  s(x), For a leaf x, 2r(x)  s(x) < 2r(x) +1 proof. By induction on r(x). If x is a leaf the definition r(x) = log2s(x) implies that 2r(x)  s(x) < 2r(x) +1 If x is an internal node with a minor child then x has a major child which is a leaf, say y. So 2r(x)-1 = 2r(y)  s(y) < s(x) If x is an internal node with no minor child then it has at least two major children y and z: 2r(x)-1 = 2r(y)-1 +2s(z)-1  s(y) + s(z)  s(x)

  28. 10 9 8 7 6 = 4 3 2 1 0 -1 -1 Concatenation (example) 8 10 7 4 9 + 3 6 2 1 -1 -1

  29. Catenation (definition) Traverse the right path of the tree rooted at r and the left path of the tree rooted at r’ concurrently. Go down one step from the node of higher rank. Stop either when they are both equal or the node of higher rank is a leaf. r’ r p(x) p(y) x y w.l.o.g. let rank(x) ≥ rank(y). If rank(x) > rank(y) then x is a leaf Note that rank(p(y)) ≥ rank(x) (otherwise we should not have traversed y, but continue from x or stop)

  30. Catenation (definition) p(x) p(y) x y Let v be the node among p(x) and p(y) of minimum rank Assume v=p(x), the other case is symmetric

  31. Catenation (definition) Case 1: If the rank of v is larger by at least 2 than the rank of x stick x and y as children of a new node g. Stick g underneath v Merge the paths by rank. v=p(x) v=p(x) p(y) p(y) g x y x y

  32. Catenation (definition) Case 2: If the rank of v is larger by 1 than the rank of x Add y as a child of v Merge the paths by rank. v=p(x) p(y) v=p(x) p(y) y y x x

  33. 10 9 8 7 6 = 4 3 2 1 0 -1 -1 Concatenation (example) 8 10 7 4 9 + 3 6 2 1 -1 -1

  34. Catenation (definition) Note that in both cases local biased is preserved ! v=p(x) p(y) v=p(x) p(y) y y x x

  35. Catenation (the symmetric case) p(x) p(y) x y Let v be the node among p(x) and p(y) of minimum rank If v=p(y) then p(y) p(x) Note that if y is minor then x is a major leaf y x

  36. Catenation (splitting the high degree node) It could be that we have to split a high degree node. We split as long as we have a high degree node, when a minor node splits we add a new parent to the two pieces and stop. Why does a node split into two nodes of the same rank ? 1 Can’t have two minor consecutive siblings

  37. Catenation (proof of correctness) Follows from the following observations: Obs1: Before splitting every minor node stands where a minor node used to stand before in one of the trees. Obs2: Splitting preserves local bias.

  38. Catenation (worst case analysis) Worst case bound: O(max{r(x),r(y)} - max{r(u),r(v)}) = O(log(W/(w- + w+)) x and y are the two roots u is the rightmost leaf descendant of x and v is the leftmost leaf descendant of y w- = s(u), w+ = s(v), W is the total weight of both trees. In particular if y is leaf and x is the root of a big tree of weight W then this bound is O(W/s(y))

  39. Catenation (amortized analysis) amortized bound: O(|r(x) - r(y) |) Proof: We want the potential to decrease by one for every node of rank smaller than r(y) that we traverse. Potential (def): every (minor) node x has r(p(x)) - r(x) - 1 credits.  = total number of credits.

  40. Catenation (amortized analysis) a b a a b b a c b + c = d c c d e d f e e d f e

  41. Catenation (amortized analysis) f had r(e) - r(f) - 1 credits. g needs r(d) - r(g) - 1 which is smaller by at least 2, in general it would be smaller by at least 1 + the number of blue guys a e b d a g b f c c d d had r(c) - r(d) - 1 d needs r(e) - r(d) - 1 # of released credits is at least the number of pink guys e c d c g d f e e d

  42. 10 9 8 7 6 5 4 1 4 10 8 3 -1 7 2 9 4 -1 + 6 3 5 2 1 4 -1 -1 3-way concatenation (example) 8 10 7 4 9 + + 4 3 6 2 1 -1 -1 =

  43. 3-way concatenation Do two succesive 2-way catenations. Analysis: Amortized: O(max{r(x), r(y), r(z)} - min{r(x), r(y), r(z)}) worst-case: O(max{r(x), r(y), r(z)} - r(y))

  44. 2-way split Similar to what we did for regular search trees. Suppose we split at a leaf y which is in the tree. We go up from y towards the root x and accumulate a left tree and a right tree by succesive 2-way catenations Analysis: To split a tree with root x at a leaf y. amortized: O(r(x) - r(y)) = O(log(W/s(y))

  45. 3-way split Splitting at an item i which is not in the tree. Let i- be the largest item in the tree which is smaller than i Let i+ be the smallest item in the tree which is bigger than i Let y be the lowest common ancestor of i- and i+ The initial left tree is formed from the children of y containing item less than i. The initial right tree is formed from the children of y containing items bigger than i. Analysis: To split a tree with root x at an item i not in the tree amortized: O(r(x) - r(y)) = O(log(W/(s(i-) + s(i+)))

  46. Other operations Define delete, insert, and weight change in a straightforward way in terms of catenate and split.

  47. Extensions There are many variants. Binary variants. Variants that has good bounds for all operations on the worst case

More Related