Saddleback search finds a target in a matrix where each row and each column is sorted in nondecreasing order. It walks through the matrix along a monotone path, eliminating one row or one column at each step.
Problem
Given a matrix of size such that:
find a position such that:
If no such position exists, return .
Key Idea
Start from the top-right corner:
- At
At each step:
- If current value equals , return it
- If current value is greater than , move left
- If current value is less than , move down
Each move discards one row or one column.
Algorithm
saddleback_search(A, x):
n = number of rows
m = number of columns
i = 0
j = m - 1
while i < n and j >= 0:
if A[i][j] == x:
return (i, j)
else if A[i][j] > x:
j = j - 1
else:
i = i + 1
return (-1, -1)Example
Let
and
Path:
| step | position | value | action |
|---|---|---|---|
| 1 | (0, 3) | 11 | move left |
| 2 | (0, 2) | 7 | move down |
| 3 | (1, 2) | 8 | move down |
| 4 | (2, 2) | 9 | found |
Return .
Correctness
At each step, the algorithm maintains a candidate region where the target may lie.
From the top-right corner:
- All elements below are larger
- All elements to the left are smaller
If , then every element below in column is greater than or equal to , so none can equal . The column can be discarded.
If , then every element to the left in row is less than or equal to , so none can equal . The row can be discarded.
Each step reduces the search space while preserving all possible positions of .
Complexity
| dimension | cost |
|---|---|
| rows | |
| columns | |
| total |
Space:
Comparison
| method | requirement | time |
|---|---|---|
| full scan | none | |
| binary search per row | rows sorted | |
| saddleback search | rows and columns sorted |
Variants
| start point | direction |
|---|---|
| top-right | move left or down |
| bottom-left | move up or right |
Both give the same complexity.
When to Use
- Matrix sorted by rows and columns
- Efficient elimination of rows or columns
- Large matrices where
Notes
- Does not require global ordering across rows
- Works with duplicates
- Deterministic path with no backtracking
Implementation
def saddleback_search(a, x):
if not a or not a[0]:
return -1, -1
n, m = len(a), len(a[0])
i, j = 0, m - 1
while i < n and j >= 0:
if a[i][j] == x:
return i, j
elif a[i][j] > x:
j -= 1
else:
i += 1
return -1, -1func SaddlebackSearch(a [][]int, x int) (int, int) {
if len(a) == 0 || len(a[0]) == 0 {
return -1, -1
}
n, m := len(a), len(a[0])
i, j := 0, m-1
for i < n && j >= 0 {
if a[i][j] == x {
return i, j
} else if a[i][j] > x {
j--
} else {
i++
}
}
return -1, -1
}