Presentation is loading. Please wait.

Presentation is loading. Please wait.

Persistent deques.

Similar presentations


Presentation on theme: "Persistent deques."— Presentation transcript:

1 Persistent deques

2 Lets revisit queues first.
Only pop & inject It will be possible to generalize our solution in various ways so we can get a purely functional solution with O(1) time per push, pop, inject, eject, and catenate !

3 Queues revisited Represent a queue over A by a triple
(prefix, queue over AA, suffix) Prefix and suffix contain at most 2 elements of the queue. prefix suffix prefix suffix

4 Inject prefix suffix prefix suffix

5 Inject prefix suffix suffix prefix suffix

6 Inject suffix prefix suffix suffix suffix prefix suffix

7 Inject suffix suffix prefix suffix suffix suffix prefix suffix

8 Inject prefix suffix suffix suffix suffix prefix suffix suffix

9 Inject prefix suffix suffix suffix suffix suffix prefix suffix suffix

10 Inject suffix prefix suffix suffix suffix suffix suffix suffix prefix

11 Inject prefix suffix suffix suffix suffix suffix suffix prefix suffix

12 Inject prefix suffix suffix suffix suffix suffix suffix suffix prefix

13 result = inject(x,d) If (suffix(d) = (a,b) ) then
d’ := inject(child(d),(a,b)) child(d) := d’ suffix(d) := empty endif allocate(result) prefix(result) := prefix(d) child(result) := child(d) suffix(result) := allocate(suffix(d) + x)

14 (x,r)= pop(Q) prefix suffix prefix suffix

15 (x,r)= pop(Q) prefix prefix suffix prefix suffix

16 (x,r)= pop(Q) prefix prefix suffix prefix suffix

17 (x,r)= pop(Q) prefix prefix prefix prefix suffix prefix suffix Formal definition of the algorithm is similar to inject, do by yourself.

18 How do we analyze this ? We want each call to inject (pop) that triggered another call to inject (pop) to release one unit of potential. ==> suffix suffix suffix ==> prefix prefix prefix

19 Fully persistent queues -- analysis
Define a suffix with 2 elements as red. Define a prefix with 0 elements as red. Otherwise the buffer is green. ==> suffix suffix suffix ==> prefix prefix prefix

20 Fully persistent queues -- analysis
Maybe  = # (red buffers) ? Red buffers still exists for other queues after the change. ==> suffix suffix suffix ==> prefix prefix prefix

21 Fully persistent queues -- analysis
Define a queue/subqueue as either rr, rg, gg Either an rr queue dies and we get two rg queues or an rg queue dies and we get two gg queues ==> suffix suffix suffix ==> prefix prefix prefix

22 Fully persistent queues -- analysis
 = 3# (rr) + #(rg) ==> suffix suffix suffix ==> prefix prefix prefix

23 Fully persistent deques
How do we generalize this to allow insertions/deletions from both sides ? Increase the size of the buffers to 0-3. Everything else is the same, when a buffer is full we may have to do a recursive push/inject, when a buffer is empty we may have to do a recursive eject/pop. Analysis: Define prefix/suffix to be red if it contains either 0 or 3 elements.

24 Worst-case bounds/purely functional implementation
Increase the size of the buffers to 0-5. Define a buffer as red if it contains 0 or 5 elements yellow if it contains 1 or 4 elements green if it contains 2 or 3 elements Let green < yellow < red and define the color of a deque as the maximum of the colors of its buffers.

25 Purely functional deques
1 2 Let green = 0 red = 2 yellow = 1 Think of the stack of queues as a redundant binary counter. prefix suffix prefix suffix prefix suffix

26 Redundant binary counters
We shall allow an additional digit. Use 0,1, and 2, but keep the base to be 2 ! = 0*20 + 1*21 + 1*22 + 2*23 + 1*24 = 38 The representation now is not unique Can represent 38 also by or We shall exploit the redundancy.

27 Exploiting the redundancy
We are not going to use all possible representations Define a representation to be regular if it does not contain the following patterns …… …... ………… When you walk from left to right after you see a 2 you should see a 0 before hitting another 2 or running out of digits

28 Redundant binary counters (cont)
Note: every number has a regular representation. Given a regular representation for x, how do you get a regular representation for x+1. 1) increment the rightmost digit. We get a representation for x+1 which may not be regular ……… ==> ……… 2) If indeed so, then replace the rightmost 2 with a 0 and increment the digit to its left. (Fix) ==> ………

29 Purely functional deques
1 2 Push/pop/inject/eject may increase the least significant digit. Then you need to do a fix. Fix: fill an empty buffer by popping from the corresponding buffer of the subqueue. Similarly empty a full buffer. prefix suffix prefix suffix prefix suffix

30 Representation 2 1 1 1 2 1 1 We need a purely functional representation of these counters.

31 Stack of stacks representation
2 1 2 1 1 1 1 1 Only a constant number of new nodes per increment.

32 Purely functional deques
2 1 1

33 Adding catenation We will do only steques (no eject)
prefix, steque of pairs, suffix pair = (prefix, steque of pairs) prefix contains 2-6 elements suffix contains 1-3 elements

34 Adding catenation (cont)

35 Adding catenation (cont)

36 Push

37 Push

38 Push Inject is similar.

39 Catenate

40 Pop

41 Pop

42 Pop

43 Pop catenate There are two more cases.
The analysis is similar to what we have seen before

44 Summary In the paper: The full set of operations.
Together with some generalization of the counters you can get a purely functional implementation with worst case performance.

45 Alternative approaches
Put the items at the leaves of a tree which is at least binary. Catenation is done via linking: left linking

46 Alternative approaches (cont.)
Alternative forms of linking right linking symmetric linking

47 Alternative approaches (cont.)
How to pop ? v flip v <==> u w Left path reversal: Do right flips bottom-up on the parent of the leftmost leaf.

48 Alternative approaches (cont.)
After the left path reversal: r r` Create a new root for the queue after the pop. Represent children lists as deques (or steques) so each flip takes constant time, and duplicating the root takes constant time

49 Alternative approaches (cont.)
Can do eject symmetrically, do push and inject via catenation Can analyze only for steques (when eject is not allowed). (Okasaki FOCS’96) The performance in general is not known ??

50 Alternative approaches (cont.)
To avoid pushing down stuff on the right side of the tree when doing a left path reversal replace the flips with the following v v <==> u w z w Performance of this is not known ??

51 Alternative approaches (cont.)
Can we avoid the extra persistent machanism to represent children lists ? w w <==> v z y u Here stacks suffice if there is no eject.

52 Alternative approaches (cont.)
Use binary trees. Link by symmetric linking. To pop do a splay on the parent of the leftmost leaf and then delete it and its parent. z y x A B C D x ==> A y B z C D


Download ppt "Persistent deques."

Similar presentations


Ads by Google