0

I am implementing a BFS on a maze represented by a 2D numpy array in Python 3.7.3. This code generates the maze:

for row in range(dim):
        # Add an empty array that will hold each cell
        # in this row
        grid.append([])
        for column in range(dim):
            grid[row].append(np.random.binomial(1, 0.2, 1))  # Append a cell
            if (row == column == dim - 1):
                grid[row][column] = 0
        grid[0][0] = 0

I would like to avoid using a node class as I have had limited success with it. I want to know how I can represent/store parent-child relationships in a BFS search from the (0,0) position on this maze to the (dimension - 1, dimension - 1) position. Also, given the nature of the graph/maze/grid, I NEED to avoid having children with the same location but different parents. I want to return the shortest list of nodes between the start and end positions.

Thank you for your help.

arnavlohe15
  • 332
  • 5
  • 16
  • A node class would be the best and most robust option. An alternative would be to store the node as a tuple like `(x, y, parent_index)`, where `parent_index` links back to the parent of the node. This requires you to store the nodes in a fixed-order list so that the parent indices work properly. eg. `node_parent = nodes_list[node[2]]` – Dominic D Feb 17 '20 at 05:27
  • With that approach the problem is that a single node will have all of its neighbors as parents. I already tried something like that. @Dominic D – arnavlohe15 Feb 17 '20 at 05:29
  • If you're doing a BFS, the first node you find at a particular location will have the minimum distance, so if you're trying to find the shortest path, you only need to store the first parent you find for a particular location. – Dominic D Feb 17 '20 at 05:31

0 Answers0