示例#1
0
def prim(agraph, start):
    """
    Prim's algorithm for minimum spanning tree
    Using min-heap data structure

    return a minimum spanning tree
    """
    # vertex of minimun spanning tree
    mst_vertex = []
    pq = PriorityQueue()
    for v in agraph:
        v.setDistance(sys.maxsize)
        v.setPred(None)
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(), v) for v in agraph])
    while not pq.isEmpty():
        u = pq.delMin()
        mst_vertex.append(u)
        for adjacent in u.getConnections():
            newcost = u.getWeight(adjacent)
            if adjacent in pq and newcost < adjacent.getDistance():
                adjacent.setPred(u)
                adjacent.setDistance(newcost)
                pq.decreaseKey(adjacent, newcost)

    # edges of minimum spanning tree
    mst = []
    for i in range(1, len(mst_vertex)):
        # u, v, cost
        mst.append(
            (mst_vertex[i - 1], mst_vertex[i], mst_vertex[i].getDistance()))

    return mst
示例#2
0
def dijkstra_pyds3(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)
示例#3
0
def prim(agraph, start):
    pq = PriorityQueue()
    for v in agraph:
        v.setDistance(sys.maxsize)
        v.setPred(None)
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(), v) for v in G])
    while not pq.isEmpty():
        u = pq.delMin()
        for adjacent in u.getConnections():
          newCost = u.getWeight(adjacent)
          if adjacent in pq and newCost < adjacent.getDistance():
              adjacent.setPred(u)
              adjacent.setDistance(newCost)
              pq.decreaseKey(adjacent, newCost)
示例#4
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)
示例#5
0
    def prim(self, start):
        # Ejecutamos el algoritmo in-place
        pq = PriorityQueue()
        for v in self:
            v.set_distance(sys.maxsize)
            v.set_pred(None)

        start.set_distance(0)
        pq.buildHeap([(v.get_distance(), v) for v in self])
        while not pq.isEmpty():
            currentVert = pq.delMin()
            for nextVert in currentVert.get_connections():
                newCost = currentVert.get_weight(nextVert)
                if nextVert in pq and newCost < nextVert.get_distance():
                    nextVert.set_pred(currentVert)
                    nextVert.set_distance(newCost)
                    pq.decreaseKey(nextVert, newCost)
示例#6
0
def dijkstra(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(pq)
    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)
        print(pq)
示例#7
0
def dijkstra(aGraph, start):
    """
	Find Single-Source shortest-paths on a weighted, directed graph
	Return shortest path
	aGraph: class Graph
	start: class Vertex
	"""
    pq = PriorityQueue()
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(), v) for v in aGraph])
    while not pq.isEmpty():
        u = pq.delMin()
        for adjacent in u.getConnections():
            newDist = u.dist + u.getWeight(adjacent)
            if adjacent.dist > newDist:
                adjacent.setDistance(newDist)
                adjacent.setPred(u)
                pq.decreaseKey(adjacent, newDist)
 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)