Beispiel #1
0
def dijkstra(aGraph, start):
    pq = PriorityQueue()
    start.set_distance(0)
    pq.build_heap([(v, v.get_distance()) for v in aGraph])
    while not pq.is_empty():
        current_vert = pq.dequeue()[0]
        for next_vert in current_vert.get_connections():
            new_dist = current_vert.get_distance() + current_vert.get_weight(
                next_vert)
            if new_dist < next_vert.get_distance():
                next_vert.set_distance(new_dist)
                next_vert.set_pred(current_vert)
                pq.decrease_priority(next_vert, new_dist)
Beispiel #2
0
def prim(aGraph, start):
    pq = PriorityQueue()
    for v in aGraph:
        v.set_distance(maxsize)
        v.set_pred(None)
    start.set_distance(0)
    pq.build_heap([(v, v.get_distance()) for v in aGraph])
    while not pq.is_empty():
        current_vert = pq.dequeue()[0]
        for next_vert in current_vert.get_connections():
            new_cost = current_vert.get_weight(next_vert)
            if next_vert in pq and new_cost < next_vert.get_distance():
                next_vert.set_distance(new_cost)
                next_vert.set_pred(current_vert)
                pq.decrease_priority(next_vert, new_cost)