def UCS(self, nodoInicial, nodoFinal): unaColaPrioridad = monticulo.PriorityQueue() unaColaPrioridad.insertarElemento((0, nodoInicial, nodoInicial)) #una vez que se inicializa la cola de prioridad se chequea que no este vacia while (not (unaColaPrioridad.empty())): #prueba recorrido # print(unaColaPrioridad.queue) node = unaColaPrioridad.eliminarElemento() #si se llega al nodo final if (node[NODE] in nodoFinal): # print("Se llego al nodo final") print("Recorrido al nodo final: " + node[RECORRIDO] + " Costo: " + str(node[COSTO])) break #sino recorre los nodoInicio elif (self.visitedNodes[node[NODE]]): continue #sino recorre los nodos hijos else: self.nodoVisitado(node[NODE]) nodosHijos = self.conecciones[node[NODE]] for nodoHijo in nodosHijos: if (self.visitedNodes[nodoHijo[NODE]] is not True): unaColaPrioridad.insertarElemento( (nodoHijo[COSTO] + node[COSTO], nodoHijo[NODE], node[RECORRIDO] + "->" + str(nodoHijo[NODE]))) else: continue
def UCS(self, root, goal): pq = h.PriorityQueue() pq.enqueue((0, root, root)) # (priority, currnode, path) while ( not (pq.empty()) ): # iterates till queue is empty otherwise until all nodes are exhausted print(pq.queue) node = pq.dequeue() if ( node[NODE] in goal ): # if the current node in queue is equal to goal then we have print path print(node[PATH] + " Cost: " + str(node[PRIORITY])) # prints path break elif (self.visitedNodes[node[NODE]]): continue #next iteration node has already been expanded else: self.visitNode(node[NODE]) children = self.edges[node[NODE]] for childnode in children: if (self.visitedNodes[childnode[NODE]] is not True): pq.enqueue((childnode[PRIORITY] + node[PRIORITY], childnode[NODE], node[PATH] + "->" + str(childnode[NODE]) )) # enques all of the node children else: continue
def __init__(self): # graph = { # 'A': ['B', 'S', 'C'], # 'B': ['A'], # 'C': ['D', 'E', 'F', 'S', 'A'], # 'D': ['C'], # 'E': ['C', 'H'], # 'F': ['C', 'G'], # 'G': ['F', 'S'], # 'H': ['E', 'G'], # 'S': ['A', 'C', 'G'] # } self.graph = { 'A': ['B', 'D'], 'B': ['A', 'C'], 'C': ['B', 'D'], 'D': ['A', 'C'], # 'H': ['G'], # 'G': ['H'], } # self.weightedGraph = { # 'A': {'A': -1, 'B': 5, 'D': 3, 'E': 12, 'F': 5}, # 'B': {'B': -1, 'A': 5, 'D': 1, 'G': 2}, # 'C': {'C': -1, 'G': 2, 'E': 1, 'F': 16}, # 'D': {'D': -1, 'B': 1, 'G': 1, 'E': 1, 'A': 3}, # 'E': {'E': -1, 'A': 12, 'D': 1, 'C': 1, 'F': 2}, # 'F': {'F': -1, 'A': 5, 'E': 2, 'C': 16}, # 'G': {'G': -1, 'B': 2, 'D': 1, 'C': 2} # } self.weightedGraph = { 'A': {'A': -1, 'B': 1, 'D': 2}, 'B': {'B': -1, 'A': 1, 'C': 7}, 'C': {'C': -1, 'D': 4, 'B': 7}, 'D': {'D': -1, 'A': 2, 'B': 3, 'C': 4} } self.vertexCount = 7 self.priorityQueue = Heap.PriorityQueue(self.vertexCount) # allocate a heap tree for priority Queue self.MAXINT = sys.maxsize # positive inf value # self.MININT = -sys.maxsize - 1 # negative inf value self.weightedGraphList = list() self.weightedGraphDict = dict() self.disjointSet = list() self.MST = []
def UCS(self, initialNode, nodeFinal): Priority = hill.PriorityQueue() Priority.insertElement((0, initialNode, initialNode)) while (not (Priority.empty())): node = Priority.removeElement() if (node[NODE] in nodeFinal): print("Final Node is : " + node[PATH] + " Cost is : " + str(node[COST])) break elif(self.visitedNodes[node[NODE]]): continue else: self.VisitedNode(node[NODE]) childNodes = self.connections[node[NODE]] for nodeChild in childNodes: if (self.visitedNodes[nodeChild[NODE]] is not True): Priority.insertElement((nodeChild[COST] + node[COST], nodeChild[NODE], node[PATH] + "->" + str(nodeChild[NODE]))) else: continue