Download presentation
Presentation is loading. Please wait.
Published byMavis Agnes Curtis Modified over 9 years ago
1
A sparse Table implementation of Priority Queue Presented by: Yaniv Nahum Written by:Alon Itai Alan G. Konheim Michael Rodeh
2
PQ - introduction A data structure with the following operations: A data structure with the following operations: –Search (x) –Insert (x) –Delete (x) –Min –Next (x) –Scan
3
PQ - introduction Most implementations use trees (2-3, AVL, weight balanced trees) and requires no more than O (log n) time. Most implementations use trees (2-3, AVL, weight balanced trees) and requires no more than O (log n) time. In many cases, the algorithms devote much of their running time and storage manipulating the PQ, often rendering a theoretically efficient algorithm to infeasible or inefficient to all practical purposes. In many cases, the algorithms devote much of their running time and storage manipulating the PQ, often rendering a theoretically efficient algorithm to infeasible or inefficient to all practical purposes.
4
PQ as Sparse table Data is stored in linear array, and requires only one pointer. Data is stored in linear array, and requires only one pointer. Insertion requires: Insertion requires: –Searching. –Moving (shifting of records). –Reconfiguring (increase size and distribute keys).
5
Performance Assuming a table of size m. Assuming a table of size m. Searching takes O (log m). Searching takes O (log m). Move takes O (m) in worst case. Move takes O (m) in worst case. We will show that the expected number of moves is constant. We will show that the expected number of moves is constant. We will present a more complicated table, in order to improve worst case, which takes O (n*log^2(n)) to build. We will present a more complicated table, in order to improve worst case, which takes O (n*log^2(n)) to build.
6
Sparse table schemata Records are stored in a sorted order. Records are stored in a sorted order. Insertion is improved by introducing gaps in the table, thereby storing n records in a table of capacity m for some m > n. Insertion is improved by introducing gaps in the table, thereby storing n records in a table of capacity m for some m > n. m-n keys are dummy keys. m-n keys are dummy keys.
7
example Genuine keys are located at addresses 2,5,6,8 in the following table: Genuine keys are located at addresses 2,5,6,8 in the following table: y = (2,2,2,3,3,3,4,5,5). y = (2,2,2,3,3,3,4,5,5). n = 4. n = 4. m = 9. m = 9.
8
Building a table Determined by 2 sequences of real numbers: Determined by 2 sequences of real numbers: –{n k : 0<=k<INFINITY} –{m k : 1<=k<INFINITY} –1=n 0 < n 1 < … < … –n k <= n k-1 *m k 1<=k
9
Building a table The size of the table, m, can only take the values n k-1 *m k for k=1,2,… The size of the table, m, can only take the values n k-1 *m k for k=1,2,… The size of the table is m= n k-1 *m k when the number of distinct keys in the table, n, satisfies n k-1 <n<=n k The size of the table is m= n k-1 *m k when the number of distinct keys in the table, n, satisfies n k-1 <n<=n k
10
Insertion Inserting x into a table of size m= n k-1 *m k containing n k-1 <n<=n k genuine keys find address s satisfying y s-1 <x<=y s Inserting x into a table of size m= n k-1 *m k containing n k-1 <n<=n k genuine keys find address s satisfying y s-1 <x<=y s If y s is a dummy key, it is replaced by x, yielding the table: (y 0,…,y s-1,x,y s+1,…y m-1 ) If y s is a dummy key, it is replaced by x, yielding the table: (y 0,…,y s-1,x,y s+1,…y m-1 ) If y s is a genuine key, the block of t consecutive genuine keys is shift right one position. If y s is a genuine key, the block of t consecutive genuine keys is shift right one position.
11
Insertion (Cont …) X is inserted at address s yielding the table: X is inserted at address s yielding the table: (y 0,…,y s-1,x,y s,…,y s+t-1,y s+t+1,…,y m-1 ) (y 0,…,y s-1,x,y s,…,y s+t-1,y s+t+1,…,y m-1 ) Note: y s+t is the dummy key removed from table. Note: y s+t is the dummy key removed from table.
12
reconfiguration When inserting x to a table with size m= n k-1 * m k, and there are n k genuine keys. When inserting x to a table with size m= n k-1 * m k, and there are n k genuine keys. The size of the table first increases to n k *m k+1, and the n k genuine keys are uniformly distribute in it. The size of the table first increases to n k *m k+1, and the n k genuine keys are uniformly distribute in it. Than, x is inserted regularly. Than, x is inserted regularly.
13
Some Insight … The numbers {m k } are the expansion factors. The numbers {m k } are the expansion factors. The ratio r = n / n k-1 * m k with n k-1 <n<=n k is the density of the genuine keys in the table. The ratio r = n / n k-1 * m k with n k-1 <n<=n k is the density of the genuine keys in the table. 1 / m k < r <= 1 1 / m k < r <= 1 m k adds log(m k ) steps to the binary search but reduces the number of keys which must be moved to insert a new key. m k adds log(m k ) steps to the binary search but reduces the number of keys which must be moved to insert a new key.
14
Some Insight … The cost of reconfiguration is O(n k *m k+1 ) if the number of keys inserted since the last reconfiguration, n k -n k-1, is proportional to the size of the expanded table, the cost of reconfiguration per key is constant. The cost of reconfiguration is O(n k *m k+1 ) if the number of keys inserted since the last reconfiguration, n k -n k-1, is proportional to the size of the expanded table, the cost of reconfiguration per key is constant.
15
Some Insight … Worst case for insertion is O(n). We will show next that the expected number of moves remains bounded as n k increases (provided r is bounded away from 1). Worst case for insertion is O(n). We will show next that the expected number of moves remains bounded as n k increases (provided r is bounded away from 1).
16
Average behavior of S.T. Proof … Proof …
17
Some theorems … Theorem 1: On insertion (no deletion) the average number of move operations is bounded by a constant. Theorem 1: On insertion (no deletion) the average number of move operations is bounded by a constant. Theorem 2: On the average, insertion requires O(log n) operations. Theorem 2: On the average, insertion requires O(log n) operations. Proof: search takes O(log m)=O(log n). The expected number of moves is constant. Reconfiguration adds only constant time per insertion. Proof: search takes O(log m)=O(log n). The expected number of moves is constant. Reconfiguration adds only constant time per insertion.
18
Improving worst case Worst case for insertion may be quite bad. Worst case for insertion may be quite bad. To improve w.c. performance, an additional structure is imposed to the sparse table. To improve w.c. performance, an additional structure is imposed to the sparse table. The basic idea is to redistribute the keys locally when the local density become high. The basic idea is to redistribute the keys locally when the local density become high.
19
Improving worst case h = log m – log log m h = log m – log log m b = m / 2^h b = m / 2^h log m <= b < 2 log m log m <= b < 2 log m We divide the table into m/b = 2^h blocks B 0,B 1,…,B 2^h-1 We divide the table into m/b = 2^h blocks B 0,B 1,…,B 2^h-1 We shall build a full binary tree of height h with leaves L 0,L 1,…,L 2^h-1. We shall build a full binary tree of height h with leaves L 0,L 1,…,L 2^h-1.
20
Improving worst case Each node v wil have a segment s(v) as follows: Each node v wil have a segment s(v) as follows: –Leaf L i associate the block B i (0<=i<2^h) –Node with children u and w: s(v) = s(u) U s(w) m(v) is the size of s(v) m(v) is the size of s(v) r(v) is the density, the number of geniune keys in s(v) divided by m(v). r(v) is the density, the number of geniune keys in s(v) divided by m(v).
21
Improving worst case The nodes of the tree are divided into levels. The root r is at level 0, and the level of any other node is greater by one than that of it’s parent. The nodes of the tree are divided into levels. The root r is at level 0, and the level of any other node is greater by one than that of it’s parent. The level of the leaves is obviously h. The level of the leaves is obviously h.
22
Improving worst case Let 0<=t L <t U <=1. Let 0<=t L <t U <=1. We define the sequence t0,t1,…,th of threshold densities of nodes in levels 0,1,…,h by: t q =t L +q(t U -t L )/h 0<=q<=h We define the sequence t0,t1,…,th of threshold densities of nodes in levels 0,1,…,h by: t q =t L +q(t U -t L )/h 0<=q<=h t L = t 0 < t 1 < … < t h = t U t L = t 0 < t 1 < … < t h = t U t q+1 – t q = (t U -t L )/h t q+1 – t q = (t U -t L )/h r(L i ) <= t h during the process of insertion. r(L i ) <= t h during the process of insertion.
23
Insertion Algorithm Insert the new key as in the previous algorithm. Insert the new key as in the previous algorithm. If the density of B i <= t h, then insertion completed. If the density of B i <= t h, then insertion completed. Otherwise, find the maximal level q for which r(v q )<t q. Otherwise, find the maximal level q for which r(v q )<t q. If such q is found, then the genuine keys are uniformly distributed. If such q is found, then the genuine keys are uniformly distributed. If q is not found, the table size is increased. If q is not found, the table size is increased.
24
Theorem 3 Performing n-n k-1 insertions into a sparse table of size m=n k-1 m k requires at most O((n-n k-1 )log^2(m/(t U -t L )) ) Performing n-n k-1 insertions into a sparse table of size m=n k-1 m k requires at most O((n-n k-1 )log^2(m/(t U -t L )) ) Proof … Proof …
25
deletions Deletions are easy to implement, but are difficult to analyze statitically. Deletions are easy to implement, but are difficult to analyze statitically. In addition to the regular reconfiguration, reconfiguration will also occur whenever deletion reduces the number of genuine keys to some threshold. In addition to the regular reconfiguration, reconfiguration will also occur whenever deletion reduces the number of genuine keys to some threshold. The authors were not able to provide an analysis od s.t. under a sequence of insertions/deletions. The authors were not able to provide an analysis od s.t. under a sequence of insertions/deletions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.