def __init__(self, graph: UndirectedGraph, v_ini: Vertex): self.graph = graph self.v_ini = v_ini self.q = Fifo() self.seen = set() self.q.push((v_ini, v_ini)) self.seen.add(v_ini)
def recorredor_anchura(g: UndirectedGraph, source: "T", r: int, c: int): #Crear e inicializar matriz distances = [] for row in range(r): distances.append([0] * c) queue = Fifo() seen = set() #Añadir el punto inicial a la cola queue.push((source, source)) seen.add(source) while len(queue) > 0: u, v = queue.pop() #Miramos los sucesores del elemento extraido de la cola for s in g.succs(v): #Si no lo hemos visitado ya lo añadimos a los visitados y anotamos en la matriz su distancia al origen if s not in seen: seen.add(s) distances[s[0]][s[1]] = distances[v[0]][ v[1]] + 1 #En la posición del sucesor el antecesor mas 1 queue.push((v, s)) return distances
def BFS_wallbraker(g: UndirectedGraph, source: Vertex, distances: dict) -> Edge: seen = set() min = float('infinity') wall = ((-1, -1), (-1, -1)) queue = Fifo() queue.push((source, 0)) seen.add(source) while len(queue) > 0: vertex, n = queue.pop() u, v = vertex # Comprobacion de vecinos tras los muros for x, y in [(1, 0), (0, 1), (-1, 0), (0, -1)]: neigh = (u + x, v + y) if neigh not in g.succs((u, v)) and neigh in distances: tdistance = n + distances[neigh] if tdistance < min: min = tdistance wall = ((u, v), neigh) for suc in g.succs(vertex): if suc not in seen: seen.add(suc) queue.push((suc, n + 1)) return wall
def kruskal_final(g, v_ini, aristas): q = Fifo() seen = set() vertices = [] # q.push(v_ini) seen.add(v_ini) vertices.append(v_ini) # sucs = g.succs(v_ini) # # vecino1 = sucs.pop() # vecino2 = sucs.pop() vecino1 = min(g.succs(v_ini)) # if aristas[(v_ini, vecino1)] < aristas[(v_ini, vecino2)]: # vecino1 = vecino2 q.push(vecino1) seen.add(vecino1) while len(vertices) != len(g.V): v = q.pop() vertices.append(v) for suc in g.succs(v): if suc not in seen: seen.add(suc) q.push(suc) return vertices
class BreadthFirstSearch: def __init__(self, graph: UndirectedGraph, v_ini: Vertex): self.graph = graph self.v_ini = v_ini self.q = Fifo() self.seen = set() self.q.push((v_ini, v_ini)) self.seen.add(v_ini) def __iter__(self): return self def __next__(self): if len(self.q) == 0: raise StopIteration u, v = self.q.pop() for suc in self.graph.succs(v): if suc not in self.seen: self.seen.add(suc) self.q.push((v, suc)) return u, v
def BFS(g, source): queue = Fifo() seen = set() queue.push((source, source)) edges = [] while len(queue) > 0: u, v = queue.pop() edges.append((u, v)) seen.add(v) for suc in g.succs(v): if suc not in seen: queue.push((v, suc)) return edges
def recorredor_aristas_anchura(g: UndirectedGraph, v_inicial: Vertex) -> List[Edge]: aristas = [] queue = Fifo() seen = set() queue.push((v_inicial, v_inicial)) seen.add(v_inicial) while len(queue) > 0: u, v = queue.pop() aristas.append((u, v)) for suc in g.succs(v): if suc not in seen: seen.add(suc) queue.push((v, suc)) return aristas
def busca_tesoro_primero_anchura (laberinto,v_inicio): seen=set() queue=Fifo() queue.push(v_inicio) seen.add(v_inicio) while len(queue)>0: v=queue.pop() if v==v_tesoro: return v for succ in laberinto.succs(v): if succ not in seen: seen.add(succ) queue.push(succ) return None
def recorredor_aristas_anchura(grafo,v_inicial): aredges] seen=set() queue=Fifo() seen.add(v_inicial) queue.push((v_inicial,v_inicial)) while len(queue)>0: u,v=queue.pop() aredgesppend((u,v)) for succ in grafo.succs(v): if succ not in seen: seen.add(succ) queue.push((v,succ)) return aredges
def min_distances(g: UndirectedGraph, ini: Vertex) -> Dict: distances = {} queue = Fifo() seen = set() queue.push((ini, ini, 0)) seen.add(ini) while len(queue) > 0: u, v, n, = queue.pop() distances[v] = n for suc in g.succs(v): if suc not in seen: seen.add(suc) queue.push((v, suc, n + 1)) return distances
def recorredor_vertices_anchura(lab, v_inicial): vertices = [] queue = Fifo() seen = set() queue.push(v_inicial) seen.add(v_inicial) while len(queue) > 0: v = queue.pop() vertices.append(v) for suc in lab.succs(v): if suc not in seen: seen.add(suc) queue.push(suc) return vertices
def travel_vertex_width(grafo, v_inicial): edges = [] queue = Fifo() seen = set() queue.push(v_inicial) seen.add(v_inicial) while len(queue) > 0: v = queue.pop() edges.append(v) for suc in grafo.succs(v): if suc not in seen: seen.add(suc) queue.push(suc) return edges
def recorredor_vertices_anchura(grafo: "Graph<T>", v_inicial: "T") -> "List<T>": vertices = [] queue =Fifo() seen = set() queue.push(v_inicial) seen.add(v_inicial) while len(queue)>0: v = queue.pop() vertices.append(v) for suc in grafo.succs(v): if suc not in seen: seen.add(suc) queue.push(suc) return vertices
def mod_anchura(g: UndirectedGraph, source: tuple) -> list: visitados = set() n = int(sqrt(len(g.V))) matriz = [[0] * n for i in range(n)] queue = Fifo() queue.push((source, 0)) visitados.add(source) while len(queue) > 0: u, n = queue.pop() for sucx, sucy in g.succs(u): if (sucx, sucy) not in visitados: visitados.add((sucx, sucy)) matriz[sucx][sucy] = n + 1 queue.push(((sucx, sucy), n + 1)) return matriz
def edges_withd_path(graph: "UndirectedGraph", initial_v: Tuple[int, int], ma): queue = Fifo() seen = set() queue.push((initial_v, initial_v)) seen.add(initial_v) ma[initial_v[0]][initial_v[1]] = -1 while len(queue) > 0: u, v = queue.pop() ma[v[0]][v[1]] = ma[u[0]][u[1]] + 1 for suc in graph.succs(v): if suc not in seen: seen.add(suc) queue.push((v, suc)) return ma
def recorredor_aristas_anchura(g, source): aristas = [] seen = set() queue = Fifo() seen.add(source) queue.push((source, source)) while len(queue) > 0: u, v = queue.pop() aristas.append((u, v)) if v != target: for succ in g.succs(v): if succ not in seen: seen.add(succ) queue.push((v, succ)) return aristas
def recorredor_aristas_anchura(lab, v_inicial): aristas = [] queue = Fifo() seen = set() queue.push((v_inicial, v_inicial)) seen.add(v_inicial) while len(queue) > 0: u, v = queue.pop() aristas.append((u, v)) for suc in lab.succs(v): if suc not in seen: seen.add(suc) queue.push((v, suc)) return aristas
def shortest_path(g, source, target): def rec_aristas(u, v): cola.push((u, u)) visitados.add(u) while len(cola) > 0: u, v = cola.pop() aristas.append((u, v)) for suc in g.succs(v): if suc not in visitados: visitados.add(suc) cola.push((v, suc)) aristas = [] cola = Fifo() visitados = set() rec_aristas(source, target) return busca_camino(aristas, target)
def marca_pasos_desde(g: "UndirectedGraph<T>", v_entrada: "T") -> "Dictionary[Cells]->Int": CuentaPasos = {} CuentaPasos[v_entrada]=0 #Seguimos el modelo de recorre_vertices_anchura queue = Fifo() seen = set() queue.push(v_entrada) seen.add(v_entrada) while len(queue)>0: v=queue.pop() for suc in g.succs(v): if suc not in seen: queue.push(suc) seen.add(suc) #Los pasos de esa casilla son uno más que los de su padre CuentaPasos[suc]=CuentaPasos[v]+1 return CuentaPasos
def width_edges(g, source, matrix): #Recorre aristas y actualiza matriz de costes edges = [] seen = set() queue = Fifo() seen.add(source) queue.push((source, source)) matrix[source[0]][source[1]] = 0 while len(queue) > 0: u, v = queue.pop() edges.append((u, v)) for succ in g.succs(v): if succ not in seen: matrix[succ[0]][succ[1]] = matrix[v[0]][v[1]] + 1 seen.add(succ) queue.push((v, succ)) return (edges, matrix)