FibonacciHeap

    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    40

    No dependencies

    #FibonacciHeap

    A high-performance Fibonacci Heap implemented in MoonBit, supporting efficient priority queue operations. Suitable for graph algorithms, scheduling systems, and scenarios requiring frequent merges and decrease-key operations. FibonacciHeap offers excellent amortized complexity, making it ideal for large-scale data processing and high-performance requirements.

    #šŸš€ Key Features

    • ⚔ O(1) Amortized Insert & Merge – Constant time for insert and heap merge • šŸ”„ O(1) Amortized Decrease-Key – Efficient decrease-key operation • šŸ“‰ O(log n) Amortized Delete-Min/Arbitrary Delete – Efficient deletion operations • 🧩 Generic Key/Value Types – Supports any type for keys and values • šŸ› ļø Batch Insert & Deep Clone – Supports insert_many and clone • šŸ” Find & Traverse – Find by value/key, unordered/sorted traversal • 🧹 Clear Heap – Clear all elements in one step • 🧪 Comprehensive Test Coverage – Includes unit tests for extreme edge cases

    #šŸ“„ Installation

    moon add SupremeHuaji/FibonacciHeap

    #šŸš€ Usage Guide

    Below are corrected MoonBit code snippets for insertion and other related operations:


    #šŸ”Ø Create Heap

    let heap = new(int_compare)

    #āž• Insert Elements

    let heap = new(int_compare)
    ignore(heap.insert("apple", 5))
    ignore(heap.insert("banana", 2))

    #šŸ“‰ Peek/Extract Minimum

    let heap = new(int_compare)
    ignore(heap.insert("apple", 5))
    ignore(heap.insert("banana", 2))
    let min = heap.peek_min() // Some(("banana", 2))
    let min_pair = heap.extract_min() // Some(("banana", 2))

    #šŸ—‘ļø Delete Arbitrary Node

    let heap = new(int_compare)
    let node = heap.insert("orange", 7)
    let result = heap.delete(node) // true

    #šŸ”— Merge Heaps

    let heap1 = new(int_compare)
    let heap2 = new(int_compare)
    ignore(heap1.insert("apple", 5))
    ignore(heap2.insert("pear", 1))
    heap1.merge(heap2)

    #šŸ› ļø Batch Insert

    let heap = new(int_compare)
    let values = ["a", "b", "c"]
    let keys = [3, 1, 2]
    heap.insert_many(values, keys)

    #šŸ” Find Node

    let heap = new(int_compare)
    ignore(heap.insert("a", 1))
    let node = heap.find_by_value("a")
    let node2 = heap.find_by_key(1)

    #šŸŽÆ Traverse All Elements (Unordered/Sorted)

    let heap = new(int_compare)
    ignore(heap.insert("a", 3))
    ignore(heap.insert("b", 1))
    ignore(heap.insert("c", 2))
    let arr = heap.to_array()
    let sorted_arr = heap.to_sorted_array()

    #🧹 Clear Heap

    let heap = new(int_compare)
    ignore(heap.insert("a", 1))
    heap.clear()

    #šŸ—ļø Deep Clone Heap

    let heap = new(int_compare)
    ignore(heap.insert("a", 1))
    ignore(heap.insert("b", 2))

    let heap2 = heap.clone()
    // heap2 is a complete copy of heap

    ignore(heap.insert("c", 3)) // Modify original heap
    ignore(heap2.insert("d", 4)) // Modify clone

    // The two heaps are independent
    let arr1 = heap.to_sorted_array() // Contains a, b, c
    let arr2 = heap2.to_sorted_array() // Contains a, b, d

    #šŸ“Š Performance Characteristics

    OperationAmortized ComplexityDescription
    insert()O(1)Insert new element
    merge()O(1)Merge two heaps
    peek_min()O(1)Peek minimum element
    extract_min()O(log n)Remove and return minimum
    delete(node)O(log n)Delete arbitrary node
    decrease_key()O(1)Decrease node key
    to_array()O(n)Unordered traversal
    to_sorted_array()O(n log n)Sorted traversal by key
    clone()O(n)Deep clone heap
    insert_many()O(m)Batch insert m elements

    Where:
    • n = number of elements in the heap
    • m = number of elements inserted in batch

    #šŸ—ļø Implementation Details

    FibonacciHeap uses a circular doubly-linked list to manage root nodes. Each node supports parent, child, left, and right pointers. Merge and insert are constant time via list operations; delete and extract-min use consolidate to merge trees of the same degree, ensuring logarithmic complexity.

    #šŸ“¦ Core Data Structures

    1. Node Structure (Node)
      • Content: Stores actual value and key
      • Degree: Number of child nodes
      • Mark: For cascading cuts
      • Parent/Child/Left/Right Pointers: Support heap and circular list structure

    2. Heap Structure (FibHeap)
      • Root List: Circular doubly-linked list
      • Min Pointer: Fast access to minimum element
      • Element Count: Current number of elements
      • Compare Function: Custom key comparison logic

    #šŸ”„ Fibonacci Heap Working Principle

    // Insert/Merge: Directly add node to root list
    // Delete-Min: Promote all children of min node to root, consolidate trees of same degree
    // Decrease-Key: If heap order violated, cut node and cascade cut parent

    #šŸ†š FibonacciHeap vs Traditional Binary Heap

    FeatureBinary HeapFibonacciHeap
    InsertO(log n)O(1)
    MergeO(n)O(1)
    Delete-MinO(log n)O(log n)
    Arbitrary DeleteO(log n)O(log n)
    Decrease-KeyO(log n)O(1)
    Use CaseGeneralGraph/High Performance

    #šŸ¤ Contributing

    Contributions are welcome! Please follow these steps:

    1. Fork this repository
    2. Create a feature branch (git checkout -b feature/AmazingFeature)
    3. Commit your changes (git commit -m 'Add some AmazingFeature')
    4. Push to the branch (git push origin feature/AmazingFeature)
    5. Open a Pull Request

    #šŸ“œ License

    This project is licensed under Apache-2.0. See the LICENSE file for details.

    FibHeap

    pub(all) struct FibHeap[A, K] {
    size : Int
    min : Node[A, K]
    roots : Int
    compare : (K, K) -> Int
    }

    impl Eq for FibHeap[A, K]
    impl Show for FibHeap[A, K]

    FibHeap::clear

    fn[A, K] FibHeap::clear(self : FibHeap[A, K]) -> Unit
    ęø…ē©ŗå †ļ¼Œé‡ē½®ę‰€ęœ‰å±žę€§

    FibHeap::clone

    fn[A : Eq, K : Eq] FibHeap::clone(self : FibHeap[A, K]) -> FibHeap[A, K]
    ę”ÆęŒå †ēš„ę·±ę‹·č“ļ¼Œčæ”å›žå †ēš„å®Œę•“å‰Æęœ¬

    FibHeap::decrease_key

    fn[A : Eq, K : Eq] FibHeap::decrease_key(self : FibHeap[A, K], node : Node[A, K], new_key : K) -> Bool
    å‡å°čŠ‚ē‚¹ēš„é”®å€¼

    FibHeap::delete

    fn[A : Eq, K : Eq] FibHeap::delete(self : FibHeap[A, K], node : Node[A, K]) -> Bool
    åˆ é™¤čŠ‚ē‚¹ļ¼ˆäøä¾čµ–ęžå°å€¼ļ¼‰

    FibHeap::extract_min

    fn[A : Eq, K : Eq] FibHeap::extract_min(self : FibHeap[A, K]) -> (A, K)?
    ęå–ęœ€å°é”®å€¼å…ƒē“ 

    FibHeap::find_by_key

    fn[A : Eq, K : Eq] FibHeap::find_by_key(self : FibHeap[A, K], key : K) -> Node[A, K]
    ęŒ‰é”®ęŸ„ę‰¾čŠ‚ē‚¹ļ¼Œčæ”å›žčŠ‚ē‚¹å¼•ē”Øęˆ– Node::Nil

    FibHeap::find_by_value

    fn[A : Eq, K : Eq] FibHeap::find_by_value(self : FibHeap[A, K], value : A) -> Node[A, K]
    ęŒ‰å€¼ęŸ„ę‰¾čŠ‚ē‚¹ļ¼Œčæ”å›žčŠ‚ē‚¹å¼•ē”Øęˆ– Node::Nil

    FibHeap::insert

    fn[A : Eq, K : Eq] FibHeap::insert(self : FibHeap[A, K], value : A, key : K) -> Node[A, K]
    å‘å †äø­ę’å…„ę–°å…ƒē“ 

    FibHeap::insert_many

    fn[A : Eq, K : Eq] FibHeap::insert_many(self : FibHeap[A, K], values : Array[A], keys : Array[K]) -> Unit
    ę”ÆęŒę‰¹é‡ę’å…„

    FibHeap::is_empty

    fn[A, K] FibHeap::is_empty(self : FibHeap[A, K]) -> Bool

    ę£€ęŸ„å †ę˜Æå¦äøŗē©ŗ

    FibHeap::merge

    fn[A : Eq, K : Eq] FibHeap::merge(self : FibHeap[A, K], other : FibHeap[A, K]) -> Unit
    åˆå¹¶äø¤äøŖę–ę³¢é‚£å„‘å †

    FibHeap::peek_min

    fn[A, K] FibHeap::peek_min(self : FibHeap[A, K]) -> (A, K)?
    ęŸ„ēœ‹ęœ€å°é”®å€¼å…ƒē“ 

    FibHeap::size

    fn[A, K] FibHeap::size(self : FibHeap[A, K]) -> Int

    čŽ·å–å †äø­å…ƒē“ ę•°é‡

    FibHeap::to_array

    fn[A : Eq, K : Eq] FibHeap::to_array(self : FibHeap[A, K]) -> Array[(A, K)]

    FibHeap::to_sorted_array

    fn[A : Eq, K : Eq] FibHeap::to_sorted_array(self : FibHeap[A, K]) -> Array[(A, K)]
    čæ”å›žęŒ‰é”®ęŽ’åŗåŽēš„ę‰€ęœ‰å…ƒē“ ę•°ē»„

    Node

    pub(all) enum Node[A, K] {
    Nil
    Cons(A, K, Int, Bool, Node[A, K], Node[A, K], Node[A, K], Node[A, K])
    }

    impl Eq for Node[A, K]
    impl Show for Node[A, K]

    add_child

    fn[A, K] add_child(parent : Node[A, K], child : Node[A, K]) -> Unit
    å°†äø€äøŖčŠ‚ē‚¹ä½œäøŗå¦äø€äøŖčŠ‚ē‚¹ēš„å­čŠ‚ē‚¹

    add_children_to_root_list

    fn[A : Eq, K : Eq] add_children_to_root_list(heap : FibHeap[A, K], parent : Node[A, K]) -> Unit

    add_to_root_list

    fn[A, K] add_to_root_list(heap : FibHeap[A, K], node : Node[A, K]) -> Unit
    å‘ę ¹é“¾č”Øę·»åŠ čŠ‚ē‚¹

    cascading_cut

    fn[A : Eq, K : Eq] cascading_cut(heap : FibHeap[A, K], node : Node[A, K]) -> Unit
    ēŗ§č”å‰Ŗåˆ‡ļ¼šå¤„ē†č¢«ę ‡č®°ēš„čŠ‚ē‚¹

    clear_children

    fn[A, K] clear_children(node : Node[A, K]) -> Unit
    ęø…é™¤čŠ‚ē‚¹ēš„ę‰€ęœ‰å­čŠ‚ē‚¹

    collect_node

    fn[A : Eq, K : Eq] collect_node(node : Node[A, K], result : Array[(A, K)]) -> Unit

    connect_right

    fn[A, K] connect_right(node : Node[A, K], right_node : Node[A, K]) -> Unit
    å®‰å…Øåœ°č®¾ē½®å·¦å³ęŒ‡é’ˆ

    consolidate

    fn[A : Eq, K : Eq] consolidate(heap : FibHeap[A, K]) -> Unit
    åˆå¹¶ę ¹é“¾č”Øäø­å…·ęœ‰ē›øåŒåŗ¦ę•°ēš„ę ‘

    create_node

    fn[A, K] create_node(value : A, key : K) -> Node[A, K]
    åˆ›å»ŗę–°čŠ‚ē‚¹

    cut

    fn[A : Eq, K : Eq] cut(heap : FibHeap[A, K], node : Node[A, K], parent : Node[A, K]) -> Unit
    å‰Ŗåˆ‡ę“ä½œļ¼šå°†čŠ‚ē‚¹ä»Žēˆ¶čŠ‚ē‚¹åˆ†ē¦»å¹¶ę·»åŠ åˆ°ę ¹é“¾č”Ø

    from_array

    fn[A : Eq, K : Eq] from_array(arr : Array[A], keys : Array[K], compare : (K, K) -> Int) -> FibHeap[A, K]

    ä»Žę•°ē»„åˆ›å»ŗę–ę³¢é‚£å„‘å †

    get_child

    fn[A, K] get_child(node : Node[A, K]) -> Node[A, K]
    å®‰å…Øåœ°čŽ·å–čŠ‚ē‚¹ēš„å­čŠ‚ē‚¹

    get_left

    fn[A, K] get_left(node : Node[A, K]) -> Node[A, K]
    å®‰å…Øåœ°čŽ·å–čŠ‚ē‚¹ēš„å·¦å…„å¼Ÿ

    get_parent

    fn[A, K] get_parent(node : Node[A, K]) -> Node[A, K]
    å®‰å…Øåœ°čŽ·å–čŠ‚ē‚¹ēš„ēˆ¶čŠ‚ē‚¹

    get_right

    fn[A, K] get_right(node : Node[A, K]) -> Node[A, K]
    å®‰å…Øåœ°čŽ·å–čŠ‚ē‚¹ēš„å³å…„å¼Ÿ

    get_value_and_key

    fn[A, K] get_value_and_key(node : Node[A, K]) -> (A, K)?
    å®‰å…Øåœ°čŽ·å–čŠ‚ē‚¹ēš„å€¼å’Œé”®

    is_less

    fn[A, K] is_less(heap : FibHeap[A, K], a : Node[A, K], b : Node[A, K]) -> Bool
    ęÆ”č¾ƒäø¤äøŖčŠ‚ē‚¹ēš„é”®å€¼

    is_marked

    fn[A, K] is_marked(node : Node[A, K]) -> Bool
    ę£€ęŸ„čŠ‚ē‚¹ę˜Æå¦č¢«ę ‡č®°
    fn[A : Eq, K : Eq] link(heap : FibHeap[A, K], child : Node[A, K], parent : Node[A, K]) -> Unit
    å°†å­čŠ‚ē‚¹é“¾ęŽ„åˆ°ēˆ¶čŠ‚ē‚¹

    make_singleton

    fn[A, K] make_singleton(node : Node[A, K]) -> Unit
    åˆå§‹åŒ–čŠ‚ē‚¹äøŗē‹¬ē«‹čŠ‚ē‚¹ļ¼ˆč®¾ē½®č‡Ŗå¾ŖēŽÆęŒ‡é’ˆļ¼‰

    mark_node

    fn[A, K] mark_node(node : Node[A, K]) -> Unit
    č®¾ē½®čŠ‚ē‚¹ēš„ę ‡č®°

    new

    fn[A, K] new(compare : (K, K) -> Int) -> FibHeap[A, K]

    åˆ›å»ŗäø€äøŖę–°ēš„ē©ŗę–ę³¢é‚£å„‘å †

    remove_from_list

    fn[A : Eq, K : Eq] remove_from_list(node : Node[A, K]) -> Unit
    å®‰å…Øåœ°ä»Žé“¾č”Øäø­ē§»é™¤čŠ‚ē‚¹

    remove_from_root_list

    fn[A : Eq, K : Eq] remove_from_root_list(heap : FibHeap[A, K], node : Node[A, K]) -> Unit
    ä»Žę ¹é“¾č”Øäø­ē§»é™¤čŠ‚ē‚¹

    reset_mark

    fn[A, K] reset_mark(node : Node[A, K]) -> Unit
    é‡ē½®čŠ‚ē‚¹ēš„ę ‡č®°

    Powered by MoonBit

    Site sourceReport issueModulesSkillsCLIBuild queueStatistics

    Ā© 2026 mooncakes.io