def connected_components(G, diplay_dsf=False): S = dsf.DisjointSetForest(len(G)) for source in range(len(G)): for dest in G[source]: dsf.union_by_size(S, source, dest) if diplay_dsf: dsf.draw_dsf(S) return dsf.NumSets(S), S
def Choice_3(S, walls, num_cells): G = [] for i in range(num_cells): G.append([]) while CountSets(S) > 1: d = random.randint(0, len(walls) - 1) print('removing wall ', walls[d]) if dsf.find(S, walls[d][0]) != dsf.find(S, walls[d][1]): # dsf.union(S,walls[d][0],walls[d][1]) dsf.union_by_size(S, walls[d][0], walls[d][1]) poppedWall = walls.pop(d) G[poppedWall[0]].append(poppedWall[1]) G[poppedWall[1]].append(poppedWall[0]) dsf.draw_dsf(S) pic = draw_maze(walls, maze_rows, maze_cols) print() print("breadth_first_search") Path = breadth_first_search(G, 0) draw_path(pic, Path, num_cells - 1, maze_cols - .5, maze_rows - .5) print(Path) printPath(Path, num_cells - 1) return G
######################################### start = time.time() maze_rows = 8 maze_cols = 12 num_cells = maze_rows * maze_cols plt.close("all") #Maze creation walls = wall_list(maze_rows, maze_cols) draw_maze(walls, maze_rows, maze_cols, cell_nums=True) S = dsf.DisjointSetForest(num_cells) dsf.draw_dsf(S) print("n, the number of cells:", num_cells) x = input("Type m, the number of walls to remove:") print() #Choice m < n-1 if int(x) < num_cells - 1: print( "A path from source to destination is not guaranteed to exist (m < n -1)" ) Gr = Choice_1(S, walls, int(x)) print("depth_first_search Stack") path3 = Depth_first_search_Stack(Gr, 0) printPath(path3, num_cells - 1) print(path3)