# Lee Algorithm in Python

In this tutorial, we will learn about the Lee Algorithm, which is used to solve maze routing problems. We will implement this algorithm using the Python programming language. Maze routing problems are one of the most interesting and asked programming problems. Lee algorithm is one of the approaches to solving maze problems based on the breath-first search algorithm. Now, let's understand the problem statement.

## Problem Statement

In this problem, a maze in the form of a binary rectangular matrix is given and we need to find the shortest path between a given source cell to a destination cell. The maze is represented as an MxN matrix where each element can either be 0 or 1. We can only create a path if its value is 1. At any given time, we can move one step in one of the four directions. The valid steps will be -

Move Up: (x, y) --> (x - 1, y)
Move Left: (x, y) --> (x, y - 1)
Move Down: (x, y) --> (x + 1, y)
Move Right: (x, y) --> (x, y + 1)

Let's explain it in a better way so that we can understand it easily. Suppose a matrix is given as below -

Input -

Output:

```Length of the shortest path is 3
```

## Solution Approach

To solve such a problem, we will use the Lee algorithm based on the breath-first search (BFS) procedure. We will use the queue and matrix to identify which cell has been visited and repeat this process until we find the shortest path from a source cell to the destination cell.

The BFS is one of the best techniques to find the shorted path because it doesn't consider a single path at once; rather, it considers all the paths starting from the source and moves ahead one unit in all those paths at the same time. Let's understand the algorithm.

### Algorithm

1. First, we create an empty queue and store the coordinate of the matrix ( source cell with a distance of 0 from source itself. Mark it as visited.
2. Call BFS procedure on the source cell.
3. Initialize the boolean array of the same size as input matrix and set all values to false.
4. Run a loop until the queue is empty.
5. Dequeue the front cell from the queue.Return if the destination cell is reached. Otherwise, for each of the four adjancent cells of a current cell, if the cell value is 1 and they are un-visited, enqueue the cells and mark them as
6. Return False, if the destination is not reached after processing all queue nodes.

Psuedocode

Let's understand the psuedocode of the Lee algorithm.

Initialize movement arrays=>

Explanation -

In the above pseudocode, we first create two arrays, rowNums and colNum, which are used in visiting all the four adjacent cells of the current cell by adding the value of these arrays to the current cell coordinates.

• The LeeAlgo() method checks whether the given source and destination cells are valid, i.e. their value is 1. Return -1 if the source or destination is invalid or the path cannot be formed.
• If the source and destination are appropriate, we initialize the visited matrix with all values set to False. We also initialize an empty queue (q), then add the source cell to this queue and mark it as visited.
• After that, we run the loop until the queue is empty and check for each node by popping it from the queue and then processing it. The coordinates of the current cell are stored in the pointer, and then we check if the coordinates are equal to our destination cell and return the distance up to that pointer. If the destination is not reached, we check for all the adjacent cells using the rowNum and colNum arrays and add them to our queue if a path is possible through them and they are still not visited. This way, we apply a BFS pattern by checking all possible paths from a current cell.
• The above loop is iterated until either the destination is achieved or the queue is empty. If the queue becomes vacant before the destination cell is visited, there is no path to reach the destination cell from the source cell, and we return -1 in that case.

Now implement the above pseudocode into the Python code.

Python Code

Output:

```Length of the Shortest Path is: 3
```

Explanation -

In the the code, we imports the "deque" and "namedtuple" classes from the "collections" module.

The "ROW" and "COL" variables are defined at the beginning, and are used to set the dimensions of the maze. The "namedtuple" class is used to create two custom classes, "Cell" and "Node", which store information about the coordinates of a cell in the maze and the distance from the source, respectively.

The "LeeAlgo" function takes in three parameters: a 2D matrix representing the maze, a "src" cell, and a "dest" cell. The function first checks if the source and destination cells are valid (i.e. if they are set to 1 in the matrix). If not, the function returns -1.

The function then initializes a 2D boolean array called "visited", which is used to keep track of which cells in the maze have been visited. The source cell is marked as visited.

A "deque" is created and the source cell is added to it, with a distance of 0. A while loop then iterates through the deque, using breadth-first search (BFS) to explore the maze. The loop continues until the deque is empty.

For each iteration, the function checks if the current cell is the destination cell. If it is, the distance from the source is returned. Otherwise, the function looks at all the neighboring cells (up, down, left, and right) and if the cell is valid (i.e. it is set to 1 in the matrix, and has not been visited yet) it is added to the deque and marked as visited.

In the end, the function returns -1 if a path from the source to the destination is not found.

At the end of the code, a matrix is created to represent the maze, and source and destination coordinates are defined. The "LeeAlgo" function is called with these inputs and the distance from the source to the destination is printed out

## Complexity Analysis

In the above solution, we traverse each cell one by one until the destination cell is reached. All this process leads to a 0(MxN) time complexity where M and N are the dimensions of the matrix. Also since, we require an additional MxN matrix to store the visited status of cells, the space complexity is also O(MxN).