-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpairing-heap-elmasri.lisp
268 lines (240 loc) · 9.25 KB
/
pairing-heap-elmasri.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
;;;; MINHEAP is by Stephan Frank <defclass@googlemail.com>, 2007-2012.
;;;;
;;;; Permission is hereby granted, free of charge, to any person obtaining
;;;; a copy of this software and associated documentation files (the
;;;; "Software"), to deal in the Software without restriction, including
;;;; without limitation the rights to use, copy, modify, merge, publish,
;;;; distribute, sublicense, and/or sell copies of the Software, and to
;;;; permit persons to whom the Software is furnished to do so, subject to
;;;; the following conditions:
;;;;
;;;; The above copyright notice and this permission notice shall be included
;;;; in all copies or substantial portions of the Software.
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(defpackage :pairing-elmasri (:use :cl)
(:export
#:pairing-elmasri
#:clear-heap
#:empty-p
#:insert
#:peek-min
#:extract-min
#:extract-node
#:heap-size
#:decrease-key
#:meld
))
(in-package :pairing-elmasri)
;;;; Paring heap with improved meld due to delayed
;;;; reconstruction. Based on "Amr Elmasri, 'Pairing Heaps with
;;;; Costless Meld', arXiv:0903.4130v2, April 2009".
(defstruct (node (:constructor %make-node (key data)))
(key 0 :type fixnum)
(data nil)
(pooled nil :type boolean)
(lchild nil :type (or null node))
(sibling nil :type (or null node))
(parent nil :type (or null node)))
(defclass pairing-elmasri ()
((size :initform 0 :initarg :size
:type (integer 0 *))
(root :initform nil :initarg :root
:type (or null node))
(min :initform nil :initarg :min
:type (or null node))
(decreased :initform nil :initarg :decreased
:type list)
(pooled :initform 0 :initarg :pooled
:type fixnum)))
(defmethod print-object ((obj pairing-elmasri) stream)
(print-unreadable-object (obj stream :type t :identity t)
(format stream "~4I~:_size: ~A~:_" (slot-value obj 'size))))
(defun heap-size (heap)
(slot-value heap 'size))
(defun empty-p (heap)
(zerop (slot-value heap 'size)))
(defun clear-heap (heap)
(setf (slot-value heap 'size) 0
(slot-value heap 'root) nil
(slot-value heap 'min) nil
(slot-value heap 'decreased) nil)
heap)
(defun peek-min (heap)
(let ((node (slot-value heap 'min)))
(when node
(values (node-data node)
(node-key node)))))
(defun extract-min (heap)
(let ((node (slot-value heap 'min)))
(assert node)
(clean-up heap)
(when (setf (slot-value heap 'root) (combine-siblings node)
(slot-value heap 'min) (slot-value heap 'root))
(node-parent (slot-value heap 'root)) nil)
(decf (slot-value heap 'size))
(values (node-data node)
(node-key node))))
(defun insert (heap key data)
(let ((node (%make-node key data)))
(incf (slot-value heap 'size))
(setf (slot-value heap 'root)
(if (slot-value heap 'root)
(prog1 (link (slot-value heap 'root) node)
(update-min heap node key))
(setf (slot-value heap 'min) node)))
node))
(defun decrease-key (heap node key)
(cond
((< (node-key node) key)
(error "Cannot decrease key: new key greater than current key."))
(t
(update-decrease heap node (setf (node-key node) key)))))
(defun extract-node (heap node)
(cond
((eq (slot-value heap 'min) node)
(extract-min heap))
(t
(clean-up heap)
(decf (slot-value heap 'size))
(cut-parent node)
(let ((tmp (make-instance 'pairing-elmasri
:size 1
:min node
:root node)))
;(declare (dynamic-extent tmp))
(prog1 (extract-min tmp)
(when (slot-value tmp 'root)
(meld heap tmp)))))))
#+(or)
(defun extract-node (heap node)
(decrease-key heap node most-negative-fixnum)
(extract-min heap))
(defun meld (heap-a heap-b)
"Melds HEAP-A and HEAP-B into HEAP-A and returns it. HEAP-A and
HEAP-B may be modified and may have become empty after this operation."
(when (< (slot-value heap-a 'size) (slot-value heap-b 'size))
;; ensure heap-b is the smaller heap
(rotatef heap-a heap-b))
(clean-up heap-b)
(incf (slot-value heap-a 'size) (slot-value heap-b 'size))
(let ((root (setf (slot-value heap-a 'root)
(link (slot-value heap-a 'root)
(slot-value heap-b 'root)))))
(update-min heap-a
root
(node-key root)))
(clear-heap heap-b)
heap-a)
;;; internal structure maintaining functions
;(declaim (inline update-min update-decrease))
(defun update-decrease (heap node key)
(when (and (node-parent node)
(not (node-pooled node)))
(incf (slot-value heap 'pooled))
(setf (node-pooled node) t)
(push node (slot-value heap 'decreased)))
(update-min heap node key))
(defun update-min (heap node key)
(if (< key (node-key (slot-value heap 'min)))
(setf (slot-value heap 'min) node)
node))
(defun clean-up (heap)
(let ((poolsize (slot-value heap 'pooled)))
(unless (zerop poolsize)
(let ((pool (make-array poolsize :element-type 'node
:initial-element (slot-value heap 'root)))
(stepsize (ceiling (log poolsize 2))))
(declare (dynamic-extent pool))
;; step i
(loop for x in (slot-value heap 'decreased)
for i from 0
for x-lchild = (node-lchild x)
for x-parent = (node-parent x)
do (setf (node-lchild x-parent)
(delete-node-from-childseq x
(node-lchild x-parent)
(when x-lchild
(cut-parent x-lchild))))
(setf (node-pooled x) nil
(aref pool i) x))
(setf (slot-value heap 'decreased) nil
(slot-value heap 'pooled) 0)
;; step ii
(loop for i fixnum by stepsize below poolsize
for subsize = (min stepsize (- poolsize i))
do (let ((v (make-array subsize :displaced-to pool
:displaced-index-offset i)))
;;(declare (dynamic-extent v))
(sort v #'> :key #'node-key)
(loop for j below (1- subsize)
do (link (aref v j) (aref v (1+ j)))
finally (setf (slot-value heap 'root)
(link (slot-value heap 'root) (aref v j))))))))))
(defun attach-child (parent child)
(shiftf (node-sibling child) (node-lchild parent) child)
(setf (node-parent child) parent))
(defun link (node-a node-b)
(cond
;((null node-a) node-b)
;((null node-b) node-a)
((<= (node-key node-a) (node-key node-b))
(attach-child node-a node-b))
(t (attach-child node-b node-a))))
(defun combine-siblings (parent)
(pair-children parent)
(link-children parent))
(defun pair-children (parent)
(when (node-lchild parent)
(loop for c1 = (shiftf (node-lchild parent) nil) then next
for c2 = (node-sibling c1)
with next
do (if c2
(progn
(setf (node-sibling c1) nil
next (shiftf (node-sibling c2) nil))
(attach-child parent (link c1 c2)))
(attach-child parent c1))
while (and c2 next))))
(defun link-children (parent)
(let ((root (node-lchild parent)))
(when root
(loop for child = (shiftf (node-sibling root) nil)
then (prog1 (shiftf (node-sibling child) nil)
(setf root (link root child)))
while child
finally (return root)))))
(defun cut-parent (node)
(let ((parent (node-parent node)))
(when parent
(setf (node-lchild parent)
(delete-node-from-childseq node
(node-lchild parent))))
node))
(defun delete-node-from-childseq (node first-child &optional replace)
(let ((first first-child))
(if (eq node first)
(if replace
(setf (node-sibling replace) (node-sibling node)
(node-parent replace) (node-parent node)
first replace)
(setf first
(node-sibling first)))
(loop for c = first then next
for next = (node-sibling c)
until (eq node next)
finally (if replace
(setf (node-sibling c) replace
(node-parent replace) (node-parent node)
(node-sibling replace) (node-sibling next))
(setf (node-sibling c)
(node-sibling next)))))
(setf (node-parent node) nil
(node-sibling node) nil)
first))