Python Program to Print a Spiral MatrixThe problem statement of this tutorial is that for a given 2D matrix, we have to design an algorithm to print it in spiral form in a 1D array. We will implement the algorithm in Python. Sample Inputs and Outputs to Understand the Problem:Solving the Problem Using the Simulation ApproachWe will follow this idea to solve the problem using a simulation approach: We will draw the spiral path for a given matrix or 2D array. The spiral path has to move in a clockwise direction. It will change the path or make turns at the edges only so that the loop does not go out of the bounds of the matrix. Below are the steps to be followed:
Now we will implement this approach in Python. Code Output 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Time Complexity: The time complexity of this approach is O(m x n). This is because we are going through each element of the matrix. As the number of elements will increase, the time complexity will also increase. Auxiliary Space: Since the resultant matrix is stored in another matrix and also we have used the matrix visit, this approach takes O(N) extra memory space. Solving the Problem by Dividing the Matrix into CyclesThis is the idea that we will follow to solve the problem: We will solve this problem by dividing the given matrix into loops or boundaries. We can see from the above examples that the outer loop items are first stored clockwise, and then the inner loop items are stored. So we can print all the loop items using four for loops. Each for loop will work for traversal in a particular direction. The first loop will move from the left of the matrix to the right. However, the second loop will move from the top to the bottom, the third one will traverse the matrix from right to left, and the last loop will move from bottom to up. Following is the step-by-step approach for the implementation of this method:
Now we will implement this method in Python: Code Output 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Time Complexity: The time complexity of this method is O(m x n). This is because we are traversing through each element of the matrix. Auxiliary Space: This method takes O(1) extra memory space. We have just created extra integer variables. Solving the Problem Using RecursionThis is the idea that we will follow to solve the problem: We will solve this problem by printing the edges of the matrix using a recursion function. In every recursive call, we will decrease the dimensions of the matrix. We will follow the same logic we used in the previous solution. Following is the step-by-step approach for the implementation of this method:
Now we will implement this method in Python: Code Output 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Time Complexity: The time complexity of this method is O(m x n). This is because we are traversing through each element of the matrix. Auxiliary Space: This method takes O(1) extra memory space. We have just created extra integer variables. Solving the Problem UsingDFSThis is the idea that we will follow to solve the problem: Another recursive way to solve this problem is by considering DFS movement within the given square matrix. The DFS movement is: going right, then down, then left, then up, then again right, and continuing this movement until we have reached the last element (the middlemost element) of the matrix. We will modify the matrix in-place so that when the DFS algorithm pointer visits every matrix cell, the algorithm will change its value to a value the matrix cannot contain. The DFS algorithm will end the iteration when it reaches a cell when all of its surrounding cells have the value forbidden to go to or, in simple words, have already visited before. We will create a variable that will control the direction of the pointer. Following is the step-by-step approach for the implementation of this method:
We will implement this approach in Python Code Output 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Time Complexity: This algorithm also has O(m x n) time complexity. This is because we are traversing through each element of the matrix individually. Auxiliary Space: This algorithm will take O(1) memory space. Except for the stack used by recursion, this algorithm uses no extra memory space. We have seen various methods to print the spiral form of the matrix. All the methods have the same time complexity O(m x n) and take the same O(1) memory space. There can be more methods to solve this problem, but the most basic approaches are discussed here. |
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