def prim(G, start): pq = PriorityQueue() for v in G: v.setDistance(sys.maxsize) v.setPred(None) start.setDisatance(0) pq.build_heap([(v.getDistance(), v) for v in G]) while not pq.is_empty(): currentVert = pq.del_min() for nextVert in currentVert.getConnections(): newCost = currentVert.getWeight(nextVert) if nextVert in pq and newCost < nextVert.getDistance(): nextVert.setPred(currentVert) nextVert.setDisatance(newCost) pq.decrease_key(nextVert, newCost)
def dijkstra(aGraph, start): # Actually, it is a priority queue. pq = PriorityQueue() start.setDistance(0) # :meth:`biuld_heap` is O(logV) pq.build_heap([(v.getDistance(), v) for v in aGraph]) while not pq.is_empty(): # :meth:`del_min()` is O(logV), within the `while` loop, it is O(Vlog(V)) currentVert = pq.del_min() # The `for` loop is O(E) for nextVert in currentVert.getConnections(): newDist = currentVert.getDistance() + currentVert.getWeight( nextVert) if newDist < nextVert.getDistance(): nextVert.setDistance(newDist) nextVert.setPred(currentVert) # :meth:`decrease_key` is within `for` loop, it is O(Elog(V)) pq.decrease_key(nextVert, newDist)