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 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 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 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_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 kruskals_algorithm(graph): dsf1 = dsf.DisjointSetForest() #holds disjoint set forest for vertex in graph['vertices']: #makes each vertex into a set dsf1.make_set(vertex) minimum_spanning_tree = set() #holds the tree to be returned edges = list(graph['edges']) #list of all edges in the tree edges.sort() #sorts the edges for edge in edges: weight, vertice1, vertice2 = edge #assigns each var. to part of the edge set if dsf1.find(vertice1) != dsf1.find(vertice2): dsf1.union(vertice1, vertice2) minimum_spanning_tree.add(edge) return sorted(minimum_spanning_tree)
def remove_walls_c(W, Print, maze_rows, maze_cols): sets = dsf.DisjointSetForest(maze_rows * maze_cols) numSets = dsf.num_of_sets(sets) start = dt.now() while numSets > 1 and W is not None: d = random.randint(0, len(W) - 1) #print(numSets) if (dsf.in_same_set(sets, W[d][0], W[d][1])) == False: dsf.union_c(sets, W[d][0], W[d][1]) numSets -= 1 W.pop(d) stop = dt.now() - start if Print == True: draw_maze(W, maze_rows, maze_cols, True) print('With compression, wall removal took ', stop, ' to complete.') return W
else: return False 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 plt.close("all") maze_rows = 10 maze_cols = 15 walls = wall_list(maze_rows, maze_cols) draw_maze(walls, maze_rows, maze_cols, cell_nums=True) #This creates a dsf the same size as the number of cells M = dsf.DisjointSetForest(maze_rows * maze_cols) RemoveWalls(M, walls) draw_maze(walls, maze_rows, maze_cols)
# User Interface # ######################################### 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('A path from source to destination is not guaranteed to exist') case = 1 elif m == (n - 1): print('There is a unique path from source to destination') case = 2 else: print('There is at least one path from source to destination') case = 3 #Draws initial maze with numbers draw_maze(walls, maze_rows, maze_cols, cell_nums=True) #Create dsf with the size of the maze(rows * cols) S = dsf.DisjointSetForest(n) #Create valid maze based on the previous case check_maze_uc(S, walls, maze_cols, maze_rows, m, case) draw_maze(walls, maze_rows, maze_cols) #Make an adjacency list G = maze_adjlist(walls_org, walls, n) print('Adjacency List: ', G) #Draw the grapical representation of the maze draw_graph_maze(G, maze_rows, maze_cols) #Create global variables for the algorithms global visited global prev