Exemple #1
0
 def test_merge_heaps_2(self):
     """Test merging empty and non-empty heap"""
     heap1 = fib_heap.FibonacciHeap()
     heap1.insert(4)
     heap1.insert(6)
     heap1.insert(7)
     heap2 = fib_heap.FibonacciHeap()
     new_heap = heap1.merge(heap2)
     assert new_heap.size == 3
     assert len(list(new_heap.root_nodes)) == 3
Exemple #2
0
def Dijkstra(G, s, t, INF=99999):
    dists = dict()
    prevs = dict()
    nodes = dict()
    S = fib_heap.FibonacciHeap()
    for v in G.g:
        if v == s:
            dists[v] = 0
        else:
            dists[v] = INF
        prevs[v] = None
        node_v = fib_heap.FibonacciHeap.Node(dists[v], v)
        nodes[v] = node_v
        S.Insert(node_v)
    v = S.extract_min().value
    while True:
        for u in G.g[v]:
            tmp = dists[v] + G.g[v][u]
            if tmp < dists[u]:
                dists[u] = tmp
                S.decrease_key(nodes[u], tmp)
                prevs[u] = v
        v = S.extract_min().value
        if v == t:
            break
    path = []
    while True:
        path.append(t)
        t = prevs[t]
        if t == s:
            return path + [s]
Exemple #3
0
	def __init__(self, size, initialize_with):
		if size != None:
			print "Bounded Fibonacci Heap currently unsupported!"
			sys.exit(1)
		super(priority_queue_FibHeap, self).__init__(size, initialize_with)
		self.l = fib_heap.FibonacciHeap()
		for item in initialize_with:
			self.add(item)
Exemple #4
0
 def test_insert(self):
     """Test inserting ino a non-empty Heap."""
     heap = fib_heap.FibonacciHeap()
     heap.insert(10)
     heap.insert(5)
     assert heap.min.key == 5
     assert len(list(heap.root_nodes)) == 2
     assert heap.size == 2
Exemple #5
0
 def test_extract_min(self):
     """Test extracting min value from heap."""
     heap = fib_heap.FibonacciHeap()
     heap.insert(10)
     heap.insert(5)
     heap.insert(3)
     heap.insert(100)
     assert heap.min.key == 3
     assert heap.size == 4
     min_node = heap.extract_min()
     assert min_node.key == 3
     assert heap.size == 3
     assert heap.min.key == 5
Exemple #6
0
def heap_from_slides():
    """Create Fib Heap from lecture slides."""
    root_nodes = parent = node.Node(24)
    sub_node = node.Node(26)
    sub_node.mark = True
    sub_node.parent = parent
    sub_node.insert(sub_node)
    n = node.Node(46)
    n.parent = parent
    sub_node.insert(n)
    sub_node.create_child(node.Node(35))
    parent.create_child(sub_node)
    sub_node.degree = 1
    parent.degree = 2
    parent = node.Node(17)
    parent.create_child(node.Node(30))
    parent.degree = 1
    root_nodes.insert(parent)
    parent = node.Node(3)
    min_node = parent
    sub_node = node.Node(18)
    sub_node.mark = True
    sub_node.insert(sub_node)
    sub_sub_node = node.Node(38)
    sub_sub_node.degree = 1
    sub_sub_node.create_child(node.Node(41))
    sub_sub_node.parent = sub_node
    sub_node.insert(sub_sub_node)
    n = node.Node(52)
    n.parent = sub_node
    sub_node.insert(n)
    n = node.Node(39)
    n.mark = True
    sub_node.create_child(n)
    parent.create_child(sub_node)
    sub_node.degree = 1
    parent.degree = 2
    root_nodes.insert(parent)
    root_nodes.insert(node.Node(7))
    root_nodes.insert(node.Node(23))

    heap = fib_heap.FibonacciHeap()
    heap.root_nodes = root_nodes
    heap.size = 14
    heap.min = min_node
    return heap
Exemple #7
0
 def test_insert_empty(self):
     """Test inserting into an empty Heap."""
     heap = fib_heap.FibonacciHeap()
     heap.insert(1)
     assert heap.min.key == 1
     assert heap.size == 1
Exemple #8
0
 def test_initial_size(self):
     """Initial size should be 0."""
     heap = fib_heap.FibonacciHeap()
     assert heap.size == 0
Exemple #9
0
 def test_initial_min(self):
     """Initial min should be nil."""
     heap = fib_heap.FibonacciHeap()
     assert heap.min is None