Example #1
0
def a_star(G: Graph, start: int, stop: int):
    H = G.H
    open = BinaryHeap()
    for succ in G.get_succesors(start):
        open.add([G[start][succ] + H[succ][stop], start, succ])
    closed = Map(start, len(G))
    while True:
        f, u, x = open.get()
        g = f - H[x][stop]
        if g < closed[x][0]:
            closed[x] = (round(g, 2), u)
            for ng in G.get_succesors(x):
                if ng not in closed:
                    open.add((closed[x][0] + G[x][ng] + H[ng][stop], x, ng))
        if x == stop:
            return closed.reconstruct_path(start, stop)
Example #2
0
class TestBinaryHeap(unittest.TestCase):
    def setUp(self):
        self.heap = BinaryHeap()
        self.replayMemory = ReplayMemory(10, 32, 4, 84, 84)

    def test_Add(self):
        totalNo = 10
        for i in range(totalNo):
            state = np.zeros((84, 84), dtype=np.int)
            state.fill(i)
            td = i
            
            addedIndex = self.replayMemory.add(0, 0, state, 0)
            self.heap.add(addedIndex, td)
            
        for i in range(totalNo):
            topItem = self.heap.getTop()
            self.assertEqual(totalNo - i - 1, topItem[0])
            self.heap.remove(0)
Example #3
0
def make_tree(freq_table):
    """
    Constructs and returns the Huffman tree from the given frequency table.
    """

    trees = BinaryHeap()
    trees.add(TreeLeaf(None), 1)
    for (symbol, freq) in freq_table.items():
        trees.add(TreeLeaf(symbol), freq)

    while len(trees) > 1:
        right, rfreq = trees.pop_min()
        left, lfreq = trees.pop_min()
        trees.add(TreeBranch(left, right), lfreq + rfreq)

    tree, _ = trees.pop_min()
    return tree
Example #4
0

print("\nBinomialHeap")
test(BinomialHeap, lst)

print("\nBinaryHeap")
test(BinaryHeap,lst)

print("\nQueue")
test(Queue,lst)

print('\n')
brh = BinaryHeap()
brh_time = timer()
for dst, u, v in lst:
    brh.add([dst, u, v])
brh_time = timer() - brh_time

blh = BinomialHeap()
blh_time = timer()
for dst, u, v in lst:
    blh.add([dst, u, v])
blh_time = timer() - blh_time

sq = Queue()
q_time = timer()
for dst, u, v in lst:
    sq.add([dst, u, v])
q_time = timer()-q_time

print(f"Binary Heap:   {brh_time}\nBinomial Heap: {blh_time}\n" +