queue.push((v, suc)) return m def recorredor_aristas_anchura(grafo: "Graph<T>", v_inicial: "T") -> "List<(T,T)>": 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 grafo.succs(v): if suc not in seen: seen.add(suc) queue.push((v, suc)) return aristas if __name__ == '__main__': num_rows = 8 num_cols = 8 grafo_tablero = horse_graph(num_rows, num_cols) print(numero_casillas_alcanzables(grafo_tablero, (0, 3))) viewer = Graph2dViewer(grafo_tablero, window_size=(400, 400)) viewer.run()
def casillas_alcanzables(grafo, vertice_inicial): return len(recorredor_aristas_anchura(grafo, vertice_inicial)) def matriz_saltos(g, num_rows, num_cols): m = [ ] #m es una matriz de distancias, donde cada casilla es la distancia de saltos al origen del caballo #porque cada arco del grafo que nos pasa la funcion de matriz de saltos es un salto for r in range(num_rows): m.append([0] * num_cols) #copiar el codigo del recorredor de aristas en anchura y modificarlo para rellenar a la vez la matriz m ################################################################################################################ #IMPLEMENTAR ################################################################################################################ return m if __name__ == '__main__': num_rows = 8 num_cols = 8 grafo_tablero = horse_graph(num_rows, num_cols) print(casillas_alcanzables(grafo_tablero, (0, 3))) viewer = Graph2dViewer(grafo_tablero, window_size=(600, 600), vertexmode=Graph2dViewer.ROW_COL) viewer.run()
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 num_achievable_spaces(graph, v_initial): return len(travel_vertex_width(graph, v_initial)) if __name__ == '__main__': num_rows = 8 num_cols = 8 table_graph = horse_graph(num_rows, num_cols) source = (0, 3) print("Número de casillas alcanzable desde", source, ": ", num_achievable_spaces(table_graph, source)) matrix = jump_matrix(table_graph, num_rows, num_cols) print(format_matrix(matrix)) viewer = Graph2dViewer(table_graph, window_size=(400, 400), vertexmode=Graph2dViewer.ROW_COL) viewer.run()
for fil, col in vertexes: for sr, sc in [(1, -2), (2, -1), (2, 1), (1, 2)]: if sr + fil < rows and 0 <= col + sc < cols: edges.append(((fil, col), (fil + sr, col + sc))) return UndirectedGraph(E=edges, V=vertexes) def BFS_modified(g, source): n = int(sqrt(len(g.V))) matrix = [[0] * n for i in range(n)] queue = Fifo() seen = set() queue.push((source, 0)) while len(queue) > 0: (x,y), level = queue.pop() matrix[x][y] = level seen.add((x,y)) for suc in g.succs((x, y)): if suc not in seen: queue.push((suc, level + 1)) return matrix if __name__ == "__main__": g = hourse_graph(4, 4) viewer = Graph2dViewer(g, vertexmode=Graph2dViewer.ROW_COL) matrix = BFS_modified(g, (0, 0)) print(matrix) viewer.run()
edges: List[Edge] = [] for r, c in vertices: for (ir, ic) in [(-2, -1), (-1, -2), (-2, 1), (-1, 2)]: if 0 <= r + ir < num_rows and 0 <= c + ic < num_cols: edges.append(((r, c), (r + ir, c + ic))) return UndirectedGraph(V=vertices, E=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 if __name__ == "__main__": g = knight_graph(num_rows=8, num_cols=8) print(len(g.V), len(g.E)) #lv = recorredor_aristas_anchura(g, (0,)) lv = Graph2dViewer(g, (1200, 800)) lv.run()