Static Dictionaries - PowerPoint PPT Presentation

About This Presentation
Title:

Static Dictionaries

Description:

Each item/key/element has an estimated access frequency (or probability) ... Sum of ps and qs = 1. Cost of tree = S0 = i = n qi (level(fi) 1) ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 22
Provided by: cise8
Category:

less

Transcript and Presenter's Notes

Title: Static Dictionaries


1
Static Dictionaries
  • Collection of items.
  • Each item is a pair.
  • (key, element)
  • Pairs have different keys.
  • Operations are
  • initialize/create
  • get (search)

2
Hashing
  • Perfect hashing (no collisions).
  • Minimal perfect hashing (space n).
  • CHD (compress, hash, and displace) algorithm.
  • O(n) time to construct the perfect or minimal
    perfect hash function.
  • O(1) search time.
  • Bothelo, Belazzougui Dietzfelbinger. Compress,
    hash, and displace.  17th European Symposium on
    Algorithms, 2009.

3
Search Tree
  • Hashing not efficient for extended operations
    such as range search and nearest match.
  • Will examine a binary search tree structure for
    static dictionaries.
  • Each item/key/element has an estimated access
    frequency (or probability).

4
Example
Key Probability
a 0.8
b 0.1
c 0.1
a lt b lt c
Cost 0.8 1 0.1 2 0.1 3 1.3
Cost 0.8 2 0.1 1 0.1 2 1.9
5
Search Types
  • Successful.
  • Search for a key that is in the dictionary.
  • Terminates at an internal node.
  • Unsuccessful.
  • Search for a key that is not in the dictionary.
  • Terminates at an external/failure node.

6
Internal And External Nodes
  • A binary tree with n internal nodes has n 1
    external nodes.
  • Let s1, s2, , sn be the internal nodes, in
    inorder.
  • key(s1) lt key(s2) lt lt key(sn).
  • Let key(s0) infinity and key(sn1) infinity.
  • Let f0, f1, , fn be the external nodes, in
    inorder.
  • fi is reached iff key(si) lt search key lt
    key(si1).

7
Cost Of Binary Search Tree
  • Let pi probability for key(si).
  • Let qi probability for key(si) lt search key lt
    key(si1).
  • Sum of ps and qs 1.
  • Cost of tree S0 lt i lt n qi (level(fi) 1)
  • S1lt i lt n pi
    level(si)
  • Cost weighted path length.

8
Brute Force Algorithm
  • Generate all binary search trees with n internal
    nodes.
  • Compute the weighted path length of each.
  • Determine tree with minimum weighted path length.
  • Number of trees to examine is O(4n/n1.5).
  • Brute force approach is impractical for large n.

9
Dynamic Programming
  • Keys are a1 lt a2 lt lt an.
  • Let Ti j least cost tree for ai1, ai2, , aj.
  • T0n least cost tree for a1, a2, , an.

T2,7
  • Ti j includes pi1, pi2, , pj and qi, qi1, ,
    qj.

10
Terminology
  • Ti j least cost tree for ai1, ai2, , aj.
  • ci j cost of Ti j
  • Si lt u lt j qu (level(fu) 1)
  • Si lt u lt j pu level(su).
  • ri j root of Ti j.
  • wi j weight of Ti j
  • sum of ps and qs in Ti j
  • pi1 pi2 pj qi qi1 qj

T2,7
11
i j
  • Ti j includes pi1, pi2, , pj and qi, qi1, ,
    qj.
  • Ti i includes qi only.
  • ci i cost of Ti i 0.
  • ri i root of Ti i 0.
  • wi i weight of Ti i
  • sum of ps and qs in Ti i
  • qi

12
i lt j
  • Ti j least cost tree for ai1, ai2, , aj.
  • Ti j includes pi1, pi2, , pj and qi, qi1, ,
    qj.
  • Let ak, i lt k lt j, be in the root of Ti j.
  • L includes pi1, pi2, , pk-1 and qi, qi1, ,
    qk-1.
  • R includes pk1, pk2, , pj and qk, qk1, , qj.

13
cost(L)
  • L includes pi1, pi2, , pk-1 and qi, qi1, ,
    qk-1.
  • cost(L) weighted path length of L when viewed
    as a stand alone binary search tree.

14
Contribution To cij
  • ci j Si lt u lt j qu (level(fu) 1)
  • Si lt u lt j pu level(su).
  • When L is viewed as a subtree of Ti j , the level
    of each node is 1 more than when L is viewed as a
    stand alone tree.
  • So, contribution of L to cij is cost(L) wi k-1.

15
cij
  • Contribution of L to cij is cost(L) wi k-1.
  • Contribution of R to cij is cost(R) wkj.
  • cij cost(L) wi k-1 cost(R) wkj pk
  • cost(L) cost(R) wij

16
cij
  • cij cost(L) cost(R) wij
  • cost(L) cik-1
  • cost(R) ckj
  • cij cik-1 ckj wij
  • Dont know k.
  • cij mini lt k lt jcik-1 ckj wij

17
cij
  • cij mini lt k lt jcik-1 ckj wij
  • rij k that minimizes right side.

18
Computation Of c0n And r0n
  • Start with ci i 0, ri i 0, wi i qi, 0 lt i
    lt n (zero-key trees).
  • Use cij mini lt k lt jcik-1 ckj wij to
    compute cii1, ri i1, 0 lt i lt n 1 (one-key
    trees).
  • Now use the equation to compute cii2, ri i2, 0
    lt i lt n 2 (two-key trees).
  • Now use the equation to compute cii3, ri i3, 0
    lt i lt n 3 (three-key trees).
  • Continue until c0n and r0n(n-key tree) have been
    computed.

19
Computation Of c0n And r0n
20
Complexity
  • cij mini lt k lt jcik-1 ckj wij
  • O(n) time to compute one cij.
  • O(n2) cijs to compute.
  • Total time is O(n3).
  • May be reduced to O(n2) by using
  • cij min ri,j-1 lt k lt ri1,j
    cik-1 ckj wij

21
Construct T0n
  • Root is r0n.
  • Suppose that r0n 10.
  • Construct T09 and T10,n recursively.
  • Time is O(n).
Write a Comment
User Comments (0)
About PowerShow.com