Create a Grid

A simple warmup - make the adjacency list for a grid.

The first basic step is to write a function to create the adjacency list representation for a grid. This should be easy. Working this way allows us not to hard-code the shape of graph into the maze-creation or maze-solving steps, so we could theoretically make other shaped mazes.

(0,0) - (1,0) - (2,0)
  |       |       |
(0,1) - (1,1) - (2,1)

The code should fit in this framework:

def make_grid(width:int, height:int):
    '''Puts out the edges for a width*height grid.'''
    return None

def test_make_grid_1():
  ans = make_grid(3,2)
  correct = {(0,0):{(1,0),(0,1)},
             (1,0):{(0,0),(2,0),(1,1)},
             (2,0):{(1,0),(2,1)},
             (0,1):{(0,0),(1,1)},
             (1,1):{(0,1),(1,0),(2,1)},
             (2,1):{(2,0),(1,1)}}
  assert correct == ans

Notes

  • You can choose to use an adjacency set or an adjacency list. They both have strengths. In this particular case, there are at most 4 elements, so there are no efficiency concerns with using a list.
Last modified November 14, 2023: Improved writeup - use ordered pairs. (483ff71)