{{ message }}

# vy / binomial-heap

The binomial-heap data structure implementation in Common Lisp.

## Files

Failed to load latest commit information.
Type
Name
Commit time

``````___  __ __   ____ _    __ ___  __        _  _ ____ ___  ____
| .\ |_\| \|\|   ||\/\ |_\|  \ | |   ___ ||_|\| __\|  \ | . \
| .<_| /|  \|| . ||   \| /| . \| |__|___\| _ ||  ]_| . \| __/
|___/|/ |/\_/|___/|/v\/|/ |/\_/|___/     |/ |/|___/|/\_/|/
``````

# Abstract

Binomial-heap is a compact and succint implementation of the binomial heap data structure in Common Lisp programming language. Insertion, extremum access, extremum extraction, and union operations are performed in O(logn) time.

# Demo

``````(defvar *list* (loop repeat 20 collect (random 100)))
; => (25 50 12 53 53 55 41 71 71 41 33 8 71 57 28 4 89 96 58 25)

(defvar *heap* (make-instance 'bh:binomial-heap :test #'<))
; => #<BINOMIAL-HEAP {1002C4EC31}>

(dolist (item *list*)
(insert-key *heap* item))
; => NIL

; => -> ( 2) 25
;      -> ( 1) 89
;        -> ( 0) 96
;      -> ( 0) 58
;    -> ( 4) 4
;      -> ( 3) 12
;        -> ( 2) 41
;          -> ( 1) 53
;            -> ( 0) 55
;          -> ( 0) 71
;        -> ( 1) 25
;          -> ( 0) 50
;        -> ( 0) 53
;      -> ( 2) 8
;        -> ( 1) 41
;          -> ( 0) 71
;        -> ( 0) 33
;      -> ( 1) 57
;        -> ( 0) 71
;      -> ( 0) 28
;    NIL

(bh:get-extremum-key *heap*)
; => 4

(loop for x in (sort (copy-list *list*) (test-of *heap*))
for y = (extract-extremum-key *heap*)
unless (= x y)
collect (cons x y))
; => NIL

(let ((h1 (make-instance 'bh:binomial-heap :test #'string<))
(h2 (make-instance 'bh:binomial-heap :test #'string<))
(l1 '("foo" "bar" "baz" "mov" "mov"))
(l2 '("i" "see" "dead" "binomial" "trees")))
(dolist (l l1) (bh:insert-key h1 l))
; => -> ( 0) "mov"
;    -> ( 2) "bar"
;      -> ( 1) "baz"
;        -> ( 0) "mov"
;      -> ( 0) "foo"
; NIL
(dolist (l l2) (bh:insert-key h2 l))
; => -> ( 0) "trees"
;    -> ( 2) "binomial"
;      -> ( 1) "i"
;        -> ( 0) "see"
; NIL
(let ((h3 (bh:unite-heaps h1 h2)))
; => -> ( 1) "mov"
;      -> ( 0) "trees"
;    -> ( 3) "bar"
;      -> ( 2) "binomial"
;        -> ( 1) "i"
;          -> ( 0) "see"
;      -> ( 1) "baz"
;        -> ( 0) "mov"
;      -> ( 0) "foo"
; NIL
``````

# Caveats

Despite binomial heaps are known to perform decrease/increase key and delete operations in O(logn) time, this is practically not that easy to implement. (For the rest of this talk, I'll skip the deletion operation because of it can be achieved through setting the key field of a node to the absolute extremum -- i.e. negative infinity -- and extracting the extremum.) Consider below example.

``````--> [ Z ] -->
^
|
|
--> [ X ] -->
^^^
|||
||+----------------------------+
|+----------+        ...       |
|           |                  |
--> [ W0 ] --> [ W1 ] --> ... --> [ WN ]
``````

Suppose you decreased the key field of X and you need to bubble up X by swapping nodes in upwards direction appropriately. Because of random access is not possible in heap data structures, you need to figure out your own way of accessing to nodes -- in this example consider you have the pointers in advance to the every `BINOMIAL-TREE` in the `BINOMIAL-HEAP`. There are two ways to swap nodes:

## Swapping Key Fields

If you just swap the key fields of the nodes

``````(rotatef (key-of x) (key-of z))
``````

everything will be fine, except that the pointers to the nodes that lost their original key fields will get invalidated. Now you cannot guarantee the validity of your node pointers and hence cannot issue any more decrease key operations.

## Swapping Node Instances

If you swap the two node instances, your pointers won't get invalidated but this time you'll need to update the sibling and parent pointers as well,

``````(setf (parent-of w0) z
(parent-of w1) z
...
(parent-of wn) z)
``````

which will make your O(logn) complexity dreams fade away. (Moreover, you'll need to traverse sibling lists at levels of nodes `X` and `Y` to be able to find previous siblings to `X` and `Y` if you are not using doubly-linked-lists. But even this scheme doesn't save us from the traversal of `W1`, ..., `WN` nodes.)

## Solution

So how can we manage to perform decrease key operation in O(logn) time without invalidating any node pointers? The solution I come up with to this problem is as follows.

We can keep a separate hash table for the pointers to the nodes. When a node's key field gets modified, related hash table entry will get modified as well. And instead of returning to the user the actual `BINOMIAL-TREE` instances, we'll return to the user the key of the related hash table entry. (Consider this hash table as a mapping between the hash table keys and the pointer to the actual node instance.)

Sounds too hairy? I think so. I'd be appreciated for any sort of enlightenment of a better solution.

The binomial-heap data structure implementation in Common Lisp.
{"ft_lang_label":"__label__en","ft_lang_prob":0.80343205,"math_prob":0.78025895,"size":4474,"snap":"2020-45-2020-50","text_gpt3_token_len":1457,"char_repetition_ratio":0.1393736,"word_repetition_ratio":0.09885058,"special_character_ratio":0.41260618,"punctuation_ratio":0.15109573,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9633003,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-23T01:11:10Z\",\"WARC-Record-ID\":\"<urn:uuid:73c08c34-b5a7-4ffa-94e2-32c1c6d463cb>\",\"Content-Length\":\"119014\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eab3f0b2-b22b-4b8b-a735-353349f57744>\",\"WARC-Concurrent-To\":\"<urn:uuid:a09bd4e2-bb39-46b7-9518-2a99c686b407>\",\"WARC-IP-Address\":\"140.82.113.4\",\"WARC-Target-URI\":\"https://github.com/vy/binomial-heap\",\"WARC-Payload-Digest\":\"sha1:7YV35JPPFTKE2667H7ONIMI5JIRDAYLF\",\"WARC-Block-Digest\":\"sha1:77PFZRFRK4DNI6655BJ6EAHBLYKB5AVK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107880401.35_warc_CC-MAIN-20201022225046-20201023015046-00247.warc.gz\"}"}