Esempio n. 1
0
 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)
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
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
Esempio n. 7
0
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
Esempio n. 8
0
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
Esempio n. 9
0
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
Esempio n. 10
0
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
Esempio n. 11
0
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
Esempio n. 12
0
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
Esempio n. 13
0
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
Esempio n. 14
0
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
Esempio n. 15
0
 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
Esempio n. 16
0
 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
Esempio n. 17
0
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
Esempio n. 18
0
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)
Esempio n. 19
0
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
Esempio n. 20
0
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)