Python program to print the Boundary elements of a Matrix

The boundary elements of a matrix are elements located on the outer edges: first row, last row, first column, or last column. These elements form the perimeter of the matrix.

Understanding Boundary Elements

Consider this 3×3 matrix ?

9  8  7
6  5  4
3  2  1

The boundary elements are: 9, 8, 7, 6, 4, 3, 2, 1. The middle element 5 is not a boundary element since it's surrounded by other elements.

Algorithm

  • Step 1 Traverse the matrix using nested loops (outer for rows, inner for columns)

  • Step 2 Check if current element is on boundary: first row, last row, first column, or last column

  • Step 3 If it's a boundary element, print it; otherwise print a space

Implementation

Method 1: Printing Boundary Elements in Matrix Format

This approach prints the matrix with boundary elements visible and spaces for inner elements ?

def print_boundary_matrix(matrix, rows, cols):
    print("Matrix with boundary elements:")
    for i in range(rows):
        for j in range(cols):
            if i == 0 or i == rows-1 or j == 0 or j == cols-1:
                print(f"{matrix[i][j]:2}", end=" ")
            else:
                print("  ", end=" ")
        print()

# Example matrix
matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]
]

print_boundary_matrix(matrix, 4, 4)
Matrix with boundary elements:
 1  2  3  4 
 5           8 
 9          12 
13 14 15 16 

Method 2: Extracting Boundary Elements as a List

This method collects all boundary elements in a list ?

def get_boundary_elements(matrix, rows, cols):
    boundary = []
    for i in range(rows):
        for j in range(cols):
            if i == 0 or i == rows-1 or j == 0 or j == cols-1:
                boundary.append(matrix[i][j])
    return boundary

# Example matrix
matrix = [
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1]
]

boundary_elements = get_boundary_elements(matrix, 3, 3)
print("Boundary elements:", boundary_elements)
print("Total boundary elements:", len(boundary_elements))
Boundary elements: [9, 8, 7, 6, 4, 3, 2, 1]
Total boundary elements: 8

Method 3: Optimized Traversal

Instead of checking every element, traverse only the boundary ?

def print_boundary_optimized(matrix, rows, cols):
    boundary = []
    
    # Top row
    for j in range(cols):
        boundary.append(matrix[0][j])
    
    # Right column (excluding top-right corner)
    for i in range(1, rows):
        boundary.append(matrix[i][cols-1])
    
    # Bottom row (excluding bottom-right corner, if more than 1 row)
    if rows > 1:
        for j in range(cols-2, -1, -1):
            boundary.append(matrix[rows-1][j])
    
    # Left column (excluding corners, if more than 1 column)
    if cols > 1:
        for i in range(rows-2, 0, -1):
            boundary.append(matrix[i][0])
    
    return boundary

# Example with rectangular matrix
matrix = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
]

boundary = print_boundary_optimized(matrix, 3, 5)
print("Boundary elements (clockwise):", boundary)
Boundary elements (clockwise): [1, 2, 3, 4, 5, 10, 15, 14, 13, 12, 11, 6]

Comparison

Method Time Complexity Best For
Matrix Format O(rows × cols) Visual representation
List Collection O(rows × cols) Processing boundary elements
Optimized O(rows + cols) Large matrices

Conclusion

Boundary elements form the perimeter of a matrix. Use the optimized method for better performance with large matrices, and the matrix format method for visual clarity.

Updated on: 2026-03-27T06:18:42+05:30

880 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements