Question

I was trying to learn about fibonacci heaps, the pseudocode for inserting an element in the heap was:

Fibonacci-Heap-Insert(H,x)
degree[x] := 0
p[x] := NIL
child[x] := NIL
left[x] := x
right[x] := x
mark[x] := FALSE
concatenate the root list containing x with root list H
if min[H] = NIL or key[x]<key[min[H]]
      then min[H] := x
n[H]:= n[H]+1

Here are some things i did not understand,

  • What is root list containing x and how to concatenate it with root list containing H?

While extracting min we do something like this:

    Fibonacci-Heap-Extract-Min(H)
     z:= min[H]
     if x <> NIL
         then for each child x of z
            do add x to the root list of H
                p[x]:= NIL
            remove z from the root list of H
            if z = right[z]
                then min[H]:=NIL
            else
                  min[H]:=right[z]
                  CONSOLIDATE(H)
            n[H] := n[H]-1
     return z

(Here are the other functions, consolidate and link, http://www.cse.yorku.ca/~aaw/Jason/FibonacciHeapAlgorithm.html)

  • In the previous insert function, we set child, and p of x as nil, the while extracting min, if <> nil will always be false and so it will never give and accurate min if we call the function several times.

  • If this structure is called a Fibonacci 'heap', where does it maintain the heap property?

  • If we use a binary heap in Dijkstra's Algorithm instead of a fibonacci heap will the time taken be almost as slow as it will be if we use an array or a linked list?

Can anyone explain the difficulties I have? Thank you.

Was it helpful?

Solution

You have a lot of questions here, so I'm going to do my best to answer all of them.

For your first question: the Fibonacci heap is implemented as a collection of trees all linked together in a circularly, doubly-linked list. When the insert function is asking you to concatenate x with root list H, it's asking you to take a singleton node you've created (x) and wire it in to the circular, doubly-linked list of all the existing trees H.

For your question about extract-min, there is an error in your pseudo code. The test

x <> NIL

Should be

z <> NIL

x isn't defined at that point in the function, so there's no way that this code could possibly work as written.

As to how the heap property is maintained: each individual tree in the Fibonacci heap individually obeys the heap property. When CONSOLIDATE is called, multiple trees can be merged back together into individual trees. This is where the heap property is maintained. When merging together two trees, the Fibonacci heap always takes the tree with the greater root value and makes it a child of the root of the tree whose root has the lesser value. That way, the resulting tree obeys the heap property.

Finally, Dijkstra's and heaps: Dijkstra's algorithm with a heap-based priority queue takes time O(m log n) to complete, while a Fibonacci-heap backed Dijkstra's takes O(m + n log n), which is asymptotically faster for sparse graphs. However, in practice, Fibonacci heaps tend to be slower than binary heaps due to higher constant factors hidden in the big-O terms, both because the implementation is so much more complicated and because they have worse locality of reference. The binary heap version of Dijkstra's, however, is dramatically faster in theory and in practice than a linked-list or sorted-array backed version of Dijkstra's.

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top