def Maze2(rows, columns, wallList): n = rows * columns #number of cells print("there are ", n, "cells ") m = input("How many walls would you like to remove") m = int(m) s = dsf.DisjointSetForest(n) #a if m < n - 1: print( "A path from source to destination is not guaranteed to exist (when m < n − 1)" ) #b if m == n - 1: print( "The is a unique path from source to destination (when m = n − 1)") #c if m > n - 1: print( "There is at least one path from source to destination (when m > n − 1)" ) if m > len(wallList): while m > 0: x = random.randint(0, len(wallList) - 1) wallList.pop(x) m -= 1 return wallList while m > 0: curr = random.randint(0, len(wallList) - 1) wall = wallList[curr] if dsf.find(s, wall[0]) != dsf.find(s, wall[1]): dsf.union(s, wall[0], wall[1]) wallList.pop(curr) m -= 1 return wallList
def dsfMaze(rows,columns,wallList): cells = rows * columns s = dsf.DisjointSetForest(cells) while dsf.NumSets(s)> 1: curr = random.randint(0,len(wallList)-1) wall = wallList[curr] if dsf.find(s,wall[0]) != dsf.find(s,wall[1]): dsf.union(s, wall[0], wall[1]) wallList.pop(curr)
def RemoveWalls(Set, walls): #This method removes walls until all celss are part of the same set. while CheckSet(M) is not True: #Here we choose a random wall d = random.randint(0, len(walls) - 1) #If the cells are not in the same set, we remove the wall keeping them apart. if SameSet(Set, walls, d) == False: #NOTE: For this particular submission, I used union without compression. dsf.union(Set, walls[d][0], walls[d][1]) walls.pop(d) return
def adjListdsfMaze(rows, columns, wallList): cells = rows * columns s = dsf.DisjointSetForest(cells) G = [[] for i in range(len(cells))] while dsf.NumSets(s) > 1: curr = random.randint(0, len(wallList) - 1) wall = wallList[curr] if dsf.find(s, wall[0]) != dsf.find(s, wall[1]): dsf.union(s, wall[0], wall[1]) newEntry = wallList.pop(curr) G[newEntry[0]].append(newEntry[1]) return G
def remove_walls(W, sets, Print): numSets = dsf.num_of_sets(sets) start = dt.now() while numSets > 1 and W is not None: d = random.randint(0, len(walls) - 1) #print(numSets) if (dsf.in_same_set(sets, walls[d][0], walls[d][1])) == False: dsf.union(sets, walls[d][0], walls[d][1]) numSets -= 1 walls.pop(d) stop = dt.now() - start if Print == True: draw_maze(walls, maze_rows, maze_cols) print('Without compression, wall removal took ', stop, ' to complete.')
def remove_x_walls(W, Print, maze_rows, maze_cols, x): sets = dsf.DisjointSetForest(maze_rows * maze_cols) start = dt.now() for i in range(x): d = random.randint(0, len(W) - 1) dsf.union(sets, W[d][0], W[d][1]) W.pop(d) stop = dt.now() - start if Print == True: draw_maze(W, maze_rows, maze_cols, True) print('wall removal took ', stop, ' to complete.') return W
def primsMinTree(AL): forest = dsf.dsf(len(AL)) AL2 = [[] for i in range(len(AL))] heap = [] vertex = 0 for i in range(len(AL) - 1): for j in range(len(AL[vertex])): HeapInsert(heap, [vertex] + AL[vertex][j]) while True: pointWeight = ExtractMin(heap) if pointWeight == None: break if forest[pointWeight[1]] == -1 and pointWeight[1] != 0: AL2[pointWeight[0]].append(pointWeight[1:]) AL2[pointWeight[1]].append([pointWeight[0], pointWeight[2]]) dsf.union(forest, pointWeight[0], pointWeight[1]) vertex = pointWeight[1] print() break return AL2
w.append([cell, cell + 1]) if r != maze_rows - 1: w.append([cell, cell + maze_cols]) return w plt.close("all") maze_rows = 10 maze_cols = 15 maze = dsf.DisjointSetForest(maze_rows * maze_cols) walls = wall_list(maze_rows, maze_cols) draw_maze(walls, maze_rows, maze_cols, cell_nums=True) for i in range(len(maze) * 2): d = random.randint(0, len(walls) - 1) if dsf.union(maze, walls[d][1], walls[d][0]): walls.pop(d) """ if dsf.union_c(maze, walls[d][0], walls[d][1]): walls.pop(d) """ #print(walls) #d = random.randint(0, len(walls)-1) #print(walls[d][1], walls[d][0]) draw_maze(walls, maze_rows, maze_cols) plt.show() print((time.time() - start))