Docsity

MIT 6.851 Advanced Data Structures, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
MIT 6.851
Advanced Data Structures
Prof. Erik Demaine
Spring ’12 Scribe Notes Collection
TA: Tom Morgan, Justin Zhang
Editing: Justin Zhang
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download MIT 6.851 Advanced Data Structures and more Exams Data Structures and Algorithms in PDF only on Docsity!

MIT 6.

Advanced Data Structures

Prof. Erik Demaine

Spring ’12 Scribe Notes Collection

TA: Tom Morgan, Justin Zhang

Editing: Justin Zhang

Contents

1 1. Temporal data structure 1 4

Scribers: Oscar Moll (2012), Aston Motes (2007), Kevin Wang (2007) 1.1 Overview.......................................... 4 1.2 Model and definitions................................... 4 1.3 Partial persistence..................................... 6 1.4 Full persistence....................................... 9 1.5 Confluent Persistence.................................... 12 1.6 Functional persistence................................... 13

2 2. Temporal data structure 2 14

Scribers: Erek Speed (2012), Victor Jakubiuk (2012), Aston Motes (2007), Kevin Wang (2007) 2.1 Overview.......................................... 14 2.2 Retroactivity........................................ 14

3 3. Geometric data structure 1 24

Scribers: Brian Hamrick (2012), Ben Lerner (2012), Keshav Puranmalka (2012) 3.1 Overview.......................................... 24 3.2 Planar Point Location................................... 24 3.3 Orthogonal range searching................................ 27 3.4 Fractional Cascading.................................... 33

4 4. Geometric data structure 2 35

Scribers: Tim Kaler(2012), Jenny Li(2012), Elena Tatarchenko(2012)

Scribers: Edward Z. Yang (2012), Katherine Fang (2012), Benjamin Y. Lee

Scribers: Karan Sagar (2012), Jacob Hurwitz (2012), Jingjing Liu (2010),

Scribers: Leon Bergen (2012), Andrea Lincoln (2012), Tana Wattanawaroon

Scribers: Josh Alman (2012), Vlad Firoiu (2012), Di Liu (2012), TB Schardl

8.2 Conceptual binary tree showing how to build the intervals. If we insert a new element in a leaf and there is not enough room for it and interval is too dense (showed in light gray), we are going to walk up the tree and look at a bigger interval (showed in dark gray) until we find the right interval. In the end we at most get to the root which means redistribution of the the entire array................... 69 8.3 A cache-oblivious priority queue............................. 71

Lecture 9 9.1 Memory layout for K-funnel sort.............................. 74 9.2 Filling a buffer........................................ 75 9.3 Left and Right slabs containing points and rectangles.................. 76 9.4 Slabs that also contain slabs that span either L or R................... 77 9.5 Depicts the three different types of range queries..................... 77 9.6 A high level description of the datastructure which supports 2 sided range queries using only linear space................................... 78 9.7 An example showing how our first attempt may construct an array with size quadratic in N.............................................. 79

Lecture 10

Lecture 11 11.1 A visual representation of the recursive structure of a VEB. Note that at each level, the minimum is stored directly, and not recursively.................... 95

Lecture 12 12.1 An example of the sketch function with 4 keys. The levels corresponding to the 3 bits sketched are circled................................... 100 12.2 An example when the search query is not among the keys of the fusion node. The paths to the keys are bolded, whereas the path to the query q is dashed; the levels corresponding to the bits sketched are circled as before. Here, the sketch neighbours of q are x 0 and x 1 , but x 0 is neither a predecessor nor successor of q.......... 101

Lecture 13

Lecture 14

Lecture 15

Lecture 16 16.1 On the left we have node v with 5 children. Each triangle represents a subtree, and the size of the triangle represents the weight of the subtree (the number of descendand leaves of the subtree). On the right we can see the weight balanced BST. The solid lines are the edges of the weight balanced BST and the dashed lines represent the initial edges to the children of v....................... 131

Lecture 17 17.1 i − 1 and i are on the same level.............................. 142 17.2 i − 1 and i are different levels................................ 142 17.3 Division of bit string into chunks and sub-chunks, as in the rank algorithm..... 144 17.4 Division of bit string into uneven chunks each containing the same number of 1’s, as in the select algorithm................................... 145 17.5 An example binary trie with circled right spine...................... 146 17.6 A Rooted Ordered Tree That Represents the Trie in Figure 17.5............ 147 17.7 A Balanced Parentheses String That Represents the Ordered Tree in Figure 17.6.. 147

Lecture 18

Lecture 19

Lecture 20 20.1 An example tree and its Euler tour. The order in which nodes in the tree are visited is indicated by the curved red arrows. The Euler tour of the tree shown here corresponds to the sequence: R A R B C D C E C B F B G B R.......... 166 20.2 We represent a tree’s Euler tour as a sequence of visitations starting and ending at the root. Pointers from that sequence point to the node, and pointers from the node point to the first and last visitations. In this figure only the pointers relating to the root are shown........................................ 167

Lecture 21 21.1 A √n by √n grid of vertices, with one of the disjoint paths darkened......... 173

Lecture 1

Temporal data structure 1

Scribes: Oscar Moll (2012), Aston Motes (2007), Kevin Wang (2007)

1.1 Overview

In this first lecture we cover results on persistent data structures, which are data structures where we keep all information about past states. Persistent data structures are part of the larger class of temporal data structures. The other kind of temporal data structures, retroactive data structures, are the topic of lecture 2. Usually we deal with data structure updates by mutating something in the existing data structure: either its data or the pointers that organize it. In the process we lose information previous data structures states. Persistent data structures do not lose any information. For several cases of data structures and definitions of persistence it is possible to transform a plain data structure into a persistent one with asymptotically minimal extra work or space overhead. A recurring theme in this area is that the model is crucial to the results. Partial and full persistence correspond to time travel with a branching universe model such as the one in Terminator, and Deja Vu parts 1 and 2

1.2 Model and definitions

1.2.1 The pointer machine model of data structures

In this model we think of data structures as collections of nodes of a bounded size with entries for data. Each piece of data in the node can be either actual data, or a pointer to a node. The primitive operations allowed in this model are:

  1. x = new Node() 13
  1. x = y.field
  2. x.field = y
  3. x = y + z, etc (i.e. data operations)
  4. destroy(x) (if no other pointers to x)

Where x, y, z are names of nodes or fields in them. Data structures implementable with these shape constraints and these operations includes linked lists and binary search trees, and in general corresponds to struct’s in C or objects in Java. An example of a data structure not in this group would be a structure of variable size such as an array.

1.2.2 Definitions of persistence

We have vaguely referred to persistence as the ability to answer queries about the past states of the structure. Here we give several definitions of what we might mean by persistence.

  1. Partial Persistence – In this persistence model we may query any previous version of the data structure, but we may only update the latest version. We have operations read(var, version) and newversion = write(var, val). This definition implies a linear ordering on the versions like in 1.1a.
  2. Full Persistence – In this model, both updates and queries are allowed on any version of the data structure. We have operations read(var, version) and newversion = write(var, version, val). The versions form a branching tree as in 1.1b.
  3. Confluent Persistence – In this model, in addition to the previous operation, we allow combi- nation operations to combine input of more than one previous versions to output a new single version. We have operations read(var, version), newversion = write(var, version, val) and newversion = combine(var, val, version1, version2). Rather than a branch- ing tree, combinations of versions induce a DAG (direct acyclic graph) structure on the version graph, shown in 1.1c
  4. Functional Persistence – This model takes its name from functional programming where objects are immutable. The nodes in this model are likewise immutable: revisions do not alter the existing nodes in the data structure but create new ones instead. Okasaki discusses these as well as other functional data structures in his book [10].

The difference between functional persistence and the rest is we have to keep all the structures related to previous versions intact: the only allowed internal operation is to add new nodes. In the previous three cases we were allowed anything as long as we were able to implement the interface. Each of the succeeding levels of persistence is stronger than the preceding ones. Functional implies confluent, confluent implies full, and full implies partial. Functional implies confluent because we are simply restricting ways on how we implement persis- tence. Confluent persistence becomes full persistence if we restrict ourselves to not use combinators. And full persistence becomes partial when we restrict ourselves to only write to the latest version.

14

  1. (new) a writable modifications (‘mods’) area for entries of the form (field, version, value). The size of this area also needs to be fixed, and it also has important consequences for write performance.

For an illustration of the construction check figures 1.2a and 1.2b We implement read and write operations as follows:

  1. read(var, v) search the mod log for the largest version w such that w ≤ v. What if the value is in an ‘old’ node? then we would have gotten to it via an old version of a pointer (c.f. Figure 1.2c and Figure 1.3).
  2. write(var, val) if n is not full, simply add to mod log. if n has no space for more mod logs, - n′^ = new Node() - copy latest version of each field (data and forward pointers) to the static field section. - also copy back pointers to n′ - for every node x such that n points to x, redirect its back pointers to n′^ (using our pointers to get to them) (at most d of them). - for every node x such that x points to n, call write(x.p, n’) recursively (at most p recursive calls).

For some data structures such as lists or trees we often know what p is ahead of time, so we can implement the algorithm for these specific structures like in figure 1.3.

1.3.2 Analysis:

  • Space: If we choose the mod log to be bounded at size 2p then node has size d + p + 2p which is also O(1) because we assumed there were only p ≤ O(1) pointers into any node. The reasons for choosing such a mod log size are clarified in the following cost analysis.
  • Time: A read is cheap, it requires constant time to check through a single node’s mod log and pick the required version. A write is also cheap if we have space in the mod log. If not, a write can be expensive. Let cost(n) denote the cost of writing to node n. In the worst case the algorithm makes recursive calls so we get: cost(n) = c + ∑ x → n

(cost(x))

Where c represents the O(1) cost of determining latest versions to copy into the new node, copying backpointers, etc. x → n stands for x points to n. Clearly the cost can be large because we could split many data nodes in the recursive steps. However, we know when a node becomes full then next operation will likely find a more empty

16

(a) Ephemeral linked structure. It has one data field and one pointer field

(b) The structure in 1.2a partially persistent. Showing one update: write(root.ptr.val = 20)

(c) The structure in 1.2b after a second update: write(root.ptr.val = 200). Gray indicates the data node is read only

Figure 1.2: Constructing a partially persistent structure from an ephemeral one

  1. Now we have to support writes to any version. This affects how we handle writes: we no longer have the concept of ‘current’ vs. ‘old’ nodes: every node needs to support writes.

1.4.1 Version representation

The version representation problem is solved by keeping a tree representing the version structure, as well as an efficient representation of this tree in a linearized way. The linearized representation of the tree in the figure 1.4 is ba, bb, bc, ec, eb, bd, ed, ea. You can read ba as ‘begin node a’ and ea as ‘end node a’. This representation losslessly encodes the tree, and we can directly answer queries about the tree using that encoded representation. For example we know c is nested within b, since bb < bc and ec < eb.

Figure 1.4: in-order tree traversal

This linearized representation can be implemented using an ‘order maintenance’ data structure. For now, it suffices to say an order maintenance data structure supports the following two operations, both in O(1) time.

  • insert an item before or after a specified element.
  • check if item s precedes item t.

For example, a linked list supports insertions in O(1), but tests for precedence take O(n). Similarly, a balanced BST supports both operations but in O(log n) time. Deitz and Sleator show an O(1) implementation for both operations in [4], which will be covered in lecture 8. To implement version tree queries such as ‘is version v an ancestor of version w’ we can use two comparison queries bv < bw and ew < ev in O(1). To implement updates like ‘add version v as a child of version w’ we can insert the two elements bv and ev after bw and before ew respectively, also in O(1).

1.4.2 Construction and algorithm:

The nodes in our data structure will keep the same kinds of additional data per node as they did in the partially persistent case.

19

Figure 1.5: Splitting a tree-shaped version genealogy into two subtrees

For each node we store d data entries and p back pointers, but now allow up to 2(d + p + 1) modifications. The amount of data d is also a bound on out-pointers per node. Additionally we now also version back-pointers.

  1. read(n.field, version): By using the order-maintenance data structure we can pick the most recent ancestor of version from among the entries in the mod log and return that value.
  2. write(n.field, value, version): If there is space in node, just add mod entry. else:
    • m = new Node(). Split the contents of node n’s mod log into two parts following the diagram figure 1.5. Partitioning into subtrees rather than arbitrarily is required. Now node m has some of the mods of the internal tree in Figure 1.5, and node n retains the ‘older’ half of the updates.
    • from the ‘old’ mod entries in node n, compute the latest values of each field and write them into the data and back pointer section of node m.
    • recursively update all (up to) d + p + (d + p + 1) forward and backward pointers of neighbors.
    • insert new version to our version tree representation.

1.4.3 Analysis:

  • space – 1 if we do not split or d + p + 2(d + p + 1) = 3d + 3p + 2 when we split a node, both O(1)
  • time – read(var, version) is implemented in a similar way to the partial case. We use our auxiliary version tree data structure to find the largest ancestor of version from among a list of O(1) elements in O(1). Like with the partial case, writes are cheap when a node has space in its mods log and more expensive when nodes are full.

20