Lee Algorithm in Python29 Aug 2024 | 7 min read 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 StatementIn 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 - Input - Output: Length of the shortest path is 3 Solution ApproachTo 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
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.
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 AnalysisIn 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 |
Introduction: In this article, we are discussing parsing tsv python. Files store readable and writable information. The operations achieved on documents in Python are study, write, open, close, rename, and delete. Python has two main types of files: binary files and text files. There are many types...
3 min read
Python Dash module We all have surely heard many times that Python is a dynamically typed programming language, but not all of us maybe know that we can also use Python for web development purposes. Yes, it is right, we can use Python for web development, and...
9 min read
As we all know, there is an incredibly huge amount of text data available on the internet. But, most of us may not be familiar with the methods in order to start working with this text data. Moreover, we also know that it is a tricky...
10 min read
An array's properties are critical to determining the shape, dimension of the array, item size, etc. If related to the numpy ndarray object, we can learn more about these in depth. Let's examine a few of these qualities along with the corresponding instances. Now that we are...
3 min read
In this tutorial, we will discuss how we can compute the average of the list in Python. The average of a list is defined as the sum of elements present in the list divided by the number of elements present in the list. Here, we will make use...
3 min read
Introduction: In this article, we are discussing type conversion in Python. It converts the Py-type data into another form of data. It is a conversion technique. Implicit type translation and explicit type converter are Python's two basic categories of type conversion procedures. Python has type conversion routines that...
6 min read
Introduction In this tutorial, we will discuss how to create a Modern login UI using the CustomTkinter module in Python. Contemporary pc packages are consumer-friendly. Consumer interaction is not limited to console-based totally I/O. They have a different ergonomic graphical person interface (GUI) way to excessive...
7 min read
A barrier object allows a group of threads to wait for each other before continuing execution. It can be useful for tasks that need to be performed in a specific order, or for tasks that need to be synchronized to avoid race conditions. These are used to...
3 min read
As we all know, without a doubt that the popularity of Cryptocurrency has risen steeply in the last few years, and trying to understand the working of the blockchain and what Bitcoin can be annoying and puzzling. There are blockchains, contracts, ledgers, and even more...
8 min read
2048 is a famous yet simple mathematical sliding puzzle game played by a single player. 2048 is quite an addictive game; the main things performed in this game are adding numbers and merging the tiles. So, let us build this game using the Tkinter library in...
57 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India