def dijkstra(self, start_label, end_label): start_vertex = self.find_vertex_by_label(start_label) start_vertex.weight = 0 pq = PriorityQueue() pq.build_heap(self.vertices) while not pq.isEmpty(): min_vertex = pq.getMin() # return Vertex object for neighbour_vertex in self.adjacencyList[min_vertex.label]: tmp_vertex = self.find_vertex_by_label( neighbour_vertex.label) # vertex object from self.vertices if min_vertex.weight + neighbour_vertex.weight < tmp_vertex.weight: tmp_vertex.parent = min_vertex.label tmp_vertex.weight = min_vertex.weight + neighbour_vertex.weight pq.deleteMin() print(self.print_path(end_label))
def prim(self, start_label): start_vertex = self.find_vertex_by_label(start_label) start_vertex.weight = 0 pq = PriorityQueue() #for vertex in self.vertices: # pq.insert(vertex) pq.build_heap(self.vertices) while not pq.isEmpty(): min_vertex = pq.getMin() # return Vertex object if min_vertex.parent is not None: self.path += (min_vertex.parent + " -> " + min_vertex.label + ", ") for neighbour_vertex in self.adjacencyList[min_vertex.label]: tmp_vertex = self.find_vertex_by_label( neighbour_vertex.label) # vertex object from self.vertices if neighbour_vertex.weight < tmp_vertex.weight: tmp_vertex.parent = min_vertex.label tmp_vertex.weight = neighbour_vertex.weight pq.deleteMin() print(self.path)