-
Notifications
You must be signed in to change notification settings - Fork 65
/
priority-que.cl
1465 lines (1318 loc) · 46.3 KB
/
priority-que.cl
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;This package was written by Abe_s originally named 'netviz-lib.cl'.
;;;(eval-when (:load-toplevel :compile-toplevel :execute)
;;; (setq *locale* (find-locale "japan.932")))
(defpackage :priority-que
(:use :cl :excl)
(:export #:make-prique
#:prique-empty-p
#:prique-box-item
#:insert-prique
#:find-min-prique
#:delete-min-prique
#:union-prique
#:after-decrease-key-prique
))
(in-package :priority-que)
;; (declaim (optimize (speed 3) (safety 0) (debug 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Some constants
(defconstant portable-most-positive-fixnum (1- (expt 2 29)))
(defconstant portable-most-negative-fixnum (- (expt 2 29)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; QUE - FIFO queue
(defstruct (que (:constructor _make-que))
list
last)
(defmethod print-object ((que que) stream)
(format stream "#<que ~s>" (cdr (que-list que))))
(defun make-que ()
(let ((que (_make-que))
(root (list nil)))
(setf (que-list que) root
(que-last que) root)
que))
(defun append-que (que x)
(setf (que-last que)
(setf (cdr (que-last que))
(list x)))
x)
(defun append-que-list (que x)
(dolist (e x)
(append-que que e))
x)
(defun insert-que (que x)
(setf (car (que-list que)) x)
(setf (que-list que) (cons nil (que-list que)))
x)
(defun pop-que (que)
(if (cdr (que-list que))
(prog1
(cadr (que-list que))
(setf (cdr (que-list que))
(cddr (que-list que)))
;; !!!!!
(unless (cdr (que-list que))
(setf (que-last que) (que-list que))))
(error "Empty que")))
(defun insert-que-before-last (que x)
(assert (not (eq (que-list que) (que-last que))))
(let ((last (que-last que))
(last1 (que-list que)))
(while (not (eq (cdr last1) last))
(setq last1 (cdr last1)))
(setf (cdr last1) (cons x last))
x))
(defun insert-que-list-before-last (que x)
(dolist (e x)
(insert-que-before-last que e))
x)
(defun que-to-list (que)
(cdr (que-list que)))
(defun list-to-que (list)
(let ((que (make-que)))
(append-que-list que list)
que))
(defun que-empty-p (que)
(null (que-to-list que)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; DQUE - Doubly linked list
(defstruct dque
next
prev
;;
item)
(defconstant dque-item-header-tag '#'dque-item-header-tag)
(defmethod print-object ((dque dque) stream)
(cond
((dque-header-p dque)
(format stream "#<dque-header>")
#+ignore
(format stream "#<dque-header ~s>" (dque-to-list dque)))
(t
(format stream "#<dque-item>")
#+ignore
(format stream "#<dque-item ~s>" (dque-item dque)))))
(defun make-dque-header (&optional (d (make-dque)))
(link2-dque d d)
(setf (dque-item d) dque-item-header-tag)
d)
(defun dque-header-p (x)
(and (dque-p x)
(eq (dque-item x) dque-item-header-tag)))
(defun dque-empty-p (dqh)
(assert (dque-header-p dqh))
(eq (dque-next dqh) dqh))
(defun first-dque (dqh)
(assert (dque-header-p dqh))
(dque-next dqh))
(defun last-dque (dqh)
(assert (dque-header-p dqh))
(dque-prev dqh))
(defun link2-dque (d1 d2)
(setf (dque-next d1) d2
(dque-prev d2) d1)
nil)
(defun link3-dque (d1 d2 d3)
(setf (dque-next d1) d2
(dque-next d2) d3
(dque-prev d3) d2
(dque-prev d2) d1)
nil)
(defun append-dque (d1 d2)
#+ignore (assert (and (null (dque-prev d2)) (null (dque-next d2))))
(link3-dque d1 d2 (dque-next d1))
d2)
(defun insert-dque (d1 d2)
#+ignore (assert (and (null (dque-prev d2)) (null (dque-next d2))))
(link3-dque (dque-prev d1) d2 d1)
d2)
(defun delete-dque (d)
#+ignore (assert (and (dque-prev d) (dque-next d)))
(link2-dque (dque-prev d) (dque-next d))
#+ignore (setf (dque-prev d) nil (dque-next d) nil)
d)
(defun merge-dque (dqh1 dqh2)
"Example:
-> (setq dqh1 (list-to-dque '(a b c)))
-> (setq dqh2 (list-to-dque '(d e f)))
-> (merge-dque dqh1 dqh2)
-> (dque-to-list dqh1)
(a b c d e f)
-> (dque-empty-p dqh2)
t"
(assert (dque-header-p dqh1))
(assert (dque-header-p dqh2))
(link2-dque (last-dque dqh1) (first-dque dqh2))
(link2-dque (last-dque dqh2) dqh1)
(make-dque-header dqh2)
dqh1)
(defmacro do-dque ((var dqh) &rest body)
`(let ((,var ,dqh))
(assert (dque-header-p ,dqh))
(while (not (eq (setq ,var (dque-next ,var))
,dqh))
,@body)))
(defmacro do-dque-reverse ((var dqh) &rest body)
`(let ((,var ,dqh))
(assert (dque-header-p ,dqh))
(while (not (eq (setq ,var (dque-prev ,var))
,dqh))
,@body)))
(defun list-to-dque (list)
(let ((dqh (make-dque-header)))
(dolist (x list)
(insert-dque dqh (make-dque :item x)))
dqh))
(defun dque-to-list (dqh)
(assert (dque-header-p dqh))
(let ((ret nil))
(do-dque (d dqh)
(push (dque-item d) ret))
;;
(nreverse ret)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; LHEAP - List implementation of priority queue
(defstruct (lheap
(:constructor _make-lheap))
(head nil)
(tail nil)
(lessp #'<)
(key #'identity))
(defstruct lheap-box
item
keyval)
(defmethod print-object ((lheap lheap) sm)
(format sm "#<lheap ~s>" (mapcar #'lheap-box-keyval (lheap-head lheap)))
(assert (or (and (lheap-head lheap)
(eq (lheap-tail lheap)
(last (lheap-head lheap))))
(and (null (lheap-head lheap))
(null (lheap-tail lheap))))))
(defun make-lheap (&key (lessp #'<) (key #'identity))
(_make-lheap :lessp lessp :key key))
(defun lheap-empty-p (lh)
(null (lheap-head lh)))
(defun make-lheap-empty (lh)
(setf (lheap-head lh) nil
(lheap-tail lh) nil))
(defun insert-lheap (lh item)
;; Insert ITEM into linear heap LH where
;; the ITEM is boxed with a box object of type lheap-box
;; and returns the box.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (lheap-p lh)
(error "insert-lheap: ~s is not lheap-p" lh))
(let* ((key (lheap-key lh))
(keyval (funcall key item))
(ib (make-lheap-box :item item :keyval keyval))
(place (cons ib nil)))
(cond
((lheap-tail lh)
(setf (cdr (lheap-tail lh)) place
(lheap-tail lh) place))
(t
(setf (lheap-head lh) place
(lheap-tail lh) place)))
;;
ib))
(defun find-min-lheap (lh)
;; Returns an item of minimum key in linear heap LH.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (lheap-p lh)
(error "find-min-lheap: ~s is not lheap-p" lh))
(when (lheap-empty-p lh)
(error "find-min-lheap: ~s is empty" lh))
(let ((lessp (lheap-lessp lh))
(minib (car (lheap-head lh))))
(dolist (ib (cdr (lheap-head lh)))
(when (funcall lessp (lheap-box-keyval ib) (lheap-box-keyval minib))
(setq minib ib)))
;;
(lheap-box-item minib)))
(defun delete-min-lheap (lh)
;; Removes an item of minimum key in linear heap LH
;; and returns the item.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (lheap-p lh)
(error "find-min-lheap: ~s is not lheap-p" lh))
(when (lheap-empty-p lh)
(error "find-min-lheap: ~s is empty" lh))
(let* ((lessp (lheap-lessp lh))
(head (lheap-head lh))
(minplace head)
(minib (car head))
(minprev nil)
(prev head))
(loop
for place on (cdr head)
for ib = (car place)
do
(when (funcall lessp (lheap-box-keyval ib) (lheap-box-keyval minib))
(setq minplace place
minib ib
minprev prev))
(setq prev place))
;;
(when (eq (lheap-tail lh) minplace)
(setf (lheap-tail lh) minprev))
;;
(cond
(minprev
(setf (cdr minprev) (cddr minprev)))
(t
(pop (lheap-head lh))))
;;
(lheap-box-item minib)))
(defun union-lheap (lh1 lh2)
;; Make the union of two linear heaps LH1 and LH2.
;; The resulting heap is stored destructively into LH1 and it is returned.
;; LH2 is also destroyed and is made empty.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (lheap-p lh1)
(error "union-lheap: ~s is not lheap-p" lh1))
(unless (lheap-p lh2)
(error "union-lheap: ~s is not lheap-p" lh2))
(unless (eq (lheap-lessp lh1) (lheap-lessp lh2))
(error "union-lheap: given heaps have different lessp: ~s, ~s"
(lheap-lessp lh1) (lheap-lessp lh2)))
(unless (eq (lheap-key lh1) (lheap-key lh2))
(error "union-lheap: given heaps have different key: ~s, ~s"
(lheap-key lh1) (lheap-key lh2)))
;;
(cond
((lheap-empty-p lh1)
(setf (lheap-head lh1) (lheap-head lh2)
(lheap-tail lh1) (lheap-tail lh2)))
((lheap-empty-p lh2))
(t
(setf (cdr (lheap-tail lh1)) (lheap-head lh2)
(lheap-tail lh1) (lheap-tail lh2))))
;;
(make-lheap-empty lh2)
;;
lh1)
(defun after-decrease-key-lheap (lh ib)
;; After decreasing key of an item in binary heap LH,
;; this function rebalances LH.
;; The second argument must be the box IB of the item instead of item
;; itself. The box is an object of type lheap-box returned
;; from the function insert-lheap when the item was inserted into LH.
;; Returns IB.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (lheap-p lh)
(error "after-decrease-key-lheap: ~s is not lheap-p" lh))
(unless (lheap-box-p ib)
(error "after-decrease-key-lheap: ~s is not lheap-box-p" ib))
;;
(let* ((lessp (lheap-lessp lh))
(key (lheap-key lh))
(newkey (funcall key (lheap-box-item ib))))
(when (funcall lessp (lheap-box-keyval ib) newkey)
(error "after-decrease-key-lheap: key of ~s has been increased"
(lheap-box-item ib)))
(setf (lheap-box-keyval ib) newkey)
;;
ib))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; BIHEAP - Binary Heap implementation of priority queue
(defstruct (biheap
(:constructor _make-biheap))
;; 0
;; 1 2
;; 3 4 5 6
(array nil :type (simple-array t (*))) ; of biheap-box
(count 0 :type fixnum)
(lessp #'<)
(key #'identity))
(defstruct biheap-box
item
(node 0 :type fixnum)
keyval)
(defmethod print-object ((biheap biheap) sm)
(format-biheap sm biheap))
(defmacro biheap-parent (id) `(the fixnum (floor (the fixnum (1- ,id)) 2)))
(defmacro biheap-left (id) `(the fixnum (1+ (the fixnum (* ,id 2)))))
(defmacro biheap-right (id) `(the fixnum (* (the fixnum (1+ ,id)) 2)))
(defun format-biheap (sm bh)
(unless (biheap-p bh)
(error "format-biheap: ~s is not biheap-p" bh))
(let ((array (biheap-array bh))
(count (biheap-count bh))
(lessp (biheap-lessp bh))
(key (biheap-key bh)))
(flet ((childfun (id)
(append (and (< (biheap-left id) count)
(list (biheap-left id)))
(and (< (biheap-right id) count)
(list (biheap-right id)))))
(labelfun (id)
(list
(format nil "~s"
(funcall key (biheap-box-item (aref array id))))
#+ignore
(format nil "~s ~s ~d"
(funcall key (biheap-box-item
(aref array id)))
(biheap-box-item (aref array id))
(biheap-box-node (aref array id)))
"")))
(format sm "BinaryHeap lessp=~s count=~d~%" lessp count)
(unless (biheap-empty-p bh)
(terpri sm)
(format-tree sm 0 #'childfun #'labelfun)))))
(defun make-biheap (maxcount &key (lessp #'<) (key #'identity))
(unless (typep maxcount '(integer 0 *))
(error "make-biheap: maxcount=~s must be a non-negative integer" maxcount))
(_make-biheap :array (make-array maxcount)
:count 0
:lessp lessp
:key key))
(defun biheap-empty-p (bh)
(declare (optimize (speed 3) (safety 0) (debug 0)))
(zerop (biheap-count bh)))
(defun make-biheap-empty (bh)
(setf (biheap-count bh) 0)
bh)
(defun union-biheap (bh1 bh2)
;; Make the union of two binary heaps BH1 and BH2.
;; The resulting heap is stored destructively into BH1 and it is returned.
;; BH2 is also destroyed and is made empty.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (biheap-p bh1)
(error "union-biheap: ~s is not biheap-p" bh1))
(unless (biheap-p bh2)
(error "union-biheap: ~s is not biheap-p" bh2))
(unless (eq (biheap-lessp bh1) (biheap-lessp bh2))
(error "union-biheap: given heaps have different lessp: ~s, ~s"
(biheap-lessp bh1) (biheap-lessp bh2)))
(unless (eq (biheap-key bh1) (biheap-key bh2))
(error "union-biheap: given heaps have different key: ~s, ~s"
(biheap-key bh1) (biheap-key bh2)))
;;
(let ((array (biheap-array bh2)))
(dotimes (i (biheap-count bh2))
(insert-biheap bh1 (biheap-box-item (aref array i))))
(make-biheap-empty bh2)
;;
bh1))
(defun insert-biheap (bh item)
;; Insert ITEM into binary heap BH where
;; the ITEM is boxed with a box object of type biheap-box
;; and returns the box.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (biheap-p bh)
(error "insert-biheap: ~s is not biheap-p" bh))
(let* ((array (biheap-array bh))
(id (biheap-count bh))
(lessp (biheap-lessp bh))
(key (biheap-key bh))
(keyval (funcall key item))
(ib (make-biheap-box :item item :node id :keyval keyval)))
(declare (type fixnum id))
(unless (< (biheap-count bh) (length array))
(error "insert-biheap: cannot insert more than maxcount=~d items"
(length array)))
(incf (biheap-count bh))
(while (and (<= 1 id)
(funcall lessp
keyval
(biheap-box-keyval
(aref array (biheap-parent id)))))
(setf (aref array id) (aref array (biheap-parent id))
(biheap-box-node (aref array id)) id
id (biheap-parent id)))
(setf (aref array id) ib
(biheap-box-node (aref array id)) id)
;;
ib))
(defun find-min-biheap (bh)
;; Returns an item of minimum key in binary heap BH.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (biheap-p bh)
(error "find-min-biheap: ~s is not biheap-p" bh))
(when (biheap-empty-p bh)
(error "find-min-biheap: ~s is empty" bh))
;;
(biheap-box-item (aref (biheap-array bh) 0)))
(defun delete-min-biheap (bh)
;; Removes an item of minimum key in binary heap BH
;; and returns the item.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (biheap-p bh)
(error "delete-min-biheap: ~s is not biheap-p" bh))
(when (biheap-empty-p bh)
(error "delete-min-biheap: ~s is empty" bh))
;;
(labels ((heapify-biheap (bh id)
(declare (type fixnum id))
(let* ((array (biheap-array bh))
(count (biheap-count bh))
(lessp (biheap-lessp bh))
(lid (biheap-left id))
(rid (biheap-right id))
(sid (if (and (< lid count)
(funcall
lessp
(biheap-box-keyval (aref array lid))
(biheap-box-keyval (aref array id))))
lid
id)))
(when (and (< rid count)
(funcall lessp
(biheap-box-keyval (aref array rid))
(biheap-box-keyval (aref array sid))))
(setq sid rid))
(unless (= id sid)
(let ((idbox (aref array id))
(sidbox (aref array sid)))
(setf (aref array id) sidbox
(biheap-box-node sidbox) id)
(setf (aref array sid) idbox
(biheap-box-node idbox) sid))
(heapify-biheap bh sid)))))
;;
(let* ((array (biheap-array bh))
(minitem (biheap-box-item (aref array 0))))
(decf (biheap-count bh))
(setf (aref array 0) (aref array (biheap-count bh)))
(setf (biheap-box-node (aref array 0)) 0)
(heapify-biheap bh 0)
;;
minitem)))
(defun after-decrease-key-biheap (bh ib)
;; After decreasing key of an item in binary heap BH,
;; this function rebalances BH.
;; The second argument must be the box IB of the item instead of item
;; itself. The box is an object of type biheap-box returned
;; from the function insert-biheap when the item was inserted into BH.
;; Returns IB.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (biheap-p bh)
(error "after-decrease-key-biheap: ~s is not biheap-p" bh))
(unless (biheap-box-p ib)
(error "after-decrease-key-biheap: ~s is not biheap-box-p" ib))
;;
(let* ((array (biheap-array bh))
(lessp (biheap-lessp bh))
(key (biheap-key bh))
(id1 (biheap-box-node ib))
(id2 (biheap-parent id1))
(newkey (funcall key (biheap-box-item ib))))
(declare (type fixnum id1 id2))
(when (funcall lessp
(biheap-box-keyval (aref array id1))
newkey)
(error "after-decrease-key-biheap: key ~s of item ~s has been increased"
newkey (biheap-box-item (aref array id1))))
(setf (biheap-box-keyval (aref array id1)) newkey)
(while (and (<= 0 id2)
(funcall
lessp
(funcall key (biheap-box-item (aref array id1)))
(funcall key (biheap-box-item (aref array id2)))))
;; Swap boxes of elements at id1 and id2 in array.
(let ((ib1 (aref array id1))
(ib2 (aref array id2)))
(setf (aref array id1) ib2
(biheap-box-node ib2) id1)
(setf (aref array id2) ib1
(biheap-box-node ib1) id2))
;; Up
(setq id1 id2
id2 (biheap-parent id2)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; BNHEAP - Binomial Heap implementation of priority queue
(defstruct (bnheap
(:constructor _make-bnheap))
(head nil)
(lessp #'<)
(key #'identity))
(defstruct bnheap-node
(box nil)
(parent nil)
(degree 0 :type fixnum)
(child nil)
(sibling nil))
(defstruct bnheap-box
item
node
keyval)
(defmacro bnheap-node-keyval (bn)
`(bnheap-box-keyval (bnheap-node-box ,bn)))
(defmethod print-object ((bnheap bnheap) sm)
(format-bnheap sm bnheap))
(defmethod print-object ((bnheap-node bnheap-node) sm)
(format sm "#<bnheap-node degree=~d>"
(bnheap-node-degree bnheap-node)))
(defun bnheap-degrees (bh)
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh)
(error "bnheap-degrees: ~s is not bnheap-p" bh))
(do ((bn (bnheap-head bh) (bnheap-node-sibling bn))
(degs nil))
((null bn) (nreverse degs))
(push (bnheap-node-degree bn) degs)))
(defun bnheap-count (bh)
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh)
(error "bnheap-count: ~s is not bnheap-p" bh))
(apply #'+ (mapcar #'(lambda (k) (expt 2 k))
(bnheap-degrees bh))))
(defun format-bnheap (sm bh)
(unless (bnheap-p bh)
(error "format-bnheap: ~s is not bnheap-p" bh))
(let ((key (bnheap-key bh)))
(format sm "BinomialHeap lessp=~s count=~d(=~{2^~d~^+~})~%"
(bnheap-lessp bh) (bnheap-count bh) (bnheap-degrees bh))
(unless (bnheap-empty-p bh)
(terpri sm)
(do ((bn (bnheap-head bh) (bnheap-node-sibling bn)))
((null bn) (values))
(format-tree
sm
bn
#'(lambda (bn)
(do ((s (bnheap-node-child bn) (bnheap-node-sibling s))
(child nil))
((null s) (nreverse child))
(push s child)))
#'(lambda (bn)
(list (format nil "~s"
(funcall key
(bnheap-box-item
(bnheap-node-box bn))))
"")))))))
(defun make-bnheap (&key (lessp #'<) (key #'identity))
(_make-bnheap :lessp lessp :key key))
(defun bnheap-empty-p (bh)
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh)
(error "bnheap-empty-p: ~s is not bnheap-p" bh))
(null (bnheap-head bh)))
(defun make-bnheap-empty (bh)
(setf (bnheap-head bh) nil)
bh)
(defun union-bnheap (bh1 bh2)
;; Make the union of two binomial heaps BH1 and BH2.
;; The resulting heap is stored destructively into BH1 and it is returned.
;; BH2 is also destroyed and is made empty.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh1)
(error "union-bnheap: ~s is not bnheap-p" bh1))
(unless (bnheap-p bh2)
(error "union-bnheap: ~s is not bnheap-p" bh2))
(unless (eq (bnheap-lessp bh1) (bnheap-lessp bh2))
(error "union-bnheap: given heaps have different lessp: ~s, ~s"
(bnheap-lessp bh1) (bnheap-lessp bh2)))
(unless (eq (bnheap-key bh1) (bnheap-key bh2))
(error "union-bnheap: given heaps have different key: ~s, ~s"
(bnheap-key bh1) (bnheap-key bh2)))
;;
(let* ((lessp (bnheap-lessp bh1))
(key (bnheap-key bh1))
(bh (make-bnheap :lessp lessp :key key)))
(macrolet ((link-bnheap-node (y z)
`(setf (bnheap-node-parent ,y) ,z
(bnheap-node-sibling ,y) (bnheap-node-child ,z)
(bnheap-node-child ,z) ,y
(bnheap-node-degree ,z) (1+ (bnheap-node-degree ,z)))))
(flet ((merge-bnheap (bh1 bh2)
(let* ((bn1 (bnheap-head bh1))
(bn2 (bnheap-head bh2))
(head (make-bnheap-node))
(tail head))
(while (or bn1 bn2)
(cond
((and bn1 bn2)
(let ((bn (cond
((<= (bnheap-node-degree bn1)
(bnheap-node-degree bn2))
(prog1
bn1
(setq bn1 (bnheap-node-sibling bn1))))
(t
(prog1
bn2
(setq bn2 (bnheap-node-sibling bn2)))))))
(setf (bnheap-node-sibling bn) nil
(bnheap-node-sibling tail) bn
tail bn)))
(bn1
(setf (bnheap-node-sibling tail) bn1
bn1 nil))
(bn2
(setf (bnheap-node-sibling tail) bn2
bn2 nil))))
;;
(bnheap-node-sibling head))))
;;
(setf (bnheap-head bh) (merge-bnheap bh1 bh2))
(when (null (bnheap-head bh))
(return-from union-bnheap bh))
;;
(let* ((prev-x nil)
(x (bnheap-head bh))
(next-x (bnheap-node-sibling x)))
(while next-x
(cond
((or (/= (bnheap-node-degree x)
(bnheap-node-degree next-x))
(and (bnheap-node-sibling next-x)
(eq (bnheap-node-degree (bnheap-node-sibling next-x))
(bnheap-node-degree x))))
(setq prev-x x
x next-x))
((not (funcall lessp
(bnheap-node-keyval next-x)
(bnheap-node-keyval x)))
(setf (bnheap-node-sibling x) (bnheap-node-sibling next-x))
(link-bnheap-node next-x x))
(t
(cond
((null prev-x)
(setf (bnheap-head bh) next-x))
(t
(setf (bnheap-node-sibling prev-x) next-x)))
(link-bnheap-node x next-x)
(setq x next-x)))
(setq next-x (bnheap-node-sibling x))))
;;
(setf (bnheap-head bh1) (bnheap-head bh)
(bnheap-lessp bh1) (bnheap-lessp bh)
(bnheap-key bh1) (bnheap-key bh))
;;
(make-bnheap-empty bh2)
;;
bh1))))
(defun insert-bnheap (bh item)
;; Insert ITEM into binomial heap BH where
;; the ITEM is boxed with a box object of type bnheap-box
;; and returns the box.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh)
(error "insert-bnheap: ~s is not bnheap-p" bh))
(let* ((key (bnheap-key bh))
(ib (make-bnheap-box :item item :keyval (funcall key item)))
(bn (make-bnheap-node :box ib)))
(setf (bnheap-box-node ib) bn)
(union-bnheap bh (_make-bnheap :head bn
:lessp (bnheap-lessp bh)
:key (bnheap-key bh)))
;;
ib))
(defun find-min-bnheap (bh)
;; Returns an item of minimum key in binomial heap BH.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh)
(error "find-min-bnheap: ~s is not bnheap-p" bh))
(when (bnheap-empty-p bh)
(error "find-min-bnheap: ~s is empty" bh))
(let ((lessp (bnheap-lessp bh))
(minnode (bnheap-head bh)))
(do ((node (bnheap-node-sibling minnode) (bnheap-node-sibling node)))
((null node))
(when (funcall lessp
(bnheap-node-keyval node)
(bnheap-node-keyval minnode))
(setq minnode node)))
;;
(bnheap-box-item (bnheap-node-box minnode))))
(defun delete-min-bnheap (bh)
;; Removes an item of minimum key in binomial heap BH
;; and returns the item.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh)
(error "find-min-bnheap: ~s is not bnheap-p" bh))
(when (bnheap-empty-p bh)
(error "find-min-bnheap: ~s is empty" bh))
(let* ((lessp (bnheap-lessp bh))
(minnode (bnheap-head bh))
(node-prev minnode)
(minnode-prev nil)
(bh1 (make-bnheap :lessp (bnheap-lessp bh) :key (bnheap-key bh))))
;; Set minnode-prev to the previous node of the bnheap-node.
(do ((node (bnheap-node-sibling minnode) (bnheap-node-sibling node)))
((null node))
(when (funcall lessp
(bnheap-node-keyval node)
(bnheap-node-keyval minnode))
(setq minnode node
minnode-prev node-prev))
(setq node-prev node))
;; Modify bh.
(cond
(minnode-prev
(setf (bnheap-node-sibling minnode-prev) (bnheap-node-sibling minnode)))
(t
(setf (bnheap-head bh) (bnheap-node-sibling minnode))))
(setf (bnheap-node-sibling minnode) nil)
;; Set up bh1.
(let ((revsibling (bnheap-node-child minnode)))
(setf (bnheap-node-child minnode) nil
(bnheap-node-degree minnode) 0)
(when revsibling
(let ((prev nil)
(next (bnheap-node-sibling revsibling)))
(loop
(setf (bnheap-node-parent revsibling) nil
(bnheap-node-sibling revsibling) prev
prev revsibling)
(when (null next)
(return))
(setf revsibling next)
(setf next (bnheap-node-sibling next)))
(setf (bnheap-head bh1) revsibling))))
;; Union
(union-bnheap bh bh1)
;;
(bnheap-box-item (bnheap-node-box minnode))))
(defun after-decrease-key-bnheap (bh ib)
;; After decreasing key of an item in binomial heap BH,
;; this function rebalances BH.
;; The second argument must be the box IB of the item instead of item
;; itself. The box is an object of type bnheap-box returned
;; from the function insert-bnheap when the item was inserted into BH.
;; Returns IB.
(declare (optimize (speed 3) (safety 0) (debug 0)))
(unless (bnheap-p bh)
(error "after-decrease-key-bnheap: ~s is not bnheap-p" bh))
(unless (bnheap-box-p ib)
(error "after-decrease-key-bnheap: ~s is not bnheap-box-p" ib))
;;
(let* ((lessp (bnheap-lessp bh))
(key (bnheap-key bh))
(bn1 (bnheap-box-node ib))
(bn2 (bnheap-node-parent bn1))
(newkey (funcall key (bnheap-box-item ib))))
(when (funcall lessp
(bnheap-node-keyval bn1)
newkey)
(error "after-decrease-key-bnheap: key of ~s has been increased"
(bnheap-box-item ib)))
(setf (bnheap-node-keyval bn1) newkey)
(while (and bn2 (funcall lessp
(bnheap-node-keyval bn1)
(bnheap-node-keyval bn2)))
;; Swap boxes of bn1 and bn2.
(let ((ib1 (bnheap-node-box bn1))
(ib2 (bnheap-node-box bn2)))
(setf (bnheap-node-box bn1) ib2
(bnheap-box-node ib2) bn1)
(setf (bnheap-node-box bn2) ib1
(bnheap-box-node ib1) bn2))
;; Up
(setq bn1 bn2
bn2 (bnheap-node-parent bn2))))
;;
ib)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; FHEAP - Fibonacci heap implementation of priority queue
(defstruct fdque
next
prev)
(defmacro link2-fdque (d1 d2)
(let ((vd1 (gensym))
(vd2 (gensym)))
`(let ((,vd1 ,d1)
(,vd2 ,d2))
(setf (fdque-next ,vd1) ,vd2
(fdque-prev ,vd2) ,vd1))))
(defmacro link3-fdque (d1 d2 d3)
(let ((vd1 (gensym))
(vd2 (gensym))
(vd3 (gensym)))
`(let ((,vd1 ,d1)
(,vd2 ,d2)
(,vd3 ,d3))
(setf (fdque-next ,vd1) ,vd2
(fdque-next ,vd2) ,vd3
(fdque-prev ,vd3) ,vd2
(fdque-prev ,vd2) ,vd1))))
(defun make-fdque-header (&optional (d (make-fdque)))
(link2-fdque d d)
d)
(defmacro fdque-empty-p (dqh)
`(eq (fdque-next ,dqh) ,dqh))
(defmacro append-fdque (d1 d2)
(let ((vd1 (gensym))
(vd2 (gensym)))
`(let ((,vd1 ,d1)
(,vd2 ,d2))
(link3-fdque ,vd1 ,vd2 (fdque-next ,vd1)))))
(defmacro delete-fdque (d)
(let ((vd (gensym)))
`(let ((,vd ,d))
(link2-fdque (fdque-prev ,vd) (fdque-next ,vd)))))
(defmacro merge-fdque (dqh1 dqh2)
(let ((vdqh1 (gensym))
(vdqh2 (gensym)))
`(let ((,vdqh1 ,dqh1)
(,vdqh2 ,dqh2))
(link2-fdque (fdque-prev ,vdqh1) (fdque-next ,vdqh2))
(link2-fdque (fdque-prev ,vdqh2) ,vdqh1)
(make-fdque-header ,vdqh2))))
(defmacro do-fdque ((var dqh) &rest body)
`(let ((,var ,dqh))
(while (not (eq (setq ,var (fdque-next ,var))
,dqh))
,@body)))
(defstruct (fheap
(:constructor _make-fheap))
(lessp #'<)
(key #'identity)
(count 0 :type fixnum)
(minnode nil)
(trees (make-fdque-header))
(Dn 0 :type fixnum)
(A (make-array 100) :type (simple-array t (*))))
(defstruct (fheap-node
(:include fdque))
(box nil)
(parent nil)
(degree 0 :type fixnum)
(mark nil)
(child (make-fdque-header)))
(defstruct fheap-box
item
node
keyval)
(defmacro fheap-node-keyval (bn)
`(fheap-box-keyval (fheap-node-box ,bn)))
(defmethod print-object ((fheap fheap) sm)
(format-fheap sm fheap))
(defmethod print-object ((fheap-node fheap-node) sm)
(let ((key #'identity))
(format-tree
sm
fheap-node
#'(lambda (fn)
(let ((child nil))