thm01 - persistent ds_1

Download Report

Transcript thm01 - persistent ds_1

Algorithmic Aspects of Searching in the Past
Lecture 1: Persistent Data Structures
Advanced Topics in Algorithms & Data Structures
Christine Kupich
Institut für Informatik, Universität Freiburg
1
Overview
•
Motivation
•
Example: Natural search trees
•
Making data structures partially persistent
•
Example: Partially persistent red-black trees
•
An application: Point location
•
An application: Grounded 2-dimensional range searching
•
Making data structures fully persistent
2
Motivation
Ephemeral: no mechanism to revert to previous states
A structure is called persistent, if it supports access to multiple versions.
Partially persistent: All versions can be accessed but only the newest version can be
modified.
Fully persistent: All versions can be accessed and modified.
Confluently persistent: Two or more old versions can be combined into one new
version.
Oblivious: The data structure yields no knowledge about the sequence of operations
that have been applied to it other than the final result of the operations.
3
Example: Natural search trees
1
5
3
1
5
7
7
3
Only partially oblivious!
•
Insertion history can sometimes be reconstructed.
•
Deleted keys are not visible.
4
Simple methods for making structures persistent
•
Structure-copying method: Make a copy of the data structure each time it is
changed. Yields full persistence at the price of W(n) time and space per update to a
structure of size n
•
Store a log-file of all updates! In order to access version i, first carry out i updates,
starting with the initial structure, and generate version i. W(i) time per access, O(1)
space and time per update
•
Hybrid-method: Store the complete sequence of updates and additionally each k-th
version for a suitably chosen k. Result: Any choice of k causes blowup in either
storage space or access time
Are there any better methods?
5
Making data structures persistent
Several constructions to make various data structures persistent have been devised,
but no general approach has been taken until the seminal paper by Driscoll,
Sarnak, Sleator and Tarjan, 1986.
They propose methods to make linked data structures partially as well as fully
persistent.
Let’s first have a look at how to make structures partially persistent
6
Fat node method - partial persistence
Record all changes made to node fields in the nodes
• Each fat node contains same fields as ephemeral node and a version
stamp
• Add a modification history to every node: each field in a node contains a list
of version-value pairs
7
Fat node method - partial persistence
Modifications
• Ephemeral update step i creates new node: create a new fat node with
version stamp i and original field values
• Ephemeral update step i changes a field: store the field value plus a
timestamp
Each node knows what its value was at any previous point in time
Access field f of version i
Choose the value with maximum version stamp no greater than i
8
Fat node method - analysis
• Time cost per access gives O(log m) slowdown per node (using binary
search on the modification history)
• Time and Space cost per update step is O(1) (to store the modification
along with the timestamp at the end of the modification history)
9
Fat node method - Example
A partially persistent search tree. Insertions:5,3,13,15,1,9,7,11,10, followed
by deletion of item 13.
1-10
5
2
3
3
6 13
5
9
1
10
77
7
4
15
8
10
10
11
10
9
10
10
Path-copying method - partial persistence
• Make a copy of the node before changing it to point to the new child.
Cascade the change back until root is reached. Restructuring costs
O(height_of_tree) per update operation
• Every modification creates a new root
• Maintain an array of roots indexed by timestamp.
11
Path-copying method - Example
0
version 0:
5
7
1
3
version 1:
Insert (2)
version 2:
Insert (4)
12
Path-copying method - Example
0
version 0:
5
7
1
3
13
Path-copying method - partial persistence
0
1
5
1
1
version 1:
Insert (2)
5
7
3
3
2
14
Path-copying method - partial persistence
0
1
5
version 1:
Insert (2)
5
5
1
1
2
1
3
3
2
7
version 2:
Insert (4)
3
4
15
Node-copying method - partial persistence
Extend each node by a time-stamped modification box (initially empty)
k
lp
t: rp
Version
at/ after time t
rp
Version
before the modification
time t
Searching in version j
Follow an entry pointer with largest version number i, i <= j
Compare keys and follow newest pointer no greater than j
16
Node-copying method - partial persistence
5
1
7
3
version 0
version 1:
Insert (2)
version 2:
Insert (4)
17
Node-copying method - partial persistence
5
1
7
3
version 0:
1 lp
2
version 1:
Insert (2)
18
Node-copying method - partial persistence
5
1
7
3
version 1:
Insert (2)
3
1 lp
2
4
version 2:
Insert (4)
19
Node-copying method - partial persistence
5
1
7
2 rp
3
version 1:
Insert (2)
version 2:
Insert (4)
3
1 lp
2
4
20
Node-copying method - partial persistence
Modification
If modification box empty, fill it.
Otherwise, make a copy of the node, using only the latest values, i.e. value in
modification box plus the value we want to insert, without using modification box
Cascade this change to the node’s parent
If the node is a root, add the new root to a sorted array of roots
Access time gets O(1) slowdown per node, plus additive O(log m) cost for
finding the correct root
21
Node-copying method - Example
A partially persistent search tree. Insertions: 5,3,13,15,1,9,7,11,10, followed by
deletion of item 13.
22
1-2
3-9
10
5
5
5
6
3
13
5
13
4
1
8
9
9
15
7
7
10
11
11
9
10
22
Node-copying method - partial persistence
The amortized costs (time and space) per modification are O(1).
Proof: Using the potential technique
23
Potential technique
Potential technique
The potential is a function of the entire data structure
Definition potential function: A measure of a data structure whose
change after an operation corresponds to the time cost of the operation
• The initial potential has to be equal to zero and non-negative for all
versions
• The amortized cost of an operation is the actual cost plus the change in
potential
• Different potential functions lead to different amortized bounds
24
Node-copying method - partial persistence
Definitions
• Live nodes: they form the latest version (reachable from the root of the
most recent version), dead otherwise
• Full live nodes: live nodes whose modification boxes are full
25
Node-copying method - potential paradigm
The potential function f (T): the number of full live nodes in T
(initially zero)
The amortized cost of an operation is the actual cost plus the change in
potential
Δ f =?
Each modification involves k number of copies, each with a O(1) space
and time cost, and one change to a modification box with O(1) time cost
Change in potential after update operation i: Δ f =
Space: O(k + Δ f), time: O(k + 1 + Δ f)
Hence, a modification takes O(1) amortized space and O(1) amortized
time
26
Red-black trees
Constraints
• All missing nodes are regarded as black
• Any red node has a black parent
• From any node, all paths to a missing node contain the same number
of black nodes
Depth of an n-node red-black tree is at most 2 log n
Root is colored black
27
Red-black trees
Rebalancing transformations - insertion
1.
recolor
bubble the violation up the tree
2.
recolor
28
Red-black trees
Rebalancing transformations - insertion
3.
leaving no
inconsistency
lr + recolor parent
and gran-parent
4.
11
rr
Case 3.
An insertion requires O(log n) recolorings plus at most 2 rotations
29
Red-black trees - partial persistence
A red-black tree can be made partially persistent using the node copying
method at an amortized space cost of O(1) per insertion or deletion and
a worst-case time cost of O(log n) per access, insertion or deletion.
Each node contains:
• a key
• 2 pointers for the successors
• a color bit and
• an extra pointer (version stamp, direction)
Colors are not used in access operations. Old colors can be overwritten
30
Red-black trees - partial persistence
An Example: insert E, C, M, O, N
31
Red-black trees - partial persistence
An Example: insert E, C, M, O, N
1-2
1-2
1
Insert C
r,b E
r,b E
Insert M
r C 2
1-2
r,b E
2
r C
r,b E
2
r C
1-2
3-4
b E
recolor
r M
4
r O
r,b E
2
r,b C
3
b E
Insert O
r M
3-4
E b,r,b
r,b
M
4
r O
32
Red-black trees - partial persistence
1-2
r,b E
2
r,b C
1-2
3-4
b E
r,b
r,b E
2
r,b C
Insert N
M
b E
r,b
M
4
r O
r,b
RR
3-4
4
r O
r N 5
M
r N
r O
33
r,b
M
r N
r M
r O
1-2
r,b E
2
r,b C
r,b N
LR +
recolor
3-4
b E
r,b
M
r N 5
4
r O
r O
1-2
3-5
r,b E
2
r,b C
b E
5
N
r,b M
r,b
M
r
4
r O
34
Application: Grounded 2-Dimensional Range
Searching
Given a set of points, and a query triple (a,b,i)
Report the set of points a<x<b and y<i.
y
i
a
b
x
35
Application: Grounded 2-Dimensional Range
Searching
Version i contains every point for which y<i. Use x-coordinates as keys.
i
a
b
To answer a query: Report all points in version i whose x-coordinates are in [a,b].
Query time?
Persistent red-black tree: Space ?
, preprocessing time ?
36
1-Dimensional Range
Search
37
Application: Planar point location
Suppose that the Euclidian plane is subdivided into polygons by n line
segments that intersect only at their endpoints.
Given such a polygonal subdivision and an on-line sequence of query points
in the plane, the planar point location problem, is to determine for each
query point the polygon containing it.
Measure an algorithm by three parameters:
1) The preprocessing time.
2) The space required for the data structure.
3) The time per query.
38
Planar point location - example
39
Solving planar point location (Cont.)
Dobkin-Lipton:
Partition the plane into vertical slabs by drawing a vertical line through
each endpoint.
Within each slab the lines are totally ordered.
Allocate a search tree per slab containing the lines and with each line
associate the polygon above it.
Allocate another search tree on the x-coordinates of the vertical lines
40
Planar point location -- example
41
Solving planar point location (Cont.)
To answer a query:
first find the appropriate slab
then search the slab to find the polygon
Query time is O(log n)
How about the space ?
42
Planar point location -- bad example
Total # lines O(n), and number of lines in each slab is O(n).
43
Planar point location & persistence
So how do we improve the space bound ?
Key observation: The lists of the lines in adjacent slabs are very similar.
Create the search tree for the first slab.
Then obtain the next one by deleting the lines that end at the corresponding
vertex and adding the lines that start at that vertex
How many insertions/deletions are there all together ?
2n (One insertion and one deletion per segment)
44
Planar point location & persistence (cont)
Updates should be persistent since we need all search trees at the end.
Partial persistence is enough.
Well, we already have the path copying method, lets use it.
What do we get ?
O(n log n) space and O(n log n) preprocessing time.
Using the node-copying method, we can improve the space bound to O(n).
45
Making data structures fully persistent
•
With this type of persistence the versions don't form a simple linear path, they form
a version tree (since you can also update in the past). Lack of linear ordering.
•
Impose a total ordering on the versions (version list)
•
The version list defines a preorder on the version tree (for navigation): for any
version i, the descendants of i in the version tree occur consecutively in the version
list, starting with i.
0
1
version list:
5
6
A version tree
2
7
3
4
46
Making data structures fully persistent
Search tree
versions:
0
1
6
7
iE
2
iA
iG
10
iK
11 dE
8
iC
3 iM
iA
9 iM
4 iI
5
dM
12 iO
47
Full persistence
It must be possible to:
•
perform insertions in the version list and
•
given two versions i and j, determine whether i precedes or follows j in the version
list
This list order problem has been addressed by Dietz and Sleator
•
order queries are answered in O(1) worst case time with an O(1) amortized time
bound for insertion
48
Fat node method - full persistence
•
Each fat node contains same fields as ephemeral node plus space for extra fields
(each with a field name and a version stamp)
•
Each field in a node contains a list of version-value pairs
Access
•
Versions are compared with respect to their position in the version list, not with
respect to their numeric values
•
Access a field in version i: search for the version stamp rightmost in version list,
but not to the right of i
49
Fat node method - Example
Version list:
1,6,7,10,11,2,8,9,3,4,5,12
0
1
6
7
11
iE
2
iA
iG
10
iK
11 dE
6
8
A
iC
2
3 iM
iA
9 iM
4 iI
8
A
11-10, 12
E21
0 7
2
,
C
1
3
2 11
G
3
3
9
5
M
M
4
2
I
10
K
12
O
5
dM
A fully persistent search tree
12 iO
50
Fat node method - full persistence
Update operation i
•
Add i to the version list
•
Update step creates new node: create new fat node with original field values (stamp i)
•
Update step changes a field f: we have to guarantee that the new value of f will be used only
in version i
Time cost per access and update step
•
O(log m), provided each set of field values is stored in a search tree, ordered by version
stamp
Space cost
•
Worst-case space cost per update step is O(1)
51
Applications
Partially persistent balanced search trees
•
give a simple solution to the planar point location problem, the grounded 2dimensional range searching problem, …
•
can be used as a substitute for Chazelle‘s hive graph (geometric retrieval)
Fully persistent data structures can be used for
•
the binary dispatching problem (OO – languages: find for a invocation the most
specific applicable method)
•
text editing
Oblivious data structures
•
cryptography
52
References
•
J. R. Driscoll, N. Sarnak, D. D. Sleator, and R. E. Tarjan: Making data structures
persistent. Journal of Computer and System Sciences, 38:86-124, 1989. Final
version.
•
N. Sarnak, R. E. Tarjan. Planar Point Location Using Persistent Search Trees:
Communications of the ACM,29:669 – 679, July 1986.
•
D. Micciancio: Oblivious Data Structures: Applications to Cryptography.1997.
53