├── LICENSE ├── Problem Description.pdf ├── README.md └── main.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Areesha-Tahir 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Problem Description.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Areesha-Tahir/BFS-DFS-Maze-Solver-In-Python/6a6c42f9eeb601ef6dfaeae385ffb0cfd4316113/Problem Description.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BFS-DFS-Maze-Solver-In-Python 2 | Feel free to add any comments 3 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | 2 | from collections import deque 3 | 4 | # to keep track of the blocks of maze 5 | class Grid_Position: 6 | def __init__(self, x, y): 7 | self.x = x 8 | self.y = y 9 | 10 | # each block will have its own position and cost of steps taken 11 | class Node: 12 | def __init__(self, pos: Grid_Position, cost): 13 | self.pos = pos 14 | self.cost = cost 15 | 16 | 17 | #BFS algo for the maze 18 | def bfs(Grid, dest: Grid_Position, start: Grid_Position): 19 | # to get neighbours of current node 20 | adj_cell_x = [-1, 0, 0, 1] 21 | adj_cell_y = [0, -1, 1, 0] 22 | m, n = (len(Grid), len(Grid)) 23 | visited_blocks = [[False for i in range(m)] 24 | for j in range(n)] 25 | visited_blocks[start.x][start.y] = True 26 | queue = deque() 27 | sol = Node(start, 0) 28 | queue.append(sol) 29 | cells = 4 30 | cost = 0 31 | while queue: 32 | current_block = queue.popleft() # Dequeue the front cell 33 | current_pos = current_block.pos 34 | if current_pos.x == dest.x and current_pos.y == dest.y: 35 | print("Algorithm used = BFS") 36 | print("Path found!!") 37 | print("Total nodes visited = ", cost) 38 | return current_block.cost 39 | 40 | if current_block not in visited_blocks: 41 | visited_blocks[current_pos.x][current_pos.y] = True 42 | cost = cost + 1 43 | x_pos = current_pos.x 44 | y_pos = current_pos.y 45 | for i in range(cells): 46 | if x_pos == len(Grid) - 1 and adj_cell_x[i] == 1: 47 | x_pos = current_pos.x 48 | y_pos = current_pos.y + adj_cell_y[i] 49 | if y_pos == 0 and adj_cell_y[i] == -1: 50 | x_pos = current_pos.x + adj_cell_x[i] 51 | y_pos = current_pos.y 52 | else: 53 | x_pos = current_pos.x + adj_cell_x[i] 54 | y_pos = current_pos.y + adj_cell_y[i] 55 | if x_pos < 12 and y_pos < 12 and x_pos >= 0 and y_pos >= 0: 56 | if Grid[x_pos][y_pos] == 1: 57 | if not visited_blocks[x_pos][y_pos]: 58 | next_cell = Node(Grid_Position(x_pos, y_pos), 59 | current_block.cost + 1) 60 | visited_blocks[x_pos][y_pos] = True 61 | queue.append(next_cell) 62 | return -1 63 | 64 | def create_node(x, y, c): 65 | val = Grid_Position(x, y) 66 | return Node(val, c + 1) 67 | 68 | #dfs algo for maze 69 | def dfs(Grid, dest: Grid_Position, start: Grid_Position): 70 | adj_cell_x = [1, 0, 0, -1] 71 | adj_cell_y = [0, 1, -1, 0] 72 | m, n = (len(Grid), len(Grid)) 73 | visited_blocks = [[False for i in range(m)] 74 | for j in range(n)] 75 | visited_blocks[start.x][start.y] = True 76 | stack = deque() 77 | sol = Node(start, 0) 78 | stack.append(sol) 79 | neigh = 4 80 | neighbours = [] 81 | cost = 0 82 | while stack: 83 | current_block = stack.pop() 84 | current_pos = current_block.pos 85 | if current_pos.x == dest.x and current_pos.y == dest.y: 86 | print("Algorithm used = DFS") 87 | print("Path found!!") 88 | print("Total nodes visited = ", cost) 89 | return current_block.cost 90 | x_pos = current_pos.x 91 | y_pos = current_pos.y 92 | 93 | for i in range(neigh): 94 | if x_pos == len(Grid) - 1 and adj_cell_x[i] == 1: 95 | x_pos = current_pos.x 96 | y_pos = current_pos.y + adj_cell_y[i] 97 | if y_pos == 0 and adj_cell_y[i] == -1: 98 | x_pos = current_pos.x + adj_cell_x[i] 99 | y_pos = current_pos.y 100 | else: 101 | x_pos = current_pos.x + adj_cell_x[i] 102 | y_pos = current_pos.y + adj_cell_y[i] 103 | if x_pos != 12 and x_pos != -1 and y_pos != 12 and y_pos != -1: 104 | if Grid[x_pos][y_pos] == 1: 105 | if not visited_blocks[x_pos][y_pos]: 106 | cost += 1 107 | visited_blocks[x_pos][y_pos] = True 108 | stack.append(create_node(x_pos, y_pos, current_block.cost)) 109 | return -1 110 | 111 | 112 | def main(): 113 | maze = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 114 | [0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0], 115 | [0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0], 116 | [0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0], 117 | [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1], 118 | [0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0], 119 | [0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 120 | [0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 121 | [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0], 122 | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0], 123 | [1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0], 124 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 125 | destination = Grid_Position(10, 0) 126 | starting_position = Grid_Position(4, 11) 127 | res = bfs(maze, destination, starting_position) 128 | if res != -1: 129 | print("Shortest path steps = ", res) 130 | else: 131 | print("Path does not exit") 132 | 133 | print() 134 | res2 = dfs(maze, destination, starting_position) 135 | if res2 != -1: 136 | print("Steps with backtracking = ", res2) 137 | else: 138 | print("Path does not exit") 139 | # Press the green button in the gutter to run the script. 140 | if __name__ == '__main__': 141 | print("main start\n") 142 | main() 143 | 144 | --------------------------------------------------------------------------------