예제 #1
0
def build_tree(freq):
    T = MinHeap([[(freq[symbol], symbol)] for symbol in freq])
    while len(T) > 1:
        left_tree = T.extract_opt()
        right_tree = T.extract_opt()
        T.add([(left_tree[0][0] + right_tree[0][0], ), left_tree, right_tree])
    return T
예제 #2
0
 def solve_minimization(initial_ps: BabPartialSolution) -> Solution:
     heap = MinHeap()
     heap.add(initial_ps)
     bps = initial_ps.pes()
     while len(heap) > 0:
         best_ps = heap.extract_opt()
         if best_ps.is_solution():
             return best_ps.get_solution()
         for new_ps in best_ps.successors():
             bps = min(bps, new_ps.pes())
             if new_ps.opt() <= bps:
                 heap.add(new_ps)
예제 #3
0
def cuerdas(v):
    minheap = MinHeap()
    for elem in v:
        minheap.add(elem)
    coste = 0
    while len(minheap) > 1:
        c1 = minheap.extract_opt()
        c2 = minheap.extract_opt()
        union = c1 + c2
        coste += union
        minheap.add(union)

    return coste
예제 #4
0
 def test_MinHeap(self):
     mh = MinHeap(list(self.minh2))
     self.assertEqual(list(self.minh2), list(mh))