Array shapes and dimensions in Data Analysis Python - Time & Space Complexity
When working with arrays, knowing how their size and shape affect processing time is important.
We want to understand how the time to handle arrays grows as their dimensions change.
Analyze the time complexity of the following code snippet.
import numpy as np
def sum_all_elements(arr):
total = 0
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
total += arr[i, j]
return total
# Example usage:
# arr = np.array([[1, 2], [3, 4]])
# sum_all_elements(arr)
This code sums all elements in a 2D array by visiting each element one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops visiting each element in the 2D array.
- How many times: Outer loop runs for number of rows, inner loop runs for number of columns, so total visits equal rows x columns.
As the array grows in rows and columns, the total operations grow by multiplying these dimensions.
| Input Size (rows x columns) | Approx. Operations |
|---|---|
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
| 1000 x 1000 | 1,000,000 |
Pattern observation: Doubling rows and columns roughly quadruples the operations because total elements multiply.
Time Complexity: O(n * m)
This means the time grows proportionally to the total number of elements in the array, which is rows times columns.
[X] Wrong: "The time depends only on the number of rows or columns, not both."
[OK] Correct: Because the code visits every element, both rows and columns matter. Ignoring one dimension underestimates the work done.
Understanding how array dimensions affect processing time helps you explain efficiency clearly in interviews and shows you can reason about data size impact.
"What if we changed the array to 3 dimensions? How would the time complexity change?"