Ejemplo n.º 1
0
class Cmplx1HeapTestCase(unittest.TestCase):
    def setUp(self):
        self.fh = FibonacciHeap()
        #
        # 17    24    23  7  21     3
        #  |    /\                / | \
        # 30  26  46            18 52 41
        #      |                 |     |
        #     35                39    44
        #
        self.elems = {
            v: HeapNode(v)
            for v in (30, 7, 35, 26, 46, 24, 23, 17, 21, 39, 18, 52, 44, 41, 3)
        }
        self.snd_min_elem = sorted(self.elems.values())[1]

        self.elems[41].link(self.elems[44])
        self.elems[18].link(self.elems[39])
        self.elems[3].link(self.elems[41])
        self.elems[3].link(self.elems[52])
        self.elems[3].link(self.elems[18])
        self.fh.add_tree(self.elems[3])
        self.fh.add_tree(self.elems[21])
        self.fh.add_tree(self.elems[7])
        self.fh.add_tree(self.elems[23])
        self.elems[26].link(self.elems[35])
        self.elems[24].link(self.elems[46])
        self.elems[24].link(self.elems[26])
        self.fh.add_tree(self.elems[24])
        self.elems[17].link(self.elems[30])
        self.fh.add_tree(self.elems[17])

        # Remove `3`
        self.fh.extract_min()
        self.exp_num_trees = 3

    def test_snd_min_elem(self):
        self.assertEqual(self.fh.min_elem, self.snd_min_elem)

    def test_trees(self):
        self.assertEqual(self.fh.trees, self.exp_num_trees)
Ejemplo n.º 2
0
class EmptyHeapTestCase(unittest.TestCase):
    def setUp(self):
        self.fh = FibonacciHeap()

    def test_min_elem(self):
        self.assertEqual(self.fh.min_elem, None)

    def test_trees(self):
        self.assertEqual(self.fh.trees, 0)

    def test_extract_min(self):
        self.assertEqual(self.fh.extract_min(), None)
Ejemplo n.º 3
0
class OneElemHeapTestCase(unittest.TestCase):
    def setUp(self):
        self.fh = FibonacciHeap()
        self.elem = HeapNode(random.randint(1, 100))
        self.fh.add_tree(self.elem)

    def test_min_elem(self):
        self.assertEqual(self.fh.min_elem, self.elem)

    def test_trees(self):
        self.assertEqual(self.fh.trees, 1)

    def test_extract_min(self):
        self.assertEqual(self.fh.extract_min(), self.elem)
Ejemplo n.º 4
0
class ManyElemHeapTestCase(unittest.TestCase):
    def setUp(self):
        self.fh = FibonacciHeap()
        self.num_elems = 30
        self.elems = [
            HeapNode(random.randint(1, 100)) for i in range(self.num_elems)
        ]
        self.min_elem = min(self.elems)
        for elem in self.elems:
            self.fh.add_tree(elem)

    def test_min_elem(self):
        self.assertEqual(self.fh.min_elem, self.min_elem)

    def test_trees(self):
        self.assertEqual(self.fh.trees, self.num_elems)

    def test_extract_min(self):
        self.assertEqual(self.fh.extract_min(), self.min_elem)
Ejemplo n.º 5
0
def dijkstra(vertice_list, src, num_ver):
    parent = []
    #marca todos os vertices como nao visitados
    for i in range(num_ver):
        parent.append(-1)
    #vertice inicial tem distancia 0.  Na 1 iteracao todos os demais estao setados com 'infinidade'
    vertice_list[src].dist = 0

    from fibheap import FibonacciHeap
    f = FibonacciHeap()
    f.insert(vertice_list[src].dist, src)
    #print("Nodes:",f.total_nodes)
    #pelo numero de vertices, escolha o vertice de menor distancia atualmente no grafo.
    #Na primeira iteracao sera o source.
    i = 0
    while (f.total_nodes != 0):
        #while(i<10):
        i += 1
        #print(i)
        v_min_node = f.extract_min()  # 1.1
        #print('key:',v_min_node.key)
        #print("Nodes:",f.total_nodes)
        v_min = vertice_list[v_min_node.value]

        if (v_min == None):
            continue
        v_min.visited = 1  #marque vertice minimo como ja visitado. Desse modo o 1.2 sera feito, pois nao sera mais contado
        #para cada vertice adjacente ao vertice minimo
        for key in v_min.adjs:  # 1.3
            adj_ver = vertice_list[
                v_min.adjs[key].id]  #pega o vertice adjacente
            #Se a distancia atual do vertice minimo, mais o seu peso, for menor que a distancia do vert adjacente
            if (adj_ver.visited == 0
                    and v_min.dist + v_min.weights[key] < adj_ver.dist):
                adj_ver.dist = v_min.dist + v_min.weights[
                    key]  #a distancia do vertice adjacente tera esse novo valor
                parent[
                    adj_ver.
                    id] = v_min.id  # a pos que era do vertice adjacente, sera do menor v agora
                f.insert(adj_ver.dist, adj_ver.id)