0
0
DSA Pythonprogramming~10 mins

Spiral Matrix Traversal in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Spiral Matrix Traversal
Start with boundaries: top, bottom, left, right
Traverse from left to right on top row
Move top boundary down
Traverse from top to bottom on right column
Move right boundary left
Traverse from right to left on bottom row
Move bottom boundary up
Traverse from bottom to top on left column
Move left boundary right
Check if boundaries crossed
Yes
End
We move around the matrix edges in a spiral by updating boundaries after each side traversal until all elements are visited.
Execution Sample
DSA Python
def spiral_traversal(matrix):
    result = []
    top, bottom = 0, len(matrix) - 1
    left, right = 0, len(matrix[0]) - 1
    while left <= right and top <= bottom:
        for i in range(left, right + 1):
            result.append(matrix[top][i])
        top += 1
        for i in range(top, bottom + 1):
            result.append(matrix[i][right])
        right -= 1
        if top <= bottom:
            for i in range(right, left - 1, -1):
                result.append(matrix[bottom][i])
            bottom -= 1
        if left <= right:
            for i in range(bottom, top - 1, -1):
                result.append(matrix[i][left])
            left += 1
    return result
This code starts traversing the top row from left to right and collects elements in spiral order.
Execution Table
StepOperationBoundaries (top,bottom,left,right)Elements AddedVisual State of Matrix Traversed
1Initialize boundaries(0,2,0,2)[][ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
2Traverse top row left->right(0,2,0,2)[1,2,3][1] [2] [3] [ ] [ ] [ ] [ ] [ ] [ ]
3Move top boundary down(1,2,0,2)[1,2,3][1] [2] [3] [ ] [ ] [ ] [ ] [ ] [ ]
4Traverse right column top->bottom(1,2,0,2)[1,2,3,6,9][1] [2] [3] [ ] [ ] [6] [ ] [ ] [9]
5Move right boundary left(1,2,0,1)[1,2,3,6,9][1] [2] [3] [ ] [ ] [6] [ ] [ ] [9]
6Traverse bottom row right->left(1,2,0,1)[1,2,3,6,9,8,7][1] [2] [3] [ ] [ ] [6] [7] [8] [9]
7Move bottom boundary up(1,1,0,1)[1,2,3,6,9,8,7][1] [2] [3] [ ] [ ] [6] [7] [8] [9]
8Traverse left column bottom->top(1,1,0,1)[1,2,3,6,9,8,7,4][1] [2] [3] [4] [ ] [6] [7] [8] [9]
9Move left boundary right(1,1,1,1)[1,2,3,6,9,8,7,4][1] [2] [3] [4] [ ] [6] [7] [8] [9]
10Traverse top row left->right(1,1,1,1)[1,2,3,6,9,8,7,4,5][1] [2] [3] [4] [5] [6] [7] [8] [9]
11Move top boundary down(2,1,1,1)[1,2,3,6,9,8,7,4,5][1] [2] [3] [4] [5] [6] [7] [8] [9]
12Check boundaries crossed(2,1,1,1)[1,2,3,6,9,8,7,4,5]Traversal complete
💡 Top boundary (2) > bottom boundary (1), so traversal ends
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9After Step 11Final
top0111122
bottom2221111
left0000111
right2211111
result[][1,2,3][1,2,3,6,9][1,2,3,6,9,8,7][1,2,3,6,9,8,7,4][1,2,3,6,9,8,7,4,5][1,2,3,6,9,8,7,4,5]
Key Moments - 3 Insights
Why do we update the boundaries after each side traversal?
Updating boundaries (top, bottom, left, right) after traversing a side prevents revisiting the same row or column. For example, after traversing the top row (step 3), top moves down to avoid repeating that row (see execution_table rows 2 and 3).
Why does the loop stop when top > bottom or left > right?
The loop condition ensures we only traverse valid rows and columns. When top boundary crosses bottom or left crosses right (step 12), it means all elements are visited, so traversal ends (see exit_note).
How do we know which direction to traverse next?
We follow a fixed order: left to right (top row), top to bottom (right column), right to left (bottom row), bottom to top (left column). This repeats while boundaries allow (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, which elements were added to the result?
A[1,2,3,6,9]
B[7,8,9]
C[1,2,3,6,9,8,7]
D[1,2,3]
💡 Hint
Check the 'Elements Added' column at step 6 in execution_table
At which step does the left boundary move from 0 to 1?
AStep 9
BStep 7
CStep 5
DStep 3
💡 Hint
Look at the 'Boundaries' column in execution_table rows and variable_tracker for 'left' variable
If the matrix was 4x4, how would the exit condition change?
ALoop ends after fixed 4 iterations
BLoop ends when top > bottom or left > right, same as 3x3
CLoop ends when top == bottom and left == right
DLoop never ends
💡 Hint
Refer to exit_note and concept_flow about boundary crossing conditions
Concept Snapshot
Spiral Matrix Traversal:
- Use four boundaries: top, bottom, left, right
- Traverse edges in order: left->right, top->bottom, right->left, bottom->top
- After each edge, update the corresponding boundary
- Stop when boundaries cross (top > bottom or left > right)
- Collect elements in a list in spiral order
Full Transcript
Spiral Matrix Traversal moves around the matrix edges in a spiral pattern. We start with boundaries at the edges: top row, bottom row, left column, and right column. We first traverse the top row from left to right, then move the top boundary down to avoid revisiting. Next, we traverse the right column from top to bottom, then move the right boundary left. Then we traverse the bottom row from right to left, and move the bottom boundary up. Finally, we traverse the left column from bottom to top, and move the left boundary right. We repeat this process until the top boundary crosses the bottom or the left boundary crosses the right, meaning all elements are visited. This method ensures every element is visited exactly once in spiral order.