How to print the spiral matrix of a given matrix in Python?In this article, you will learn about printing the spiral matrix of a given matrix in Python. Here is the Python implementation to print the spiral matrix of a given matrix: Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
Explanation: The code resulted in elements of the matrix in a spiral pattern, starting from the top-left corner and going clockwise. - The spiral_matrix function takes a matrix as input, which is assumed to be a list of lists representing a 2D array. The function first initializes some variables that will be used to track the boundaries of the matrix (i.e. top, bottom, left, and right) as well as the current direction of the spiral traversal (i.e. direction).
- After that, the function enters a loop that continues until all the elements of the matrix have been added to the spiral result. The loop works by iterating through each row or column of the matrix, depending on the current direction of traversal, and appending each element to the result list.
- After each iteration, the function updates the appropriate boundary variable (i.e. top, bottom, left, or right) to reflect the fact that the current row or column has been fully processed. The function also updates the direction of traversal by incrementing the direction variable and taking the modulus of the result with 4 (since there are four possible directions of traversal).
- Finally, the function returns the result list containing the elements of the matrix in spiral order.
Note: This code assumes that the input matrix is rectangular, meaning that all rows have the same length. If this assumption does not hold, the code may fail with an IndexError or produce unexpected results. Additionally, the code assumes that the input matrix is non-empty, so it may fail with a ValueError if the input is an empty list. - Time complexity: The time complexity of the function is O(m*n), where m is the number of rows in the matrix and n is the number of columns. It is because the function iterates through each element of the matrix exactly once in order to generate the spiral result.
- Space complexity: The space complexity of the function is also O(mn). It is because the function creates a new list (result) that contains all the elements of the matrix in spiral order. The size of this list is proportional to the number of elements in the matrix, which is mn.
Recursive Approach:One way to generate a spiral matrix is to use a recursive approach that visits each layer of the matrix in turn. To do this, we start at the outermost layer of the matrix and add its elements to the result list in a clockwise direction. After that, we recursively call the function on the submatrix inside the outermost layer and repeat the process until we have visited all layers of the matrix. Here is an example implementation: Code: Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
- Time complexity: The time complexity of the function is O(m*n), where m is the number of rows in the matrix and n is the number of columns. It is because the function visits each element of the matrix exactly once in order to generate the spiral result. Additionally, the function makes O(min(m, n)) recursive calls, since it processes one layer of the matrix in each recursive call.
- Space complexity: The space complexity of the function is O(mn), where m is the number of rows in the matrix and n is the number of columns. It is because the function creates a new list (result) that contains all the elements of the matrix in spiral order. Additionally, the function creates O(min(m, n)) new submatrices in each recursive call, each of which contains at most (m-2)(n-2) However, since the total number of elements in all submatrices is at most mn, the space complexity of the function is still O(mn).
Note: These complexities assume that the input matrix is a valid rectangular matrix with at least one element. If the input matrix does not meet these assumptions, the function may fail with an error or produce unexpected results. Additionally, note that the recursive approach may be less efficient than the iterative approach due to the overhead of making recursive calls.Pop and Append approachAnother way to generate a spiral matrix is to repeatedly pop elements from the matrix and append them to the result list in a clockwise direction. To do this, we repeatedly pop the first row of the matrix and append its elements to the result list, then rotate the remaining matrix 90 degrees clockwise and repeat the process until the matrix is empty. Here is an example implementation: Code: Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
|