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 check_maze_uc(S, w, mc, mr, m, case): if case == 1 or case == 2: while m != 0: dw = random.randint(0, len(w) - 1) #dw: wall to remove c1 = dsf.find_c(S, w[dw][0]) #c1: cell 1 c2 = dsf.find_c(S, w[dw][1]) #c2: cell 2 #If the two cells are from different cells than remove a wall so to allow a path and combine them if c1 != c2: del w[dw] dsf.union_by_size(S, c1, c2) m -= 1 return w
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
m -= 1 return w else: sets = num_sets(S) if m == len(w) or m > len(w): return w.clear() while m != 0: dw = random.randint(0, len(w) - 1) c1 = dsf.find_c(S, w[dw][0]) c2 = dsf.find_c(S, w[dw][1]) if c1 != c2: del w[dw] dsf.union_by_size(S, c1, c2) m -= 1 sets -= 1 if sets == 1: del w[dw] m -= 1 return w #Checks to see if each cell has a simple path to another cell #Uses union by size and path compression def check_maze_uc(S, w, mc, mr, m, case): if case == 1 or case == 2: while m != 0: