25
A sparse Table A sparse Table implementation implementation of Priority Queue of Priority Queue Presented by: Presented by: Yaniv Yaniv Nahum Nahum Written by: Written by: Alon Itai Alon Itai Alan G. Alan G. Konheim Konheim Michael Rodeh Michael Rodeh

A sparse Table implementation of Priority Queue

  • Upload
    jensen

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

A sparse Table implementation of Priority Queue. Presented by: Yaniv Nahum Written by:Alon Itai Alan G. Konheim Michael Rodeh. PQ - introduction. A data structure with the following operations: Search (x) Insert (x) Delete (x) Min Next (x) Scan. PQ - introduction. - PowerPoint PPT Presentation

Citation preview

Page 1: A sparse Table implementation  of Priority Queue

A sparse Table A sparse Table implementation implementation of Priority Queueof Priority Queue

Presented by: Presented by: Yaniv Yaniv NahumNahumWritten by:Written by: Alon ItaiAlon Itai

Alan G. KonheimAlan G. KonheimMichael RodehMichael Rodeh

Page 2: A sparse Table implementation  of Priority Queue

PQ - introductionPQ - introduction

A data structure with the A data structure with the following operations:following operations:– Search (x)Search (x)– Insert (x)Insert (x)– Delete (x)Delete (x)– MinMin– Next (x)Next (x)– ScanScan

Page 3: A sparse Table implementation  of Priority Queue

PQ - introductionPQ - introduction

Most implementations use trees (2-Most implementations use trees (2-3, AVL, weight balanced trees) and 3, AVL, weight balanced trees) and requires no more than O (log n) requires no more than O (log n) time.time.

In many cases, the algorithms In many cases, the algorithms devote much of their running time devote much of their running time and storage manipulating the PQ, and storage manipulating the PQ, often rendering a theoretically often rendering a theoretically efficient algorithm to infeasible or efficient algorithm to infeasible or inefficient to all practical purposes.inefficient to all practical purposes.

Page 4: A sparse Table implementation  of Priority Queue

PQ as Sparse table PQ as Sparse table

Data is stored in linear array, and Data is stored in linear array, and requires only one pointer.requires only one pointer.

Insertion requires:Insertion requires:– Searching.Searching.– Moving (shifting of records).Moving (shifting of records).– Reconfiguring (increase size and Reconfiguring (increase size and

distribute keys).distribute keys).

Page 5: A sparse Table implementation  of Priority Queue

PerformancePerformance

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 We will show that the expected

number of moves is constant.number of moves is constant. We will present a more We will present a more

complicated table, in order to complicated table, in order to improve worst case, which takes O improve worst case, which takes O (n*log^2(n)) to build.(n*log^2(n)) to build.

Page 6: A sparse Table implementation  of Priority Queue

Sparse table schemataSparse table schemata

Records are stored in a sorted Records are stored in a sorted order.order.

Insertion is improved by Insertion is improved by introducing gaps in the table, introducing gaps in the table, thereby storing n records in a thereby storing n records in a table of capacity m for some m > table of capacity m for some m > n.n.

m-n keys are m-n keys are dummy keysdummy keys..

Page 7: A sparse Table implementation  of Priority Queue

exampleexample

Genuine keys are located at Genuine keys are located at addresses 2,5,6,8 in the following addresses 2,5,6,8 in the following table: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.

Page 8: A sparse Table implementation  of Priority Queue

Building a tableBuilding a table

Determined by 2 sequences of Determined by 2 sequences of real numbers:real numbers:– {n{nkk: 0<=k<INFINITY}: 0<=k<INFINITY}– {m{mkk: 1<=k<INFINITY}: 1<=k<INFINITY}– 1=n1=n00 < n < n11 < … < … < … < …– nnkk <= n <= nk-1k-1*m*mkk 1<=k1<=k

Page 9: A sparse Table implementation  of Priority Queue

Building a tableBuilding a table

The size of the table, m, can only The size of the table, m, can only take the values take the values nnk-1k-1*m*mk k for k=1,2,for k=1,2,……

The size of the table is m=The size of the table is m=nnk-1k-1*m*mkk when the number of distinct keys when the number of distinct keys in the table, n, satisfies in the table, n, satisfies nnk-k-

11<n<=n<n<=nkk

Page 10: A sparse Table implementation  of Priority Queue

InsertionInsertion

Inserting x into a table of size m=Inserting x into a table of size m=nnk-k-

11*m*mk k containing containing nnk-1k-1<n<=n<n<=nk k genuine genuine keys keys find address s satisfying find address s satisfying yys-s-

11<x<=y<x<=ys s

If If yyss is a dummy key, it is replaced by x, is a dummy key, it is replaced by x, yielding the table: (yyielding the table: (y00,…,y,…,ys-1s-1,x,y,x,ys+1s+1,…y,…ym-1m-1))

If If yyss is a genuine key, the block of t is a genuine key, the block of t consecutive genuine keys is shift right consecutive genuine keys is shift right one position.one position.

Page 11: A sparse Table implementation  of Priority Queue

Insertion (Cont …)Insertion (Cont …)

X is inserted at address s yielding X is inserted at address s yielding the table:the table:

(y(y00,…,y,…,ys-1s-1,x,y,x,yss,…,y,…,ys+t-1s+t-1,y,ys+t+1s+t+1,…,y,…,ym-1m-1)) Note: yNote: ys+ts+t is the dummy key is the dummy key

removed from table.removed from table.

Page 12: A sparse Table implementation  of Priority Queue

reconfigurationreconfiguration

When inserting x to a table with When inserting x to a table with size m=size m=nnk-1k-1**mmkk, and there are n, and there are nkk genuine keys.genuine keys.

The size of the table first The size of the table first increases to nincreases to nkk*m*mk+1k+1, and the n, and the nkk genuine keys are uniformly genuine keys are uniformly distribute in it.distribute in it.

Than, x is inserted regularly.Than, x is inserted regularly.

Page 13: A sparse Table implementation  of Priority Queue

Some Insight …Some Insight …

The numbers {mThe numbers {mkk} are the expansion } are the expansion factors.factors.

The ratio r = n / The ratio r = n / nnk-1k-1**mmkk with with nnk-1k-1<n<=n<n<=nk k is the density of the genuine is the density of the genuine keys in the table.keys in the table.

1 / m1 / mkk < r <= 1 < r <= 1 mmkk adds log(m adds log(mkk) steps to the binary ) steps to the binary

search but reduces the number of keys search but reduces the number of keys which must be moved to insert a new which must be moved to insert a new key.key.

Page 14: A sparse Table implementation  of Priority Queue

Some Insight …Some Insight …

The cost of reconfiguration is The cost of reconfiguration is O(nO(nkk*m*mk+1k+1) ) if the number of if the number of keys inserted since the last keys inserted since the last reconfiguration, nreconfiguration, nkk-n-nk-1k-1, is , is proportional to the size of the proportional to the size of the expanded table, the cost of expanded table, the cost of reconfiguration per key is reconfiguration per key is constant.constant.

Page 15: A sparse Table implementation  of Priority Queue

Some Insight …Some Insight …

Worst case for insertion is O(n). Worst case for insertion is O(n). We will show next that the We will show next that the expected number of moves expected number of moves remains bounded as nremains bounded as nkk increases increases (provided r is bounded away from (provided r is bounded away from 1).1).

Page 16: A sparse Table implementation  of Priority Queue

Average behavior of Average behavior of S.T. S.T. Proof …Proof …

Page 17: A sparse Table implementation  of Priority Queue

Some theorems …Some theorems …

Theorem 1:Theorem 1: On insertion (no deletion) the On insertion (no deletion) the average number of move operations is average number of move operations is bounded by a constant.bounded by a constant.

Theorem 2:Theorem 2: On the average, insertion On the average, insertion requires O(log n) operations.requires O(log n) operations.

Proof:Proof: search takes O(log m)=O(log n). search takes O(log m)=O(log n). The expected number of moves is The expected number of moves is constant. constant. Reconfiguration adds only constant time Reconfiguration adds only constant time per insertion. per insertion.

Page 18: A sparse Table implementation  of Priority Queue

Improving worst caseImproving worst case

Worst case for insertion may be Worst case for insertion may be quite bad.quite bad.

To improve w.c. performance, an To improve w.c. performance, an additional structure is imposed to additional structure is imposed to the sparse table.the sparse table.

The basic idea is to redistribute The basic idea is to redistribute the keys locally when the local the keys locally when the local density become high.density become high.

Page 19: A sparse Table implementation  of Priority Queue

Improving worst caseImproving worst case

h = log m – log log mh = log m – log log m b = m / 2^hb = m / 2^h log m <= b < 2 log mlog m <= b < 2 log m We divide the table into m/b = We divide the table into m/b =

2^h blocks B2^h blocks B00,B,B11,…,B,…,B2^h-12^h-1

We shall build a full binary tree of We shall build a full binary tree of height h with leaves Lheight h with leaves L00,L,L11,…,L,…,L2^h-12^h-1..

Page 20: A sparse Table implementation  of Priority Queue

Improving worst caseImproving worst case

Each node v wil have a segment Each node v wil have a segment s(v) as follows:s(v) as follows:– Leaf LLeaf Lii associate the block B associate the block Bi (0<=i<2^h) i (0<=i<2^h)

– Node with children u and w: Node with children u and w: s(v) = s(u) U s(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 r(v) is the density, the number of

geniune keys in s(v) divided by geniune keys in s(v) divided by m(v).m(v).

Page 21: A sparse Table implementation  of Priority Queue

Improving worst caseImproving worst case

The nodes of the tree are divided The nodes of the tree are divided into levels. The root r is at level 0, into levels. The root r is at level 0, and the level of any other node is and the level of any other node is greater by one than that of it’s greater by one than that of it’s parent.parent.

The level of the leaves is The level of the leaves is obviously h.obviously h.

Page 22: A sparse Table implementation  of Priority Queue

Improving worst caseImproving worst case

Let 0<=tLet 0<=tLL<t<tUU<=1.<=1. We define the sequence t0,t1,…,th We define the sequence t0,t1,…,th

of threshold densities of nodes in of threshold densities of nodes in levels 0,1,…,h by: tlevels 0,1,…,h by: tqq=t=tLL+q(t+q(tUU-t-tLL)/h )/h 0<=q<=h0<=q<=h

ttLL = t = t00 < t < t11 < … < t < … < thh = t = tUU

ttq+1q+1 – t – tqq = (t = (tUU-t-tLL)/h)/h r(Lr(Lii) <= t) <= thh during the process of during the process of

insertion.insertion.

Page 23: A sparse Table implementation  of Priority Queue

Insertion AlgorithmInsertion Algorithm

Insert the new key as in the Insert the new key as in the previous algorithm.previous algorithm.

If the density of BIf the density of Bii <= t <= thh, then , then insertion completed.insertion completed.

Otherwise, find the maximal level q Otherwise, find the maximal level q for which r(vfor which r(vqq)<t)<tqq..

If such q is found, then the genuine If such q is found, then the genuine keys are uniformly distributed.keys are uniformly distributed.

If q is not found, the table size is If q is not found, the table size is increased.increased.

Page 24: A sparse Table implementation  of Priority Queue

Theorem 3Theorem 3

Performing n-nPerforming n-nk-1k-1 insertions into a insertions into a sparse table of size m=nsparse table of size m=nk-1k-1mmkk requires at most O((n-nrequires at most O((n-nk-k-

11)log^2(m/(t)log^2(m/(tUU-t-tLL)) ))) ) Proof …Proof …

Page 25: A sparse Table implementation  of Priority Queue

deletionsdeletions

Deletions are easy to implement, but are Deletions are easy to implement, but are difficult to analyze statitically.difficult to analyze statitically.

In addition to the regular reconfiguration, In addition to the regular reconfiguration, reconfiguration will also occur whenever reconfiguration will also occur whenever deletion reduces the number of genuine deletion reduces the number of genuine keys to some threshold.keys to some threshold.

The authors were not able to provide an The authors were not able to provide an analysis od s.t. under a sequence of analysis od s.t. under a sequence of insertions/deletions.insertions/deletions.