Exemplo n.º 1
0
def dijkstra_shortest_paths(adj_list, node_from, infinity_value=0xFFFFFFFF):
    distances = {k: infinity_value for k in get_nodes(adj_list)}
    distances[node_from] = 0

    visit_queue = IndexedHeap(distances)

    next_edge, distance = visit_queue.pop_first()
    while next_edge is not None:
        for edge in get_edges_from(adj_list, next_edge):
            if edge[0] in visit_queue and (distances[edge[0]] >
                                           distance + edge[1]):
                distances[edge[0]] = distance + edge[1]
                visit_queue.update({edge[0]: distances[edge[0]]})
        next_edge, distance = visit_queue.pop_first()

    return distances
Exemplo n.º 2
0
def test_heap_update():
    test_values = {1: 'A', 2: 'B', 3: 'C'}

    heap = IndexedHeap()
    heap.update(test_values)
    assert are_sorted_heaps_equal(
        heap,
        IndexedHeap(test_values),
    ), 'Two heaps should be equal'

    heap.update(test_values)
    heap.update({3: 'X', 4: 'Z'})
    assert are_sorted_heaps_equal(
        heap,
        IndexedHeap({1: 'A', 2: 'B', 3: 'X', 4: 'Z'}),
    ), 'Two heaps should be equal'