The cl-heap Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The cl-heap Reference Manual

This is the cl-heap Reference Manual, version 0.1.6, generated automatically by Declt version 4.0 beta 2 "William Riker" on Wed Jun 15 03:45:49 2022 GMT+0.

Table of Contents


1 Introduction

CL-HEAP

CL-HEAP is a Common Lisp library that provides efficient priority queue and heap implementations. Heaps are tree data structure in which any child, c, of a parent, p, obeys the relation p R c, for some relation R (often less-than or greater-than). Heaps can be used directly as priority queues, and are important components of many algorithms, like those for finding shortest paths and calculating minimum spanning trees.

The library is simple to use. Here's an example covering most of the priority queue functionality:

(defparameter *queue* (make-instance 'cl-heap:priority-queue))

(cl-heap:enqueue *queue* 'test 15) ; Enqueue the item with the priority of 15.
(cl-heap:enqueue *queue* 'this -5)
(cl-heap:enqueue *queue* 'a 10)
(cl-heap:enqueue *queue* 'is 5)

(cl-heap:peep-at-queue *queue*) => 'this

(cl-heap:dequeue *queue*) => 'this
(cl-heap:dequeue *queue*) => 'is
(cl-heap:dequeue *queue*) => 'a
(cl-heap:dequeue *queue*) => 'test

The library provides an implementation of an array-based binary heap - offering O(log(n)) times for most operations - and the Fibonacci heap. While any sequence of arbitrary operations on a Fibonacci heap occur in amortised logarithmic time, many common operations occur in constant and amortised constant time. See below for further details. The Fibonacci heap should generally be your heap of choice from this library.

Installation and Usage

CL-HEAP depends on XLUNIT, which is used to perform unit testing. CL-HEAP has been tested and is known to run on SBCL. If you've tried this on other compilers and it runs successfully, please let me know. The library can be installed using either ASDF-INSTALL or QUICKLISP:

(require 'asdf-install)
(asdf-install:install 'cl-heap)

(quicklisp:quickload 'cl-heap)
(quicklisp:quickload 'cl-heap-tests) 

After which, the library can then be loaded using either ASDF or QUICKLISP:

(asdf:load-system 'cl-heap)

Test Suite

CL-HEAP comes with a test suite. The tests for the various classes can be performed as follows:

(xlunit:textui-test-run (xlunit:get-suite cl-heap:fibonacci-test))
(xlunit:textui-test-run (xlunit:get-suite cl-heap:binary-test))
(xlunit:textui-test-run (xlunit:get-suite cl-heap:priority-queue-test))

The PRIORITY-QUEUE Class

This priority queue is a container for items in which all the items are sorted by some given priority. We implement the priority queue using a Fibonacci heap.

MAKE-INSTANCE accepts the argument :sort-fun to provide the function which the items' priorities are sorted by. This defaults to #'<, which will cause the queue to always return the item of lowest priority.

(ENQUEUE queue item priority)

Adds the item to the queue at the given priority. Returns a list containing first the item's priority, then the item itself. Running time: O(1)

(DEQUEUE queue)

Removes the item of lowest priority from the queue. Running time: amortised O(log(n))

(PEEP-AT-QUEUE queue)

Returns the item of lowest priority from the queue, without modifying the queue. Running time: O(1)

(EMPTY-QUEUE queue)

Removes all items from the queue. Returns the heap. Running time: O(1)

(QUEUE-SIZE queue)

Returns the number of items in the queue. Running time: O(1)

The HEAP Class

HEAP provides functionality common to the two heap classes implement by CL-HEAP. Each heap implementation accepts at least two arguments to MAKE-INSTANCE: :key and :sort-fun. :sort-fun supplies the function to be used when comparing two objects to each other, and defaults to #'<. :key gives a function which should be first applied to the elements in the HEAP before comparing them using the sorting function. :key defaults to #'identity. These functions can be accessed using HEAP-KEY and HEAP-SORTING-FUNCTION.

Both of the heap implementations obey the same interface:

(HEAP-KEY heap)

Returns the function passed as the :key argument to the heap.

(HEAP-SORTING-FUNCTION heap)

Returns the function used to sort the elements in the heap.

(HEAP-SIZE heap)

Returns the number of elements in the heap.

(EMPTY-HEAP heap)

Removes all items from the heap, and returns the heap.

(IS-EMPTY-HEAP-P heap)

Returns true iff the heap contains no elements.

(ADD-TO-HEAP heap item)

Adds the given item to the heap. Returns two values: first the item itself, and then some index-key which can be used by DECREASE-KEY and DELETE-KEY to identify which values should be affected by these operations. See below for an example.

(ADD-ALL-TO-HEAP heap items)

Adds all items in the list to the heap, as if by repeated calls to ADD-TO-HEAP. ADD-ALL-TO-HEAP can often be implemented more efficiently, though, than by merely executing consecutive calls to ADD-TO-HEAP directly. Returns the heap object.

(PEEP-AT-HEAP heap)

Returns the minimum item in the heap, without modifying the heap.

(POP-HEAP heap)

Removes the smallest item in the heap, and returns it.

(MERGE-HEAPS first second)

Returns a new heap which contains all the items in both heaps. Only heaps which use the same HEAP-KEY and HEAP-SORTING-FUNCTION can be merged, otherwise a HEAP-ERROR is signalled.

(NMERGE-HEAPS first second)

Destructively creates a new heap which contains the items in both heaps. Only heaps which use the same HEAP-KEY and HEAP-SORTING-FUNCTION can be merged, otherwise a HEAP-ERROR is signalled.

(DECREASE-KEY heap item-index value)

Allows you to specify an item in the heap whose value should be decreased. ITEM-INDEX is the second value returned by ADD-TO-HEAP, and VALUE should be smaller than the items current value, or a KEY-ERROR will be signalled. Returns the heap. See below for an example.

(DELETE-FROM-HEAP heap item-index)

Allows you to specify an arbitrary item to remove from the heap. ITEM-INDEX is the second value returned by ADD-TO-HEAP.

DECREASE-KEY requires that HEAP-KEY is either the function IDENTITY, or a function that accepts an optional second argument, which will be the value of the new key. For instance, if the elements in the heap are lists, and they are to be sorted by the first element in the list, an appropriate HEAP-KEY could be:

(lambda (item &optional new-value)
    (if new-value
        (setf (first item) new-value)
	(first item)))

Of course, if DECREASE-KEY is not going to be used, then #'first itself could simply be used as the HEAP-KEY.

The BINARY-HEAP Class

BINARY-HEAP is constructed in the typical fashion, using an array. The BINARY-HEAP MAKE-INSTANCE call accepts an extra argument, :size, which sets the data array's initial size.

Note that DECREASE-KEY and DELETE-FROM-HEAP are not very useful operations on this heap: while they work, the required index-keys may be invalidated as soon as any heap operation is performed. If you do want to make use of DELETE-FROM-HEAP or DECREASE-KEY, consider using FIBONACCI-HEAP instead.

BINARY-HEAP provides an extra function to the above API:

(BINARY-HEAP-EXTENSION-FACTOR heap)

The percentage at which the space allocated for data in the heap should grow at once that space has been exceeded. This defaults to 50.

Here are the Big-Oh running times for the heap API, where n and m are the sizes of various heaps.

(HEAP-SIZE heap)    	    	      => O(1)

(EMPTY-HEAP heap)		      => O(1)

(IS-EMPTY-HEAP-P heap)		      => O(1)

(ADD-TO-HEAP heap item)		      => O(log(n))

(ADD-ALL-TO-HEAP heap items)	      => O(n)

(PEEP-AT-HEAP heap)   		      => O(1)

(POP-HEAP heap)			      => O(log(n))

(MERGE-HEAPS first second)	      => O(n + m + log(n + m))

(NMERGE-HEAPS first second)	      => O(m + log(n + m))

(DECREASE-KEY heap item-index value)  => O(log(n))

(DELETE-FROM-HEAP heap item-index)    => O(log(n))

The FIBONACCI-HEAP Class

The details of the Fibonacci heap are given in "Fibonacci Heaps and Their Uses in Improved Network Optimization Algorithms", by Fredman and Tarjan (see references below).

The Fibonacci heap has some interesting time constraints, and should generally be used instead of BINARY-HEAP.

Here are the Big-Oh running times for the heap API, where n and m are the sizes of various heaps.

(HEAP-SIZE heap)    	    	      => O(1)

(EMPTY-HEAP heap)		      => O(1)

(IS-EMPTY-HEAP-P heap)		      => O(1)

(ADD-TO-HEAP heap item)		      => O(1)

(ADD-ALL-TO-HEAP heap items)	      => O(n)

(PEEP-AT-HEAP heap)   		      => O(1)

(POP-HEAP heap)			      => amortised O(log(n))

(MERGE-HEAPS first second)	      => O(m + n)

(NMERGE-HEAPS first second)	      => O(1)

(DECREASE-KEY heap item-index value)  => amortised O(1)

(DELETE-FROM-HEAP heap item-index)    => amortised O(1), except where
		       		         the deleted item was the minimum
		       		         item, in which case it is
				         amortised O(log(n))

Examples of Use

A heap can be created quite simply:

(defparameter *heap* (make-instance 'cl-heap:fibonacci-heap))

This will create a heap where the elements are ordered using #'<. Elements can be added one at a time using ADD-TO-HEAP:

(cl-heap:add-to-heap *heap* 1)

or, in a batch.

(cl-heap:add-all-to-heap *heap* '(6 4 7 3 2 0))

The minimum item in this heap can easily by seen using PEEP-AT-HEEP, which will not modify the heap in any way:

(cl-heap:peep-at-heap *heap*) => 0

The minimum item can be removed from the heap using POP-HEAP:

(cl-heap:pop-heap *heap*) => 0

Heaps can be used to sort items:

(let ((heap (make-instance 'cl-heap:fibonacci-heap)))
  (cl-heap:add-all-to-heap heap (loop for i from 1 upto 10 collect (random 100)))
  (loop while (not (cl-heap:is-empty-heap-p heap)) collect (cl-heap:pop-heap heap)))

=> (14 19 30 32 38 64 74 90 96 98)

A max-heap can be constructed as follows:

(defparameter *heap* (make-instance 'cl-heap:fibonacci-heap :sort-fun #'>))

And this will order things from most to least:

(let ((heap (make-instance 'cl-heap:fibonacci-heap :sort-fun #'>)))
  (cl-heap:add-all-to-heap heap (loop for i from 1 upto 10 collect (random 100)))
  (loop while (not (cl-heap:is-empty-heap-p heap)) collect (cl-heap:pop-heap heap)))

=> (69 68 64 60 37 34 30 7 6 1)

The heaps can contain arbitrary items. For instance, a heap whose elements are all lists can be constructed as follows:

(defparameter *heap* (make-instance 'cl-heap:fibonacci-heap :key #'first))
(cl-heap:add-to-heap *heap* (list 5 'some 'arbitrary 'data))
(cl-heap:add-to-heap *heap* (list 4 'other 'data))
(cl-heap:pop-heap *heap*) => (4 other data)

DECREASE-KEY and DELETE-FROM-HEAP can be used as follows:

(defparameter *heap* (make-instance 'cl-heap:fibonacci-heap))
(cl-heap:add-all-to-heap *heap* '(5 3 4 2 1))
(let ((item-index (second (multiple-value-list (cl-heap:add-to-heap *heap* 10)))))
     (format t "Smallest item: ~A~%" (cl-heap:peep-at-heap *heap*))
     (cl-heap:decrease-key *heap* item-index 0)
     (format t "Smallest item: ~A~%" (cl-heap:peep-at-heap *heap*)))

=> nil     
Smallest item: 1
Smallest item: 0

License

CL-HEAP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

References

M. L. Fredman and R. E. Tarjan. 1987. "Fibonacci Heaps and Their Uses in Improved Network Optimizxation Algorithms". Journal of the Association for Computing Machinery, Vol 34, No 3. pp 596 - 615


2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 cl-heap

An implementation of heap and priority queue data structures.

Author

Rudy Neeser <rudy.neeser@gmail.com>

License

GPLv3

Version

0.1.6

Source

cl-heap.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.1 cl-heap/cl-heap.asd

Source

cl-heap.asd.

Parent Component

cl-heap (system).

ASDF Systems

cl-heap.

Packages

cl-heap-asdf.


3.1.2 cl-heap/package.lisp

Source

cl-heap.asd.

Parent Component

cl-heap (system).

Packages

cl-heap.


3.1.3 cl-heap/condition.lisp

Dependency

package.lisp (file).

Source

cl-heap.asd.

Parent Component

cl-heap (system).

Public Interface
Internals

heap-error-message (reader method).


3.1.4 cl-heap/heap.lisp

Dependency

condition.lisp (file).

Source

cl-heap.asd.

Parent Component

cl-heap (system).

Public Interface
Internals

compare-items (generic function).


3.1.5 cl-heap/binary-heap.lisp

Dependency

heap.lisp (file).

Source

cl-heap.asd.

Parent Component

cl-heap (system).

Public Interface
Internals

3.1.6 cl-heap/fibonacci-heap.lisp

Dependency

binary-heap.lisp (file).

Source

cl-heap.asd.

Parent Component

cl-heap (system).

Public Interface
Internals

3.1.7 cl-heap/priority-queue.lisp

Dependency

fibonacci-heap.lisp (file).

Source

cl-heap.asd.

Parent Component

cl-heap (system).

Public Interface

4 Packages

Packages are listed by definition order.


Next: , Previous: , Up: Packages   [Contents][Index]

4.1 cl-heap-asdf

Source

cl-heap.asd.

Use List
  • asdf/interface.
  • common-lisp.

Previous: , Up: Packages   [Contents][Index]

4.2 cl-heap

An implementation of heap and priority queue data structures.

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


5.1.1 Generic functions

Generic Function: add-all-to-heap (heap items)

Adds a list of items to a HEAP. This can typically
be done more efficiently than by using repeated calls to ADD-TO-HEAP, since the heap order only needs to be reinstated after all of the items have been inserted, rather than maintained through each operation. Returns the heap object.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: add-all-to-heap ((heap fibonacci-heap) (items list))

Adds the following list of items into the heap. This is an O(n) operation.

Source

fibonacci-heap.lisp.

Method: add-all-to-heap ((heap binary-heap) (items list))

Adds all the items in the list into the BINARY-HEAP. This is a O(n + log(n)) = O(n) operation. Returns the BINARY-HEAP.

Source

binary-heap.lisp.

Generic Function: add-to-heap (heap item)

Inserts an item into the given HEAP data
structure. Returns two values: first, the item added, and then an index-key which can be used to identify the item for DECREASE-KEY and DELETE-FROM-HEAP operations.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: add-to-heap ((heap fibonacci-heap) item)

Adds an item to a Fibonacci-heap. This is a constant time operation. Returns the item added to the heap.

Source

fibonacci-heap.lisp.

Method: add-to-heap ((heap binary-heap) item)

Inserts an item into a BINARY-HEAP. This is O(log(n)), n being the number of items in the heap.

Source

binary-heap.lisp.

Generic Reader: binary-heap-extension-factor (object)
Generic Writer: (setf binary-heap-extension-factor) (object)
Package

cl-heap.

Methods
Reader Method: binary-heap-extension-factor ((binary-heap binary-heap))
Writer Method: (setf binary-heap-extension-factor) ((binary-heap binary-heap))

The percentage at which the space
allocated for data in the heap should grow at once that space has been exceeded.

Source

binary-heap.lisp.

Target Slot

extension-factor.

Generic Function: decrease-key (heap item-index value)

Decreases the value of the item represented by
ITEM-INDEX. ITEM-INDEX is the index returned by ADD-TO-HEAP for a particular item. VALUE is the item’s new value, and this must be "less" than its old value. Returns the heap.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: decrease-key ((heap fibonacci-heap) (item-index node) value)

Changes the value of an item represented by the ITEM-INDEX to VALUE. This index is returned as the second argument to ADD-TO-HEAP. This is an amortised constant time operation.

Source

fibonacci-heap.lisp.

Method: decrease-key ((heap binary-heap) (item-index integer) value)

Deceases the key of the item pointed to by ITEM-INDEX. The index is returned as the second value of ADD-TO-HEAP. The value of the item at the index is changed to VALUE, which should be less than its old value. This operation is O(log(n)), with n being the number of items in the heap. Note that using a binary heap is not ideal for this operation, since the item pointed to by any given index can be changed by performing any heap operation. This function is provided mostly for completeness.

Source

binary-heap.lisp.

Generic Function: delete-from-heap (heap item-index)

Removes the item from the heap represented by the ITEM-INDEX. This index is returned as the second value of ADD-TO-HEAP. Returns the heap.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: delete-from-heap ((heap fibonacci-heap) (item-index node))

Removes an item from the heap, as pointed to by item-index. This
operation is amortised O(1), unless the item removed is the minimum item, in which case the operation is equivalent to a POP-HEAP.

Source

fibonacci-heap.lisp.

Method: delete-from-heap ((heap binary-heap) (item-index integer))

Deltes an item from the heap. ITEM-INDEX is an index representing the value to remove, and is the second value returned from ADD-TO-HEAP. Note that running most HEAP functions can modify which value is pointed to by ITEM-INDEX, so this function is given mostly for completeness. Use a Fibonacci heap if this functionality is important to you. This operation completes in O(log(n)) time, where n is the number of items in the heap.

Source

binary-heap.lisp.

Generic Function: dequeue (queue)

Removes an element from the front of the queue and returns it. This is an amortised O(log(n)) operation.

Package

cl-heap.

Source

priority-queue.lisp.

Methods
Method: dequeue (queue)
Generic Function: empty-heap (heap)

Removes all contents from the given heap. Returns the heap.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: empty-heap ((heap fibonacci-heap))

Clears all items from the heap. This is a constant time operation.

Source

fibonacci-heap.lisp.

Method: empty-heap ((heap binary-heap))

Clears the heap. A constant time operation.

Source

binary-heap.lisp.

Generic Function: empty-queue (queue)

Removes all items from the queue. This is a constant time operation.

Package

cl-heap.

Source

priority-queue.lisp.

Methods
Method: empty-queue ((queue priority-queue))
Generic Function: enqueue (queue item priority)

Adds an item with a given priority on to the
queue. Returns a list containing first the priority, then the item. This is a constant time operation.

Package

cl-heap.

Source

priority-queue.lisp.

Methods
Method: enqueue ((queue priority-queue) item priority)
Generic Reader: heap-key (object)
Package

cl-heap.

Methods
Reader Method: heap-key ((heap heap))

A function used to obtain the elements which
should be compared to give an order to the items in the heap.

Source

heap.lisp.

Target Slot

key.

Generic Function: heap-size (heap)

Returns the number of elements in the heap.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: heap-size ((heap fibonacci-heap))
Source

fibonacci-heap.lisp.

Method: heap-size ((heap binary-heap))
Source

binary-heap.lisp.

Generic Reader: heap-sorting-function (object)
Package

cl-heap.

Methods
Reader Method: heap-sorting-function ((heap heap))

The function used to apply an order to elements in the heap.

Source

heap.lisp.

Target Slot

sort-fun.

Generic Function: is-empty-heap-p (heap)

Returns true iff the given heap is empty.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: is-empty-heap-p ((heap fibonacci-heap))
Source

fibonacci-heap.lisp.

Method: is-empty-heap-p ((heap binary-heap))
Source

binary-heap.lisp.

Generic Function: merge-heaps (first second)

Returns a new heap that is the merged copy of those
given here. Can only merge heaps which use the same key and sorting function. The two arguments are nt modified by this function.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: merge-heaps ((first fibonacci-heap) (second fibonacci-heap))

Returns the merge of the two given heaps. This operation runs in O(n + m), where n and m are the number of items in each heap.

Source

fibonacci-heap.lisp.

Method: merge-heaps ((first binary-heap) (second binary-heap))

Non-destructively merges two BINARY-HEAPs. This is an O(n + m + log(n + m)) operation, n and m being the zies of the two heaps.

Source

binary-heap.lisp.

Generic Function: nmerge-heaps (first second)

Destructively updates the arguments to contain the
merge of both heaps, which is then returned. Can only merge heaps which use the same key and sorting function.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: nmerge-heaps ((first fibonacci-heap) (second fibonacci-heap))

Destructively marges the two heaps. This is a constant time operation.

Source

fibonacci-heap.lisp.

Method: nmerge-heaps ((first binary-heap) (second binary-heap))

Destructively merges two BINARY-HEAPs, and returns the
result. Where n and m are the sizes of each queue, this is an order O(m + log(n + m) operation, unless an array resize is required, for which it becomes O(n + m + log(n + m)).

Source

binary-heap.lisp.

Generic Function: peep-at-heap (heap)

Returns the heap’s root, without modifying the heap. Returns nil if the heap is empty.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: peep-at-heap ((heap fibonacci-heap))

See the heap’s minimum value without modifying the heap. This is a constant time operation.

Source

fibonacci-heap.lisp.

Method: peep-at-heap ((heap binary-heap))

Returns the minimum object of the heap, without updating the heap. This is an O(1) operation.

Source

binary-heap.lisp.

Generic Function: peep-at-queue (queue)

Returns the element at the front of the queue,
without modifying the queue. This is a constant time operation.

Package

cl-heap.

Source

priority-queue.lisp.

Methods
Method: peep-at-queue ((queue priority-queue))
Generic Function: pop-heap (heap)

Removes the top element from the heap and returns it.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: pop-heap ((heap fibonacci-heap))

Remove the minimum element in the tree. This has an amortised running time of O(log(n)), where n is the number of items in the heap.

Source

fibonacci-heap.lisp.

Method: pop-heap ((heap binary-heap))

Removes the minimum item from the heap. This is an O(log(n)) operation, where n is the number of items in the heap.

Source

binary-heap.lisp.

Generic Function: queue-size (queue)

Returns the number of elements in the queue.

Package

cl-heap.

Source

priority-queue.lisp.

Methods
Method: queue-size ((queue priority-queue))

5.1.2 Standalone methods

Method: initialize-instance :after ((queue priority-queue) &key sort-fun)
Source

priority-queue.lisp.

Method: initialize-instance :after ((heap binary-heap) &key size)
Source

binary-heap.lisp.

Method: initialize-instance :after ((node node) &key)
Source

fibonacci-heap.lisp.

Method: print-object ((heap heap) stream)
Source

heap.lisp.

Method: print-object ((node node) stream)
Source

fibonacci-heap.lisp.


5.1.3 Conditions

Condition: heap-error

The base condition type for all errors that should generally be thrown in the CL-HEAP package.

Package

cl-heap.

Source

condition.lisp.

Direct superclasses

error.

Direct subclasses

key-error.

Direct methods

heap-error-message.

Direct slots
Slot: message
Initform

(quote "an error has occured while using this data structure.")

Initargs

:message

Readers

heap-error-message.

Writers

This slot is read-only.

Condition: key-error
Package

cl-heap.

Source

condition.lisp.

Direct superclasses

heap-error.

Direct slots
Slot: message
Initform

(quote "when using decrease-key, the heap-key function should always take two arguments.")

Initargs

:message


Previous: , Up: Public Interface   [Contents][Index]

5.1.4 Classes

Class: binary-heap

An implementation of a classic binary heap. This uses the standard array-based implementation.

Package

cl-heap.

Source

binary-heap.lisp.

Direct superclasses

heap.

Direct methods
Direct slots
Slot: data

The heap’s stored data.

Slot: extension-factor

The percentage at which the space
allocated for data in the heap should grow at once that space has been exceeded.

Initform

50

Readers

binary-heap-extension-factor.

Writers

(setf binary-heap-extension-factor).

Class: fibonacci-heap

A heap made up of item-disjoint, heap-ordered
trees. Has some good time constraints on various heap operations.

Package

cl-heap.

Source

fibonacci-heap.lisp.

Direct superclasses

heap.

Direct methods
Direct slots
Slot: root

The minimum element in the tree.

Slot: count

The number of items in the heap.

Package

common-lisp.

Initform

0

Class: heap

The base type of the two HEAP implementations.

Package

cl-heap.

Source

heap.lisp.

Direct subclasses
Direct methods
Direct slots
Slot: sort-fun

The function used to apply an order to elements in the heap.

Initform

(function <)

Initargs

:sort-fun

Readers

heap-sorting-function.

Writers

This slot is read-only.

Slot: key

A function used to obtain the elements which
should be compared to give an order to the items in the heap.

Initform

(function identity)

Initargs

:key

Readers

heap-key.

Writers

This slot is read-only.

Class: priority-queue

A simple priority queue implementaiton, based on a Fibonacci heap.

Package

cl-heap.

Source

priority-queue.lisp.

Direct methods
Direct slots
Slot: heap

5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Macros

Macro: do-each-node ((symbol node) &body body)
Package

cl-heap.

Source

fibonacci-heap.lisp.


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.2 Ordinary functions

Function: children-positions (position)
Package

cl-heap.

Source

binary-heap.lisp.

Function: parent-position (position)
Package

cl-heap.

Source

binary-heap.lisp.


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.3 Generic functions

Generic Function: compare-items (heap parent child)

Compares two items, using the HEAP’s SORT-FUN and KEY functions.

Package

cl-heap.

Source

heap.lisp.

Methods
Method: compare-items ((heap heap) parent child)
Generic Function: concatenate-node-lists (lhs rhs)
Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Method: concatenate-node-lists ((lhs node) (rhs null))
Method: concatenate-node-lists ((lhs null) (rhs node))
Method: concatenate-node-lists ((lhs node) (rhs node))
Generic Function: cut-node (heap node)

Cuts a child from its parent and makes and places it in the root list.

Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Method: cut-node ((heap fibonacci-heap) (node node))
Generic Function: delete-node (node)

Deletes this node from the linked list that it
represents, and returns the new list. Nulls the node’s parent, and resets its rank if appropriate.

Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Method: delete-node ((node null))
Method: delete-node ((node node))
Generic Reader: heap-error-message (condition)
Package

cl-heap.

Methods
Reader Method: heap-error-message ((condition heap-error))
Source

condition.lisp.

Target Slot

message.

Generic Function: is-node-root-p (node)
Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Method: is-node-root-p ((node node))

Places node-two as a child of node-one if node-one’s item is smaller, or vice versa.

Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Generic Function: mark-node (node)
Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Method: mark-node ((node node))
Generic Function: meld (one two)

Joins together two fibonacci heaps.

Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Method: meld ((heap1 fibonacci-heap) (heap2 fibonacci-heap))
Method: meld ((heap fibonacci-heap) (item node))

Adds a node to the heap.

Generic Reader: node-child (object)
Package

cl-heap.

Methods
Reader Method: node-child ((node node))

automatically generated reader method

Source

fibonacci-heap.lisp.

Target Slot

child.

Generic Writer: (setf node-child) (object)
Package

cl-heap.

Methods
Writer Method: (setf node-child) ((node node))

automatically generated writer method

Source

fibonacci-heap.lisp.

Target Slot

child.

Generic Reader: node-item (object)
Package

cl-heap.

Methods
Reader Method: node-item ((node node))

automatically generated reader method

Source

fibonacci-heap.lisp.

Target Slot

item.

Generic Writer: (setf node-item) (object)
Package

cl-heap.

Methods
Writer Method: (setf node-item) ((node node))

automatically generated writer method

Source

fibonacci-heap.lisp.

Target Slot

item.

Generic Reader: node-last (object)
Package

cl-heap.

Methods
Reader Method: node-last ((node node))

automatically generated reader method

Source

fibonacci-heap.lisp.

Target Slot

last.

Generic Writer: (setf node-last) (object)
Package

cl-heap.

Methods
Writer Method: (setf node-last) ((node node))

automatically generated writer method

Source

fibonacci-heap.lisp.

Target Slot

last.

Generic Reader: node-marked-p (object)
Generic Writer: (setf node-marked-p) (object)
Package

cl-heap.

Methods
Reader Method: node-marked-p ((node node))
Writer Method: (setf node-marked-p) ((node node))

Used to implement cascading cuts.

Source

fibonacci-heap.lisp.

Target Slot

marked.

Generic Reader: node-next (object)
Package

cl-heap.

Methods
Reader Method: node-next ((node node))

automatically generated reader method

Source

fibonacci-heap.lisp.

Target Slot

next.

Generic Writer: (setf node-next) (object)
Package

cl-heap.

Methods
Writer Method: (setf node-next) ((node node))

automatically generated writer method

Source

fibonacci-heap.lisp.

Target Slot

next.

Generic Reader: node-parent (object)
Package

cl-heap.

Methods
Reader Method: node-parent ((node node))

automatically generated reader method

Source

fibonacci-heap.lisp.

Target Slot

parent.

Generic Writer: (setf node-parent) (object)
Package

cl-heap.

Methods
Writer Method: (setf node-parent) ((node node))

automatically generated writer method

Source

fibonacci-heap.lisp.

Target Slot

parent.

Generic Reader: node-rank (object)
Generic Writer: (setf node-rank) (object)
Package

cl-heap.

Methods
Reader Method: node-rank ((node node))
Writer Method: (setf node-rank) ((node node))

The number of children the node has.

Source

fibonacci-heap.lisp.

Target Slot

rank.

Generic Function: percolate-down (heap position)

Used to move a value in the DATA array of a
BINARY-HEAP down the parent-child relationship hierarchy, and so preserve heap-ordering.

Package

cl-heap.

Source

binary-heap.lisp.

Methods
Method: percolate-down ((heap binary-heap) (position integer))
Generic Function: percolate-up (heap position)
Package

cl-heap.

Source

binary-heap.lisp.

Methods
Method: percolate-up ((heap binary-heap) (position integer))
Generic Function: unmark-node (node)
Package

cl-heap.

Source

fibonacci-heap.lisp.

Methods
Method: unmark-node ((node node))

Previous: , Up: Internals   [Contents][Index]

5.2.4 Classes

Class: node

A class used for storing data in a FIBONACCI-HEAP.

Package

cl-heap.

Source

fibonacci-heap.lisp.

Direct methods
Direct slots
Slot: item
Initargs

:item

Readers

node-item.

Writers

(setf node-item).

Slot: parent
Readers

node-parent.

Writers

(setf node-parent).

Slot: child
Readers

node-child.

Writers

(setf node-child).

Slot: rank

The number of children the node has.

Initform

0

Readers

node-rank.

Writers

(setf node-rank).

Slot: marked

Used to implement cascading cuts.

Readers

node-marked-p.

Writers

(setf node-marked-p).

Slot: next
Readers

node-next.

Writers

(setf node-next).

Slot: last
Package

common-lisp.

Readers

node-last.

Writers

(setf node-last).


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   (  
A   B   C   D   E   F   G   H   I   L   M   N   P   Q   U  
Index Entry  Section

(
(setf binary-heap-extension-factor): Public generic functions
(setf binary-heap-extension-factor): Public generic functions
(setf node-child): Private generic functions
(setf node-child): Private generic functions
(setf node-item): Private generic functions
(setf node-item): Private generic functions
(setf node-last): Private generic functions
(setf node-last): Private generic functions
(setf node-marked-p): Private generic functions
(setf node-marked-p): Private generic functions
(setf node-next): Private generic functions
(setf node-next): Private generic functions
(setf node-parent): Private generic functions
(setf node-parent): Private generic functions
(setf node-rank): Private generic functions
(setf node-rank): Private generic functions

A
add-all-to-heap: Public generic functions
add-all-to-heap: Public generic functions
add-all-to-heap: Public generic functions
add-to-heap: Public generic functions
add-to-heap: Public generic functions
add-to-heap: Public generic functions

B
binary-heap-extension-factor: Public generic functions
binary-heap-extension-factor: Public generic functions

C
children-positions: Private ordinary functions
compare-items: Private generic functions
compare-items: Private generic functions
concatenate-node-lists: Private generic functions
concatenate-node-lists: Private generic functions
concatenate-node-lists: Private generic functions
concatenate-node-lists: Private generic functions
cut-node: Private generic functions
cut-node: Private generic functions

D
decrease-key: Public generic functions
decrease-key: Public generic functions
decrease-key: Public generic functions
delete-from-heap: Public generic functions
delete-from-heap: Public generic functions
delete-from-heap: Public generic functions
delete-node: Private generic functions
delete-node: Private generic functions
delete-node: Private generic functions
dequeue: Public generic functions
dequeue: Public generic functions
do-each-node: Private macros

E
empty-heap: Public generic functions
empty-heap: Public generic functions
empty-heap: Public generic functions
empty-queue: Public generic functions
empty-queue: Public generic functions
enqueue: Public generic functions
enqueue: Public generic functions

F
Function, children-positions: Private ordinary functions
Function, parent-position: Private ordinary functions

G
Generic Function, (setf binary-heap-extension-factor): Public generic functions
Generic Function, (setf node-child): Private generic functions
Generic Function, (setf node-item): Private generic functions
Generic Function, (setf node-last): Private generic functions
Generic Function, (setf node-marked-p): Private generic functions
Generic Function, (setf node-next): Private generic functions
Generic Function, (setf node-parent): Private generic functions
Generic Function, (setf node-rank): Private generic functions
Generic Function, add-all-to-heap: Public generic functions
Generic Function, add-to-heap: Public generic functions
Generic Function, binary-heap-extension-factor: Public generic functions
Generic Function, compare-items: Private generic functions
Generic Function, concatenate-node-lists: Private generic functions
Generic Function, cut-node: Private generic functions
Generic Function, decrease-key: Public generic functions
Generic Function, delete-from-heap: Public generic functions
Generic Function, delete-node: Private generic functions
Generic Function, dequeue: Public generic functions
Generic Function, empty-heap: Public generic functions
Generic Function, empty-queue: Public generic functions
Generic Function, enqueue: Public generic functions
Generic Function, heap-error-message: Private generic functions
Generic Function, heap-key: Public generic functions
Generic Function, heap-size: Public generic functions
Generic Function, heap-sorting-function: Public generic functions
Generic Function, is-empty-heap-p: Public generic functions
Generic Function, is-node-root-p: Private generic functions
Generic Function, link: Private generic functions
Generic Function, mark-node: Private generic functions
Generic Function, meld: Private generic functions
Generic Function, merge-heaps: Public generic functions
Generic Function, nmerge-heaps: Public generic functions
Generic Function, node-child: Private generic functions
Generic Function, node-item: Private generic functions
Generic Function, node-last: Private generic functions
Generic Function, node-marked-p: Private generic functions
Generic Function, node-next: Private generic functions
Generic Function, node-parent: Private generic functions
Generic Function, node-rank: Private generic functions
Generic Function, peep-at-heap: Public generic functions
Generic Function, peep-at-queue: Public generic functions
Generic Function, percolate-down: Private generic functions
Generic Function, percolate-up: Private generic functions
Generic Function, pop-heap: Public generic functions
Generic Function, queue-size: Public generic functions
Generic Function, unmark-node: Private generic functions

H
heap-error-message: Private generic functions
heap-error-message: Private generic functions
heap-key: Public generic functions
heap-key: Public generic functions
heap-size: Public generic functions
heap-size: Public generic functions
heap-size: Public generic functions
heap-sorting-function: Public generic functions
heap-sorting-function: Public generic functions

I
initialize-instance: Public standalone methods
initialize-instance: Public standalone methods
initialize-instance: Public standalone methods
is-empty-heap-p: Public generic functions
is-empty-heap-p: Public generic functions
is-empty-heap-p: Public generic functions
is-node-root-p: Private generic functions
is-node-root-p: Private generic functions

L
link: Private generic functions
link: Private generic functions

M
Macro, do-each-node: Private macros
mark-node: Private generic functions
mark-node: Private generic functions
meld: Private generic functions
meld: Private generic functions
meld: Private generic functions
merge-heaps: Public generic functions
merge-heaps: Public generic functions
merge-heaps: Public generic functions
Method, (setf binary-heap-extension-factor): Public generic functions
Method, (setf node-child): Private generic functions
Method, (setf node-item): Private generic functions
Method, (setf node-last): Private generic functions
Method, (setf node-marked-p): Private generic functions
Method, (setf node-next): Private generic functions
Method, (setf node-parent): Private generic functions
Method, (setf node-rank): Private generic functions
Method, add-all-to-heap: Public generic functions
Method, add-all-to-heap: Public generic functions
Method, add-to-heap: Public generic functions
Method, add-to-heap: Public generic functions
Method, binary-heap-extension-factor: Public generic functions
Method, compare-items: Private generic functions
Method, concatenate-node-lists: Private generic functions
Method, concatenate-node-lists: Private generic functions
Method, concatenate-node-lists: Private generic functions
Method, cut-node: Private generic functions
Method, decrease-key: Public generic functions
Method, decrease-key: Public generic functions
Method, delete-from-heap: Public generic functions
Method, delete-from-heap: Public generic functions
Method, delete-node: Private generic functions
Method, delete-node: Private generic functions
Method, dequeue: Public generic functions
Method, empty-heap: Public generic functions
Method, empty-heap: Public generic functions
Method, empty-queue: Public generic functions
Method, enqueue: Public generic functions
Method, heap-error-message: Private generic functions
Method, heap-key: Public generic functions
Method, heap-size: Public generic functions
Method, heap-size: Public generic functions
Method, heap-sorting-function: Public generic functions
Method, initialize-instance: Public standalone methods
Method, initialize-instance: Public standalone methods
Method, initialize-instance: Public standalone methods
Method, is-empty-heap-p: Public generic functions
Method, is-empty-heap-p: Public generic functions
Method, is-node-root-p: Private generic functions
Method, link: Private generic functions
Method, mark-node: Private generic functions
Method, meld: Private generic functions
Method, meld: Private generic functions
Method, merge-heaps: Public generic functions
Method, merge-heaps: Public generic functions
Method, nmerge-heaps: Public generic functions
Method, nmerge-heaps: Public generic functions
Method, node-child: Private generic functions
Method, node-item: Private generic functions
Method, node-last: Private generic functions
Method, node-marked-p: Private generic functions
Method, node-next: Private generic functions
Method, node-parent: Private generic functions
Method, node-rank: Private generic functions
Method, peep-at-heap: Public generic functions
Method, peep-at-heap: Public generic functions
Method, peep-at-queue: Public generic functions
Method, percolate-down: Private generic functions
Method, percolate-up: Private generic functions
Method, pop-heap: Public generic functions
Method, pop-heap: Public generic functions
Method, print-object: Public standalone methods
Method, print-object: Public standalone methods
Method, queue-size: Public generic functions
Method, unmark-node: Private generic functions

N
nmerge-heaps: Public generic functions
nmerge-heaps: Public generic functions
nmerge-heaps: Public generic functions
node-child: Private generic functions
node-child: Private generic functions
node-item: Private generic functions
node-item: Private generic functions
node-last: Private generic functions
node-last: Private generic functions
node-marked-p: Private generic functions
node-marked-p: Private generic functions
node-next: Private generic functions
node-next: Private generic functions
node-parent: Private generic functions
node-parent: Private generic functions
node-rank: Private generic functions
node-rank: Private generic functions

P
parent-position: Private ordinary functions
peep-at-heap: Public generic functions
peep-at-heap: Public generic functions
peep-at-heap: Public generic functions
peep-at-queue: Public generic functions
peep-at-queue: Public generic functions
percolate-down: Private generic functions
percolate-down: Private generic functions
percolate-up: Private generic functions
percolate-up: Private generic functions
pop-heap: Public generic functions
pop-heap: Public generic functions
pop-heap: Public generic functions
print-object: Public standalone methods
print-object: Public standalone methods

Q
queue-size: Public generic functions
queue-size: Public generic functions

U
unmark-node: Private generic functions
unmark-node: Private generic functions

Jump to:   (  
A   B   C   D   E   F   G   H   I   L   M   N   P   Q   U  

Previous: , Up: Indexes   [Contents][Index]

A.4 Data types

Jump to:   B   C   F   H   K   N   P   S  
Index Entry  Section

B
binary-heap: Public classes
binary-heap.lisp: The cl-heap/binary-heap․lisp file

C
cl-heap: The cl-heap system
cl-heap: The cl-heap package
cl-heap-asdf: The cl-heap-asdf package
cl-heap.asd: The cl-heap/cl-heap․asd file
Class, binary-heap: Public classes
Class, fibonacci-heap: Public classes
Class, heap: Public classes
Class, node: Private classes
Class, priority-queue: Public classes
Condition, heap-error: Public conditions
Condition, key-error: Public conditions
condition.lisp: The cl-heap/condition․lisp file

F
fibonacci-heap: Public classes
fibonacci-heap.lisp: The cl-heap/fibonacci-heap․lisp file
File, binary-heap.lisp: The cl-heap/binary-heap․lisp file
File, cl-heap.asd: The cl-heap/cl-heap․asd file
File, condition.lisp: The cl-heap/condition․lisp file
File, fibonacci-heap.lisp: The cl-heap/fibonacci-heap․lisp file
File, heap.lisp: The cl-heap/heap․lisp file
File, package.lisp: The cl-heap/package․lisp file
File, priority-queue.lisp: The cl-heap/priority-queue․lisp file

H
heap: Public classes
heap-error: Public conditions
heap.lisp: The cl-heap/heap․lisp file

K
key-error: Public conditions

N
node: Private classes

P
Package, cl-heap: The cl-heap package
Package, cl-heap-asdf: The cl-heap-asdf package
package.lisp: The cl-heap/package․lisp file
priority-queue: Public classes
priority-queue.lisp: The cl-heap/priority-queue․lisp file

S
System, cl-heap: The cl-heap system

Jump to:   B   C   F   H   K   N   P   S