real-time queue operations in pure lisp

5
Volume 13, number 2 INFORMATION PROCESSING LE’ITERS 13 Nwembr 1981 REALTIME QUEUE OPERATIONS IN PURE LISP * Robert HOODand Robert MELVILLE Departmenrt of Cbmpter Science, Cornell Uniwsity, Ithaca, NY 14853, U.S.A. Received 61 November 1980; revised version received 25 August 1981 Lisp, real-time queue I. Intmduction In Pure LISP, the inability to access the end of a list in constant time increases the asymptotic com- plexity of some algorithms. It is impossible, for exam- ple, to append one list to another one without recopy- ing the first. If the end of the first list could be accessed (and modified) then the append operation would take constant time rather than time propor- tional to its length. This restriction on access is of par- Ocular significance when implementing a queue. By its very nature a queue requires the ability to access both ends of a list. 2. Queue operations 2.1. The straigh tforwrd version We implement three queue operations: Query, Delete, and Insert. Query[q] returns the element at the from of queue q. Delete[q] returns q with its front element removed. Insert [q,v] returns the queue formed by adding the element v to the end of queue q. A straightforward implementation of these operations is given in Fig. 1. If the brad of the queue is kept at the front of a list, as is done in our implementation, then the Delete and Query operations can be performed in constant l This work has been supportsd in part by National Science Foundation grants MCS 7845850 and MCS 76-22360. Quwlql -r]ql Delete(q] =cdr[q] Insert[q,v) =append(q, Ust[vJ] Fig. 1. Queue operations in Pure LISP. (Note that a black- board dialect of LISP is used.) time. The Insert operation, however, requires the recopying of the queue, and therefore takes time pro- portional to its length. 2.2. An asymptotic impwemem A simple modification allows constant time access to both ends of the queue in most circumstances. A queue with value ql , . . . . Q, qi+], .. . . q, is stored as a head list (qr l l l Q) and a reversedtall list (q,, l l l qi+l) with the head list empty only if the tall list is also. Insert can now be performed ln constant time by ‘cons’ing an element onto the tall l&t. since the head list is empty only if the entire queue is empty, Query can be performed in constant time. Delete la per- formed by deleting the element ql and, if the head list is now empty, reversing the tall list and making the result the head list. Fig. 2 shows operations that manipula 9 e queues representedthis way. This representation reduces the cost of n opera- tions from 0(n2) to O(n). The reversalof a list of length k takes place only when there have been k Inserts since the last oc?versal operation. Since the reversalof the list of t~ngthk can be done in O(k) steps, then O(k) work is done to reverseonly after having done O(k) work to Insert the elements being 50 0020-0190~8 l fOOOO-0006/$02.75 @ 1981 North-Holland (

Upload: robert-hood

Post on 25-Aug-2016

220 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Real-time queue operations in pure LISP

Volume 13, number 2 INFORMATION PROCESSING LE’ITERS 13 Nwembr 1981

REALTIME QUEUE OPERATIONS IN PURE LISP *

Robert HOOD and Robert MELVILLE Departmenrt of Cbmpter Science, Cornell Uniwsity, Ithaca, NY 14853, U.S.A.

Received 61 November 1980; revised version received 25 August 1981

Lisp, real-time queue

I. Intmduction

In Pure LISP, the inability to access the end of a list in constant time increases the asymptotic com- plexity of some algorithms. It is impossible, for exam- ple, to append one list to another one without recopy- ing the first. If the end of the first list could be accessed (and modified) then the append operation would take constant time rather than time propor- tional to its length. This restriction on access is of par- Ocular significance when implementing a queue. By its very nature a queue requires the ability to access both ends of a list.

2. Queue operations

2.1. The straigh tforwrd version

We implement three queue operations: Query, Delete, and Insert. Query[q] returns the element at the from of queue q. Delete[q] returns q with its front element removed. Insert [q,v] returns the queue formed by adding the element v to the end of queue q. A straightforward implementation of these operations is given in Fig. 1.

If the brad of the queue is kept at the front of a list, as is done in our implementation, then the Delete and Query operations can be performed in constant

l This work has been supportsd in part by National Science Foundation grants MCS 7845850 and MCS 76-22360.

Quwlql -r]ql Delete(q] =cdr[q] Insert[q,v) =append(q, Ust[vJ]

Fig. 1. Queue operations in Pure LISP. (Note that a black- board dialect of LISP is used.)

time. The Insert operation, however, requires the recopying of the queue, and therefore takes time pro- portional to its length.

2.2. An asymptotic impwemem

A simple modification allows constant time access to both ends of the queue in most circumstances. A queue with value ql , . . . . Q, qi+], . . . . q, is stored as a head list (qr l l l Q) and a reversed tall list (q,, l l l qi+l)

with the head list empty only if the tall list is also. Insert can now be performed ln constant time by

‘cons’ing an element onto the tall l&t. since the head list is empty only if the entire queue is empty, Query can be performed in constant time. Delete la per- formed by deleting the element ql and, if the head list is now empty, reversing the tall list and making the result the head list. Fig. 2 shows operations that manipula 9 e queues represented this way.

This representation reduces the cost of n opera- tions from 0(n2) to O(n). The reversal of a list of length k takes place only when there have been k Inserts since the last oc?versal operation. Since the reversal of the list of t~ngth k can be done in O(k) steps, then O(k) work is done to reverse only after having done O(k) work to Insert the elements being

50 0020-0190~8 l fOOOO-0006/$02.75 @ 1981 North-Holland (

Page 2: Real-time queue operations in pure LISP

Volume 13, number 2 INFORMATION PROCRSSiNG LETTERS 13 November 1981

QuauiqlfguCCWql 1 - return CBT of H b=ttq. vl=com[car]sl, com(v, cd&i] ] 1 - twk v onto T Retete[q taa

if nua[ak]aufq] ] ] - one left in H utm

cons[Rmm[cdr(q]],NiL] -replaceHby~ . e4m

cens]c&]cu]q] J, cttr]q] ] - dekta first &meat n

Fi$. 2.3etter queue operations.

twmed. Since the other operations require only con- stant tima, n queue operations can be done in O(n) time,

By distributing the Reverse of the tail 1st over a number of operations, the queue operations can be performed in real time ’ . The basis idea of performing one step of the Reverse during every operation is easy to implement, if the list being reversed is not chang- ing. For example, if L is of length n, then

Incr_rW(tncr_rsv( I+-

-..~~_~~fconr(L,NILI]...]1 n time.3

will return with @IL&, where c is the reverse of L, if incr__rev is defined as follows:

incr_lW( X]=ccow] ~]WXll.

Note that any one call to incr_rev is performed in constant time.

Rather than waitfng for the head list H to empty, we periodically R8verse the tait list T and append it to the head list, for&g a new head list H’. This is a three-etep process:

(1) Reverse T to form the tail. end of H’,

r A red-time queue im@mentation performs any sequence of Imrtt, Dalrstss, and Qmys wit.41 only a conrtaat amount of prac%as@ between OpenrtZons; ‘note that this is stronger than &ear timek In the case of LISP, we assume the exis- ttmce uf red-the car, cdr, and cow primitives [ 11.

(2) Reverse H to form Hn, (3) Reverse HR onto the front of H’.

i%Ch Of til8Se Operations iS just the reversing of a list and can be done using a function like &x-rev. ‘fhe first two steps are independent and can be done in JXIra.kk Thus, ifat the start of the: reversal process wehave:

front rear

PO “‘9m qm+E “‘% -..(H}-.. .----{‘F)---

(where T indicates the Reverse of the list T ‘) then halfway through the above process we have:

front Icar

90 “*qm --{ri-,,.-

qm+l”‘% - _____(H’)___

Since qm is at the front of Hn and qm+l is at the front of H’ then cons(car(HR), H) will produce

kqmi’ l l - qn). After m + 1 cons operations we

. .

front rear

90 “‘qm qm+l”‘qn ___-._ ___{H’)_________

This is the basic idea behind implementing the n&time operations. One minor hitch remains, how- ever: while performing the incremental reverses the queue will not remain constant. The representation needs to reflect the current state of the queue as well as the current state of the recopying operation.

Taking care of the Inserts that occur while recopy- ing is simple. A new tail list T’ is used and the new elements ‘cons’ed onto it instead of the old tail T. In order to be able to satisfy Query and Delete requests, there must be two copies of the head list. One head list (h) represents the front end of the queue. Deletes . will be performed by replacing h by cd(h), and Query’s performed by returning car(h). The other head list(H) is used in the reversal process to form HR. Since there may have been Deletes, not all of I& should be reversed onto H’. A count, #copy, of the length of h is kept tc indicate how much of Hn needs to be copied.

2 Note &at the list H can have its leftmost elements accessed wh& a reversed iist such as 5: has its rightmosf elements ac~ssible.

Page 3: Real-time queue operations in pure LISP

Volume 13, number 2 INFORMATION PROCESSING LETFERS 13 Novem~ 11981

Along with the recopy process we need a strategy IS indicate when recopying shouid be done. We choose to start recopying when the length the tail list T is greater than the length of the head list H, It u;Zl be necessary to show that the recopying opera- tion leaves us with I H’l 2 IT’/. When the queue is not in ‘recopy mode’, it will behave like the queue of section 2.2.

Using the above strategy the recopy operation will start when the tail list has length n + 1 and the head list has leng& n. Simultaneously reversing H and T in the first pass of the recopying will take n + 1 incremental steps to perform. Reversing HR in the second pass will take n steps to perform. Thus there are a total of 2n + 1 steps in the recopying process. The implementation will get into trouble if the head Iist h is emptied before the recopying is completed.

Since h is n elements long, the recopy process muat perform 2n + 1 incremental steps in at moat n queue operations. This wil8 be aooomplished by pezfomdng the fust two steps of the mcopy prolobss in the opcm- tion that causes T to be longer than H, aad two more steps of the process during every subsequent queue operation. This gives us 2 X (at 1) incremental stops before the head Iist will be emptied. This is suffldent to ensure that the recopy process will be completed in time.

2.3.2. The

l l . Qn-1 Qn ’ l * qan -_-{H}-.-- _*__(~)_*

___“__“““__S”_“_“” Q- .__““____“__“__“” SW..__ I[~~}“___ ___+ “““ffr’) _““_ _ I”“” {pt) “““__”

I- 2 x#Qps 4 I+2 xkps-4

-Idelctesl- ---------------------------------- Ithe queue1 --_l_--c-----*-“_--“_------------

--_-- ----- --1------ {t&l ---------_---------

(a) During the first pass of recopying

q-qq-1 9d, 0 ‘qn-l On 92a ____________________*____ Q _“__..________I__________ ________ fll’)________

-ideLetes)- -----------------“*-____________I_ (the queue 1 -*-------------------------------

-------------------(h)-------------------- HaT=()

(b) After the first pass of recopying

q~qdl*d2-1 ‘dl+d2 qi qr-qn-l qu 92a qzrr+1 ‘q2a+q+a2 _____ “_.._____ _____ Q____ .I______ “______ __I __c_____ “__(N’) __________ “____ ____ _ _______ (qq _____I____._

I+ #copy +I ---------{de~etesj--------- ------------_------------.----- (the queue) ~-~-~.11~“~~~*~~~~.-~~~~~~--~.~~~

~-~~_~~~~~~~fh}~~~~~~~---

(cl During the second pass of recopying

HsT = 0

Li- D qdl+d*-l qdl’dl -qtl-1 qn qzrr

---__- _-____[ii,]_________-_ --“_-------------..*-- IH’I ----------.----------

9a*l 92a+qw2

_______ “____tf’f____“_____“_ ---------~deletes)*-_-______ ___1____L___L__~_-.__------“---.-” i the queue 1 ----------------_-_--------------

____““____^ ‘{hi ____“__ “__

8 = T = (1

(d) lifter recopying is finished I+ 3 The queue at various stages of the recopy iirocess.

52

Page 4: Real-time queue operations in pure LISP

volulne 13,ambt.r 2 INFORMATION PROCESSING LETTERS 13 November 1981

After aa additions to the queue and dt deletions (where aa + dt < n/2) the queue will be in the state

ts are removed from

Whenal+dlPn12theqwue~havefWshed . the fht of the recopy operation and will be as pictured in Fig 3b. After aa + da more additions and deletions, the queue is in the midst of the second pass of recopy@ and is in a state as shown in Fig. 3c. Stored in #copy is a count of the number of elements of HR that have not been deleted from the queue. HR is be@ reversed onto H’ until #copy = 0. At that time H’ and T’ can replace H and T as the representa- tion of the queue, as shown in Fig. 3d. Since the new head list is of length 2n + 1 - dl - dl and the new t~Ilstisoflengthalta2anddl+d2+al+a?=n then:

2n+l>n, 2n+ 1 >a, +aa+dl +dg ,

2n+l-dl-d2>a+az.

Thus, 1 H’I > 1 T’I which we needed to show to prove that our recopying strategy was a valid one.

3. conchlsion

The linear-time algorithm of section 2.2 is similar to a Turing Machine construction due to Stoss [S]. Since a one-tape TM can be simulated in real time in Pure LISP, the work of Leong and Seiferas [4] im- plies the existence of a real-time queue implementa- tion. However, their method, which is much more general, leads to a queue implementation that is sig- nificantly more complicated than the one presented here.

It is believed that all linear time LISP functions can be done incrementally; this could lead to many real-time LISP algorithms. It would be interesting to exhibit a problem for which the lower bound in Pure LISP is worse than some implementation using rplaca and r&cd.

References

I13

VI

(31

I41

PI

H. Baker, List processing in real time on a serial com- puter, Comm ACM 21 (4) (1978) 280-294. E. Dijkstra, A Discipline of Programming (Prentice-Hall, Englewood Cliffs, NJ, 1976). D. Knuth, The Art of Computer Programming, Vol. I (Addison-Wesley, Reading, MA, 1973). B. Leong and J. Seiferas, New real-time simulations of multi-head tape units, Proc. Ninth Annual Symposium on Theory of Computing, Boulder (1977) 239-248. H.-J. Stoss, K-band simulation von k-Kopf-Turing-Ma- schinen, Computing 6 (1970) 309-317.

53

Page 5: Real-time queue operations in pure LISP

Vohime 13, number 2 INFORMATION PROCFSSING LETTERS 13 Naranber 1981

Appendix - The reaMme queue impkmemtation

A queue is a ninealemnt list: list [recopy, lendiff, #copy, H, T, h, H’, T’, H&i, where we have given :symbolic names to the components. For example, car [cdr [q] ] is lendiff of queue q* An empty queue has ,value: list [fake, 0, 0, ML, ML, NIL, NIL, NIL, ?;;F: i].

insert/q, vj=

In the second alternative of Onestep it is known that cdr[T] is NIL sink T was exactly one larger than H when recopying started. Note that the only arithmetic operations performed are: add 1, subtract

1, and test for 0 or 1. They can be performed by appropriate list functions if we encode integers in unary noti3tion. (The integer n is a ltst of n atoms.)

ii’ 1 recopy A lendiff > 0 -L llst[F’alse, lendiff-1, 0, H, 0, H, cons&, T], H, NIL, NIL, NIL]]

recopy -, Onestep[Onestep[True, Iendiff-1, #copy, H, T, h, H’, cons[v. T], H~jj fll

Deleie[q J- it 7 recopy A lendlff > 0 + list[False, lendiff-l,O, cdr[HI, T, NIL, NIL, NIL, NIL]

1 recopy A lendiff = 0 -+ Onestep[Onestep[True, 0, 0, cdr[H], T, cdr[Hj, NIL, NIL, NlLjf recopy -* Oncstep[Onestep[True, lendiff-1, #copy-l, H, T, cdrjhj, H’, T’, HR)]

fi

Queiy[qP if -I recopy --a car[H]

recopy * car[hj fI

chlestep[q 1” if 7 recopy*q

recopy A 1nuii[HJ A lnuii(T1 4

list[True, lendiff+l, #copy+l, cdr[Hj, cdr[T), h, cons[car[T], H’], T’, cons[car[H], HR)] recopy A nuii[H] A 1 nuii[T] +

list[True, lendiff+l, #copy, NIL, NIL, h, cons[car[TJ, H’j, T’, HRJ recopy A nuii[H] A nuii[TJ A #copy > I-+

iist[True. lendiff+i, #copy-i, NIL, NIL, h, cons[car[HR], H’f, T’, cdr[HR] J recopy A nuii[H] A nuli[T] A #copy = 1 -c

iist[Fak lendiff+l, 0, cons[car[HRj, H’]* T’, NIL, NIL, NIL, NIL] recopy A nuki[H] A nuiI[T] A #copy = 04

list[False. lendiff, 0, H’, T’, NIL, NIL, NIL, NIL] fi

54