GithubHelp home page GithubHelp logo

fibonacci-heap-python's Introduction

Fibonacci heaps

Using Fibonacci heaps for priority queues improves the asymptotic running time of important algorithms, such as Dijkstra's algorithm for computing the shortest path between two nodes in a graph, compared to the same algorithm using other slower priority queue data structures.

Heap functions

  • find_min() runs in O(1) time
  • extract_min() runs in O(log n) time
  • insert(k) runs in O(1) time
  • merge(h) runs in O(1) time
  • decrease_key(x, k) runs in O(1) time

More info on the amortized analysis and running times can be found here or on Wikipedia.

Example

Test out the code live on repl.it

f = FibonacciHeap()

f.insert(10)
f.insert(2)
f.insert(15)
f.insert(6)

m = f.find_min()
print m.data # 2

q = f.extract_min()
print q.data # 2

q = f.extract_min()
print q.data # 6

f2 = FibonacciHeap()
f2.insert(100)
f2.insert(56)

f3 = f.merge(f2)
x = f3.root_list.right # pointer to random node
f3.decrease_key(x, 1)

# print the root list using the iterate class method
print [x.data for x in f3.iterate(f3.root_list)] # [10, 1, 56]

q = f3.extract_min()
print q.data # 1

Results

Quote from the original paper:

They are complicated when it comes to coding them. Also they are not as efficient in practice when compared with the theoretically less efficient forms of heaps, since in their simplest version they require storage and manipulation of four pointers per node, compared to the two or three pointers per node needed for other structures

I decided to test out my implementation of the Fibonacci heap vs. the heapq algorithm module in Python which implements a basic binary heap using array indexing for the nodes.

The Fibonacci heap did in fact run more slowly when trying to extract all the minimum nodes. You can see the comparison run times on repl.it for yourself.

Note: I performed some basic inserts and extracted the minimum several times to see which data structure was more efficient, which isn't the best test for analyzing the running time. For a more thorough analysis on applying the Fibonacci heap to the shortest path algorithm, check out this answer on Stack Overflow.

Running times

Initilaze both heaps with some data:

from heapq import *
from random import randint

f = FibonacciHeap()
h = []
n = 100
for i in xrange(0, n):
    r = randint(1, 1000)
    f.insert(r)
    heappush(h, r)

The code to extract the min from both heaps and print the running time:

import time

# test fib heap running time 
start_time = time.time()
while f.total_nodes > 0:
    m = f.extract_min()
print "%s seconds run time for fib heap" % (time.time() - start_time)

# test heapq running time 
start_time = time.time()
while h:
    m = heappop(h)
print "%s seconds run time for heapq" % (time.time() - start_time)

n = 100

0.0109820365906 seconds run time for fib heap
0.000254154205322 seconds run time for heapq

n = 500

0.0828540325165 seconds run time for fib heap
0.00195384025574 seconds run time for heapq

n = 1000

0.19855594635 seconds run time for fib heap
0.00374603271484 seconds run time for heapq

Note

Michael Fredman, one of the creators of the Fibonacci heap, was one of my professors in college. His class was very difficult.

fibonacci-heap-python's People

Contributors

agnishom avatar danielborowski avatar doktormerlin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fibonacci-heap-python's Issues

Question on the comparison with binary-heap?

Hey buddy,

Nice work!

I have a question and I hope that it is not something stupid.

The python heapq implantation (binary heap using arrays) is actually done in C, yours Fibonacci heap is pure python, so I think that comparison is not fair.

What are your thoughts on this?

AttributeError: 'NoneType' object has no attribute 'data'

Hi Daniel,

I tried to test the algorithm with some different numbers, I faced the "AttributeError: 'NoneType' object has no attribute 'data'". basically, when you have many same key nodes, this error happens. Do you maybe know why this is happening? (It removes the node from child list other than the rootlist.)

https://repl.it/Bouq/13

f = FibonacciHeap()
seed(4333)
# insert elements
for i in xrange(0, 1000): #tried to do for 1000 nodes
    f.insert(randint(1,500)) #random number between 1-500
    
# print the root list
print [x.data for x in f.iterate(f.root_list)]

# extract min
while f.total_nodes > 0:
    m = f.extract_min()
    print 'min is: ' + str(m.data)

'NoneType' object has no attribute 'key' error in decrease_key

I'm using fibonacci heaps for a priority queue in a min cut algorithm. I've been able to successfully implement it with a couple of test graphs, but suddenly for one of my tests I'm getting this issue in decrease_key:

Traceback (most recent call last):
File "TSPSolver.py", line 248, in
main('test2.txt')
File "TSPSolver.py", line 244, in main
tsp = remove_subtours(tsp,G)
File "TSPSolver.py", line 200, in remove_subtours
constraint_edges, min_cut_nodes = find_subtour(G, model.X)
File "TSPSolver.py", line 182, in find_subtour
min_cut_nodes, min_cut_value = min_cut(G_x)
File "TSPSolver.py", line 156, in min_cut
cut_of_phase, cut_nodes, G = min_cut_phase(G)
File "TSPSolver.py", line 144, in min_cut_phase
to_search = update_fibheap(G,to_search, heap_nodes, a.value)
File "TSPSolver.py", line 124, in update_fibheap
fibheap.decrease_key(node,node.key - change_in_weight)
File "/Users/ArluahsMAC/Desktop/CAAM471PTSD/fib_heap.py", line 76, in decrease_key
if x.key < self.min_node.key:
AttributeError: 'NoneType' object has no attribute 'key'

Any idea why this might be happening?
Thanks!

IndexError in consolidate function

In some cases there will be an IndexError in the consolidate function in line 122:
A = [None] * int(math.log(self.total_nodes) * 2)
int(math.log(self.total_nodes) * 2) is supposed to be the maximum number of distinct node degrees. This is however off by one and a bit. The node degree d is bound by where is the golden ratio (See Wikipedia). Doing the math I got . So the 2 needs to be replaced with something like 2.08. The number of possible distinct node degrees is one higher than this since nodes with a degree of zero need to be considered. The solution is to replace:
A = [None] * int(math.log(self.total_nodes) * 2)
with
A = [None] * int(math.log(self.total_nodes) * 2.08 + 1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.