Esempio n. 1
0
 def dijkstra(self, label: str):
     index: int = self.findIndexByLabel(label)
     self.vertices[index].weight = 0
     pq = PriorityQueue()
     pq.buildHeap(self.vertices)
     current: Vertex
     while not pq.isEmpty():
         current = pq.deleteMin()
         for neighbour in self.adjacencyList[current.label]:
             if current.weight + neighbour.weight < self.vertices[
                     neighbour.index].weight:
                 self.prev[self.vertices[
                     neighbour.index].label] = current.label
                 self.vertices[
                     neighbour.
                     index].weight = current.weight + neighbour.weight
                 pq.decreaseKey(self.vertices[neighbour.index].key)
 def prims(self, label: str):
     result: str = ""
     index: int = self.findIndexByLabel(label)
     self.vertices[index].weight = 0
     pq = PriorityQueue()
     pq.buildHeap(self.vertices)
     current: Vertex
     while not pq.isEmpty():
         current = pq.deleteMin()
         print(current.label)
         if self.prev[current.label] is not None:
             result += self.prev[
                 current.label] + " -> " + current.label + ", "
         for neighbour in self.adjacencyList[current.label]:
             if neighbour.weight < self.vertices[neighbour.index].weight:
                 self.prev[self.vertices[
                     neighbour.index].label] = current.label
                 self.vertices[neighbour.index].weight = neighbour.weight
                 pq.decreaseKey(self.vertices[neighbour.index].key)
     print(result)