def dijkstra(G, s):

    #Inicialização
    for node in G.nodes():
        G.node[node]['d'] = sys.maxsize
        G.node[node]['py'] = None
        G.node[node]['visited'] = False


    G.node[s]['d'] = 0
    
    S = [] #Guarda o caminho mais curto

    #Encontra aresta máxima
    max_weight = 0
    for edge in G.edges():
        if(G[edge[0], edge[1]]['weight'] > max_weight):
            max_weight = G[edge[0], edge[1]]['weight'] 

    #Inicializa vetor de buckets
    Q = [None] * (len(G.nodes())*max_weight)
    #Inicializa vertor de referência para saber em que bucket um vértice está
    V = [None] * G.nodes()

    #Coloca s no Bucket 0
    V[s] = 0
    
    u = s
    while u != None:
        G.node[u]['visited'] = True

        for v in G.neighbors(u):
            #Se ainda não foi marcado permanentemente ajusta distância no bucket correspondente
            if G.node[v]['visited'] = False:
                #Se já está em V, está em bucket, remove do bucket
                if not V[v] is None:
                    Q[G.node[v]['d']].remove(v)

                    #Se o bucket está vazio, remove bucket
                    if Q[G.node[v]['d']].head is None and Q[G.node[v]['d']].tail is None:
                        Q[G.node[v]['d']] = None
                
                #Calcula nova distância
                new_distance = G.node[u]['d'] + G.edge[u][v]['weight']
                
                #Se não existe bucket para nova distância, cria bucket
                if Q[new_distance] is None:
                    Q[new_distance] = dbl.DoubleLinkedList()

                #Aloca vértice no bucket correspondente a nova instância
                Q[new_distance].append(v)
                V[v] = new_distance

        #Seleciona novo vértice de distância mínima
        u = None

        for bucket in Q:
            if not bucket is None:
                u = bucket.remove_tail()
            break
Example #2
0
def test_push():
    test_list = double_linked_list.DoubleLinkedList()
    ordinary_list = []
    for i in range(1000):
        a = random.randint(-1000, 1000)
        test_list.push(a)
        ordinary_list += [a]

    assert double_linked_list_to_list(test_list) == ordinary_list
Example #3
0
def test_unshift():
    test_list = double_linked_list.DoubleLinkedList()
    ordinary_list = []
    for i in range(1000):
        a = random.randint(-1000, 1000)
        test_list.unshift(a)
        ordinary_list = [a] + ordinary_list

    assert double_linked_list_to_list(test_list) == ordinary_list
Example #4
0
def test_len():
    test_list = double_linked_list.DoubleLinkedList()
    ordinary_list = []
    for i in range(1000):
        a = random.randint(-1000, 1000)
        test_list.push(a)
        ordinary_list += [a]

    for i in range(random.randint(0, 999)):
        test_list.shift()
        ordinary_list = ordinary_list[1:]

    assert len(test_list) == len(ordinary_list)
Example #5
0
def test_last():
    test_list = double_linked_list.DoubleLinkedList()
    ordinary_list = []
    for i in range(1000):
        a = random.randint(-1000, 1000)
        test_list.push(a)
        ordinary_list += [a]

    for i in range(random.randint(0, 999)):
        test_list.shift()
        ordinary_list = ordinary_list[1:]

    assert test_list.last().element == ordinary_list[-1]
Example #6
0
def test_contains():
    test_list = double_linked_list.DoubleLinkedList()
    ordinary_list = []
    for i in range(1000):
        a = random.randint(-1000, 1000)
        test_list.push(a)
        ordinary_list += [a]

    for i in range(random.randint(0, 1000)):
        test_list.shift()
        ordinary_list = ordinary_list[1:]

    for i in range(1000):
        a = random.randint(-1000, 1000)
        if (a in test_list) != (a in ordinary_list):
            assert False
    assert True
Example #7
0
def test_delete():
    test_list = double_linked_list.DoubleLinkedList()
    ordinary_list = []
    for i in range(1000):
        a = random.randint(-1000, 1000)
        test_list.push(a)
        ordinary_list += [a]

    for i in range(random.randint(0, 1000)):
        test_list.shift()
        ordinary_list = ordinary_list[1:]

    for i in range(random.randint(0, 1000)):
        a = random.randint(-1000, 1000)
        while a in ordinary_list:
            ordinary_list.remove(a)

        test_list.delete(a)

    assert double_linked_list_to_list(test_list) == ordinary_list
Example #8
0
import double_linked_list as dll




ll_sample = dll.DoubleLinkedList()
'''
#Test Exception
ll_sample.delete_start()
ll_sample.delete_end()
ll_sample.traverse()
'''

#Test insert
ll_sample.insert_end('h')
ll_sample.insert_start('e')
ll_sample.insert_start('l')
ll_sample.insert_start('l')
ll_sample.insert_start('o')
ll_sample.traverse()
print('\n')
ll_sample.delete_end()
ll_sample.delete_end()
ll_sample.traverse()

print('\n')
ll_sample.delete_start()
ll_sample.delete_start()
ll_sample.traverse()

#Testing Exceptions - works
Example #9
0
 def __init__(self):
     self.__link = double_linked_list.DoubleLinkedList()
Example #10
0
 def __init__(self, size):
     self.cache_size = size
     self.cache = {}
     self.cache_vals = double_linked_list.DoubleLinkedList()
Example #11
0
def test_zero_delete():
    test_list = double_linked_list.DoubleLinkedList()
    test_list.unshift(1)
    test_list.pop()
    assert len(test_list) == 0
Example #12
0
def test_zero_unshift_shift():
    test_list = double_linked_list.DoubleLinkedList()
    test_list.unshift(1)
    test_list.shift()
    assert len(test_list) == 0
Example #13
0
def test_zero_push_pop():
    test_list = double_linked_list.DoubleLinkedList()
    test_list.push(1)
    test_list.pop()
    assert len(test_list) == 0
Example #14
0
def test_zero_len():
    test_list = double_linked_list.DoubleLinkedList()
    assert len(test_list) == 0
Example #15
0
 def __init__(self):
     """Init a new instance of a Deque."""
     self.dll = double_linked_list.DoubleLinkedList()
     self.length = 0