def prim(aGraph, startVertex): # create a priority queue that uses distance as the value to determine priority and thus its position # use the distance to the vertex as the priority because while exploring the next vertex, want to explore the vertex that has the smallest distance # decreaseKey method used when the distance to a vertex that is already in the queue is reduced, and thus moves that vertex toward the front of the queue. pQueue = PriorityQueue() # initialize the state of the graph for vertex in aGraph: # initialally all vertices values are = infinity (sys.maxint) because we assume the greatest value and then update appropiately vertex.setDistance(sys.maxsize) vertex.setPred(None) # distance represents distance from the startVertex, trivially 0 for the startVertex startVertex.setDistance(0) # key value pair # key is distance and vertex is the value pQueue.buildHeap([ (vertex.getDistance(), vertex) for vertex in aGraph ]) while not pQueue.isEmpty(): currentVertex = pQueue.delMin() # iterate over the currentVertex's edges for nextVertex in currentVertex.getConnections(): # calculate the weight from currentVertex to nextVertex newCost = currentVertex.getWeight(nextVertex) # found a shorter path # node is not considered to be part of the spanning tree until it is removed from the priority queue. # nextVertex in pQueue means that vertex is not yet in the spanning tree so it is safe to add (ensures an acyclic graph) if nextVertex in pQueue and newCost< nextVertex.getDistance(): # assign the predecessor appropiately nextVertex.setPred(currentVertex) # set a new distance on the nextVertex nextVertex.setDistance(newCost) # update the priorityQueue with the correct values pQueue.decreaseKey(nextVertex, newCost)
def dijkstra(graphObj, startVertex): pq = PriorityQueue() for vertex in graphObj.getVertices(): if vertex == startVertex: pq.insert([0, vertex]) graphObj.verticesList[startVertex].distance = 0 else: pq.insert([INFINITY, vertex]) while(len(pq.pqueue)): currentVertex = pq.extractMin() if len(pq.pqueue) == 1: break # print(pq.pqueue, pq.lookup) for adjNode in graphObj.verticesList[currentVertex[1]].getConnections(): newDistance = graphObj.verticesList[currentVertex[1]].distance + graphObj.verticesList[currentVertex[1]].adjList[adjNode] if newDistance < graphObj.verticesList[adjNode].distance: graphObj.verticesList[adjNode].distance = newDistance graphObj.verticesList[adjNode].predecessor = currentVertex[1] index = pq.lookup[adjNode] pq.decreaseKey(index, newDistance) return graphObj
def dijkstra(aGraph, startVertex): # create a priority queue that uses distance as the value to determine priority and thus its position # use the distance to the vertex as the priority because while exploring the next vertex, want to explore the vertex that has the smallest distance # decreaseKey method used when the distance to a vertex that is already in the queue is reduced, and thus moves that vertex toward the front of the queue. pQueue = PriorityQueue() # distance represents distance from the startVertex, trivially 0 for the startVertex # initialally all vertices values are = infinity (sys.maxint) because we assume the greatest value and then update appropiately startVertex.setDistance(0) # key value pair # key is distance and vertex is the value pQueue.buildHeap([ (vertex.getDistance(), vertex) for vertex in aGraph ]) while not pQueue.isEmpty(): currentVertex = pQueue.delMin() # iterate over the currentVertex's edges for nextVertex in currentVertex.getConnections(): # distance of current vertex and the weight of it's edges newDistance = currentVertex.getDistance() + currentVertex.getWeight(nextVertex) # found a shorter path if newDistance < nextVertex.getDistance(): # set a new distance on the nextVertex nextVertex.setDistance(newDistance) # assign the predecessor appropiately nextVertex.setPred(currentVertex) # update the priorityQueue with the correct values pQueue.decreaseKey(nextVertex, newDistance)
def Dijkstras(graph,start): pq=PriorityQueue() start.setDistance(0) pq.buildHeap([(v.getDistance(),v) for v in graph]) # distance is the key in the priority queue while not pq.isEmpty(): currentvertex=pq.delMin() for newvertex in currentvertex.getConnections(): newDist=currentvertex.getDistance()+currentvertex.getWeight(newvertex) if newDist<newvertex.getDistance(): # at the start the distance of all the vertices is set to maximum. That's why the if statement will be executed newvertex.setDistance(newDist) newvertex.setPredecessor(currentvertex) pq.decreaseKey(newvertex,newDist)
def dijkstra(aGraph, start): pq = PriorityQueue() start.setDistance(0) pq.buildHeap([(v.getDistance(), v) for v in aGraph]) while not pq.isEmpty(): currentVert = pq.delMin() for nextVert in currentVert.getConnections(): newDist = currentVert.getDistance() \ + currentVert.getWeight(nextVert) if newDist < nextVert.getDistance(): nextVert.setDistance(newDist) nextVert.setPred(currentVert) pq.decreaseKey(nextVert, newDist)
def prim(graph,start): # it belongs to the family of greedy algorithms pq=PriorityQueue() for v in graph: v.setDistance(sys.maxsize) v.setPredecessor(None) start.setDistance(0) pq.buildHeap([(v.getDistance(),v) for v in graph]) while not pq.isEmpty() currentvertex=pq.delMin() for newvertex in currentvertex.getConnections(): newDist=currentvertex.getWeight(newvertex) if newDist<newvertex.getDistance() and newvertex in pq: newvertex.setDistance(newDist) newvertex.setPredecessor(currentvertex) pq.decreaseKey(newvertex,newDist)
def dijkstra(g, start): # print('Finding shortest paths to ' + start.payload['city']) g.reset() pq = PriorityQueue() start.setDistance(0) pq.buildHeap([(v.getDistance(), v) for v in g]) while not pq.isEmpty(): currentVert = pq.delMin() for nextVert in currentVert.getConnections(): newDist = currentVert.getDistance() \ + currentVert.getCost(nextVert) if newDist < nextVert.getDistance(): # print('Setting {0}s predecessor to {1} (distance {2})'.format(nextVert.payload['city'], currentVert.payload['city'], round(newDist))) nextVert.setDistance(newDist) nextVert.parent = currentVert pq.decreaseKey(nextVert, newDist)
def dijkstra(self, labelStart: str, labelEnd: str): startVertex: Vertex = self.findVertexByLabel(labelStart) startVertex.weight = 0 pq = PriorityQueue() for vertex in self.vertices: pq.insert(vertex) while not pq.isEmpty(): min: Vertex = pq.deleteMin() for neighbour in self.adjacencyList[min.label]: # loop through self.vertices avoided, O(1) instead of O(V) # tmp: Vertex = self.findVertexByLabel(neighbour.label) tmp: Vertex = self.vertices[neighbour.index] if min.weight + neighbour.weight < tmp.weight: self.prev[neighbour.label] = min.label tmp.weight = min.weight + neighbour.weight pq.decreaseKey(tmp.key) print(self.showPath(labelEnd))
def prim(self, labelStart: str): result: str = "" startVertex: Vertex = self.findVertexByLabel(labelStart) startVertex.weight = 0 pq = PriorityQueue() for vertex in self.vertices: pq.insert(vertex) while not pq.isEmpty(): min: Vertex = pq.deleteMin() if self.prev[min.label] is not None: result += self.prev[min.label] + " -> " + min.label + ", " for neighbour in self.adjacencyList[min.label]: # loop through self.vertices avoided, O(1) instead of O(V) # tmp: Vertex = self.findVertexByLabel(neighbour.label) tmp: Vertex = self.vertices[neighbour.index] if neighbour.weight < tmp.weight: self.prev[neighbour.label] = min.label tmp.weight = neighbour.weight pq.decreaseKey(tmp.key) print(result)
def prim(G, start): pq = PriorityQueue() for v in G: v.setDistance(sys.maxsize) v.setPred(None) start.setDistance(0) pq.buildHeap([(v.getDistance(), v) for v in G]) print("\nOrder edges added in Prim's algorithm:") while not pq.isEmpty(): currentVert = pq.delMin() # extra print to trace order if currentVert.getPred() != None: print("Prim: edge", currentVert.getPred().getId(), "to", currentVert.getId()) for nextVert in currentVert.getConnections(): newCost = currentVert.getWeight(nextVert) # Printed textbook has incorrect "+ currentVert.getDistance()" term if nextVert in pq and newCost < nextVert.getDistance(): nextVert.setPred(currentVert) nextVert.setDistance(newCost) pq.decreaseKey(nextVert, newCost) print("\n")