440 likes | 713 Views
Randomized Algorithms: Data Structures. Randomized Treaps. Treap = TREe heAP Or, In Hebrew: ערמץ = ערמה עץ. Outline. Definition of the Problem Deterministic Data Structures Introducing: Treaps Analyzing Performance Conclusion. Definition of the Problem.
E N D
Randomized Algorithms: Data Structures Randomized Algorithms - Treaps
Randomized Treaps • Treap = TREe heAP • Or, In Hebrew: ערמץ = ערמה עץ Randomized Algorithms - Treaps
Outline • Definition of the Problem • Deterministic Data Structures • Introducing: Treaps • Analyzing Performance • Conclusion Randomized Algorithms - Treaps
Definition of the Problem • Maintain a collection of items. • Each of which is given a unique key. • Support a group of 7 operations and queries over these items: Randomized Algorithms - Treaps
MakeSet(S) • Find(k, S) • Insert(k, S) • Join(S1, k, S2) Randomized Algorithms - Treaps
Delete(k, S) • Paste(S1, S2) • Split(k, S) Randomized Algorithms - Treaps
Deterministic Data StructuresBinary Search Trees • A standard solution to the problem. • For simplicity we will assume that the trees are endogenous, and thus, also full. Randomized Algorithms - Treaps
Binary Search TreesPerformance Issues • Each of the 7 aforementioned operations could be implemented in o(H). • Ideally . • Easy to devise an order of insertion in which . Randomized Algorithms - Treaps
Binary Search TreesPossible Deterministic Improvements • Balancing the bst • using rotations at each update operation. • Drawback • Could render significant overhead at each update operation. Randomized Algorithms - Treaps
Splaying: • Move to the root a node accessed by find. • Analysis: • Guarantees an amortized time bound of o(log n). Randomized Algorithms - Treaps
Drawback: • Restructuring of the tree at each find operation. • No assurance that a specific action will take a certain amount of time. Randomized Algorithms - Treaps
Introducing: Treaps • Consider a binary tree where each node v contains a pair of values (both should be distinct from other nodes) : • a key – should suffice the binary search condition. • a priority – should suffice the heap condition. Randomized Algorithms - Treaps
7,30 4,26 11,27 2,13 12,22 • For Example:the set could be represented by the valid treap: Randomized Algorithms - Treaps
Treapsimplementation (in brief) • All operations are implemented in a similar way to the BST solutions available. • When an action violates heap condition, use rotations. Randomized Algorithms - Treaps
How a rotation is made b a C a b A B A B C Randomized Algorithms - Treaps
Note, for future analyzing purposes, that all operations could be based upon find, insert and delete operations. Randomized Algorithms - Treaps
On choosing priorities • Theorem:Let S= be any set of key-priority pairs such that the keys and the priorities are distinct. Then, there exist a unique treap T(S) for it. Randomized Algorithms - Treaps
Proof:By induction: • for n=0, n=1: the theorem holds. Randomized Algorithms - Treaps
for n>1:without the loss of generality, assume that V = (k1,p1) is the node with the highest priority: • Assign V to root. • recursively apply on both sub trees emerging from splitting by p1. Randomized Algorithms - Treaps
The theorem implies that we may choose any (distinct) priorities to our nodes, ensuring that are we are representing a valid treap. Randomized Algorithms - Treaps
We will choose our priorities in the following manner: • Upon insertion of a new key, a priority is randomly and uniformly chosen for it, in such a way that no previous node contains this priority. • The priority for a specific element remains constant. • The ordering of keys and values is completely uncorrelated. Randomized Algorithms - Treaps
Analyzing PerformanceMulmuley Games • For the sake of time performance, we will present a set of games, called Mulmuley Games. Randomized Algorithms - Treaps
Mulmuley GamesGame A • Participants: • A set of players. Each assigned a distinct numerical key. • A set of bystanders. Each assigned a distinct numerical key. Randomized Algorithms - Treaps
The course of the game consists of repeatedly sampling from P union B. Without replacement, until the pool is empty. • A random variable V is defined as the number of samples in which a player p is chosen such that p is the largest player chosen to this point. • The value of the game Ap is E[V]. Randomized Algorithms - Treaps
Lemma: for all p > 0, Ap = Hp. Randomized Algorithms - Treaps
Mulmuley GamesGame D • Participants: • A set of players. Each assigned a distinct numerical key. • A set of bystanders. Each assigned a distinct numerical key. • A set of triggers. Each assigned a distinct numerical key. Randomized Algorithms - Treaps
The course of the game consists of repeatedly sampling from P union B union T. Without replacement, until the pool is empty. • A random variable V is defined as the number of samples in which a player p is chosen such that p is the largest player chosen to this point. Only after a trigger was chosen. • The value of the game is E[V]. Randomized Algorithms - Treaps
Lemma: Randomized Algorithms - Treaps
Analyzing Performance • We will present and prove several theorems, regarding time bounds of operations on random treaps. • For the following, assume that T is a random treap for a set S of size n. Randomized Algorithms - Treaps
Theorem: The expected time for find, insert and delete operations on T is O(log n). Randomized Algorithms - Treaps
Lemma: for an element of rank k (regarding its key), it holds that: • The lemma implies that the depth of each element is o(log n)each of the operations ( find, insert and delete) are linear in reagardsto the tree depth, it follows that they also are o(log n). Randomized Algorithms - Treaps
Proof of the lemma: let us define: Randomized Algorithms - Treaps
- Ancestors of x Randomized Algorithms - Treaps
it is clear that : depth(x) = • And thus E[depth(x) ] = • Therfore, we will try and estimate and Randomized Algorithms - Treaps
Let us write S= ordered by their priority.(hence p1 > p2 > … > pn) • We will insert the elements into the tree by this order (notice that no rotations are needed) • When will an element belong to S- ? Randomized Algorithms - Treaps
In other words, we will be counting right-going arcs in the path between the root and x. • Inserting elements larger than x,doesn’t affect the counting • An element will belong to q- if itis the largest element of s- inserted yet. 7,30 4,26 11,27 2,23 5,20 12,22 Randomized Algorithms - Treaps
Notice that this resembles Game A with P = S- , B = S\S- and thus p = k, following that • Similarly it can be shown that • Therefore Randomized Algorithms - Treaps
Theorem: The expected number of rotations required during an insert or delete operation is at most 2. • We will prove that for each element x defined as before, it holds that the number of rotations is 2. Randomized Algorithms - Treaps
Proof: • Let us define: Rx – the right spine of the left subtree of X. Lx – the left spine of the right subtree of X. • Observation 1: the number of rotations needed during these operations is |Rx| + |Lx| Randomized Algorithms - Treaps
In a similar to the one taken in the previous theorem, we can show that E[|Rx|] is in fact similar to Game D with p = k-1 , t = 1 , b = n – k Which yeilds that E[|Rx|] = 1 – 1/k. • It can also be shown, by the same method that E[|Lx|] = 1 – 1/(n-k+1). • Therefore E[|Lx|] + E[|Rx|] <= 2 Randomized Algorithms - Treaps
Theorem: The expected time for join, pasteand split involving sets S1 and S2 of sizes n and m respectively, is o(log n + log m). Randomized Algorithms - Treaps