def dijkstra(g, start): # Distance is the priority in PQ pq = PriorityQueue() visited = set() # Redundant as weighs are always positive distance = {} # Store distance to nodes from start previous = {} # Store the previous node to reach a node # prepare the start node distance[start] = 0 # start to start have zero distance previous[start] = None # No previous for start # Add start to priority queue pq.add_with_priority(start, 0) while pq.length > 0: # PQ will have unvisited nodes node_id = pq.extract_min() node = g.vert_list[node_id] for neigh in node.get_neighs(): if neigh not in visited: n_dist = node.get_weight(neigh) # if distance to neigh is less through, node_id, update. if neigh not in distance or distance[node_id] + n_dist < distance[neigh]: distance[neigh] = distance[node_id] + n_dist previous[neigh] = node_id # Update/add neigh in PQ, with new distance # Here, decrease_priority will add as well as update. # If such is not available, we will have to prepare PQ with # all nodes with float('inf') distance, except start pq.decrease_priority(neigh, distance[neigh]) # Done processing the node. Mark it as visited visited.add(node_id) print(distance) print(previous)
def dijkstra(g, start): # Distance is the priority in PQ pq = PriorityQueue() visited = set() # Redundant as weighs are always positive distance = {} # Store distance to nodes from start previous = {} # Store the previous node to reach a node # Set all the weight to infinity for node_id in g.get_vertices(): distance[node_id] = float('inf') # infinity distance[start] = 0 # start to start have zero distance previous[start] = None # No previous for start # Add start to priority queue pq.add_with_priority(start, 0) while pq.length > 0: # PQ will have unvisited nodes node_id = pq.extract_min() node = g.vert_list[node_id] for neigh in node.get_neighs(): if neigh not in visited: n_dist = node.get_weight(neigh) # if distance to neigh is less through, node_id, update. if distance[node_id] + n_dist < distance[neigh]: distance[neigh] = distance[node_id] + n_dist previous[neigh] = node_id # Update/add neigh in PQ, with new distance pq.decrease_priority(neigh, distance[neigh]) # Done processing the node. Mark it as visited visited.add(node_id) print(distance) print(previous)
def prims_mst(g): visited = set() # visited set, already in MST distance = {} # edge-distance to add the node to MST previous = {} pq = PriorityQueue() start = 5 # Arbitrarily choose a start node. for node in g.get_vertices(): distance[node] = float('inf') previous[node] = None distance[start] = 0 pq.add_with_priority(start, 0) while pq.length > 0: node_id = pq.extract_min() visited.add(node_id) # Add node to MST node = g.vert_list[node_id] # Check all the neighbours and add them to PQ if needed. for neigh in node.get_neighs(): if neigh not in visited: # A node may be neigh of many nodes which are in MST if node.get_weight(neigh) < distance[neigh]: # Unlike dijkstra, just replace the distance with new edge. distance[neigh] = node.get_weight(neigh) previous[neigh] = node_id pq.decrease_priority(neigh, distance[neigh]) print(previous) print(distance)