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.
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)
Let's explain it in a better way so that we can understand it easily. Suppose a matrix is given as below -
Length of the shortest path is 3
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.
Let's understand the psuedocode of the Lee algorithm.
Initialize movement arrays=>
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.
Now implement the above pseudocode into the Python code.
Length of the Shortest Path is: 3
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
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).
Next TopicPython Site Connectivity Checker Project
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week