I’m working on an optimization problem that I believe can be framed as a graph or flow problem, but I haven’t found a good existing formulation that scales efficiently. I’d like advice on how to model or solve it optimally.
Problem description
I have a 2D grid (matrix) of integer values in the range [-128, 127], typically around 1500×2000 in size. Each value represents a pixel intensity from an ultrasound image. I've added a synthetic example:
import numpy as np
import matplotlib.pyplot as plt
# Example synthetic test matrix
np.random.seed(0)
rows, cols = 60, 100
img = np.random.normal(0, 5, (rows, cols)) # background noise
# Create smooth wiggly centerlines
y1 = rows // 6 + 2 * np.sin(np.linspace(0, 3*np.pi, cols))
y2 = rows // 2 + 3 * np.sin(np.linspace(0, 2.5*np.pi, cols) + 1)
y3 = (rows * 4) // 5 + 2 * np.cos(np.linspace(0, 2*np.pi, cols) + 0.5)
# Draw a line with intensity profile
def draw_line(img, y_center, thickness, peak_intensity):
half = thickness // 2
weights = np.exp(-0.5 * (np.arange(-half, half + 1) / (thickness / 3))**2)
weights /= weights.max() # normalize so center = 1
for x_idx, y_c in enumerate(y_center.astype(int)):
for offset, w in zip(range(-half, half + 1), weights):
y = int(y_c + offset)
if 0 <= y < img.shape[0]:
img[y, x_idx] += peak_intensity * w
# Draw three bright non-crossing lines (3–5 px thick)
draw_line(img, y1, thickness=3, peak_intensity=120)
draw_line(img, y2, thickness=4, peak_intensity=90)
draw_line(img, y3, thickness=5, peak_intensity=70)
# Display
plt.figure(figsize=(8, 5))
plt.imshow(img, cmap='gray', origin='upper', aspect='auto')
plt.tight_layout()
plt.show()
The goal is to detect multiple continuous lines (or paths) that run left to right across the entire width of the grid, maximizing the sum of the pixel values along the lines, while satisfying the following constraints:
Connectivity
Each line must start in the first column and end in the last column. From any pixel (r, c), the next step can only go to (r-1, c+1), (r, c+1), or (r+1, c+1) (up-right, right, or down-right).
Minimum vertical spacing
Between any two lines, a minimum vertical distance d_min (e.g., 5 pixels) must be maintained in every column. Therefore lines can never cross or share a pixel. Each pixel can be part of at most one line.
Objective
Maximize the total sum of pixel values along all selected lines (equivalently, minimize the negative sum as a cost).
Performance requirement
The algorithm must scale to matrices up to ~1500×2000 and solve within seconds (not minutes), so exponential or brute-force approaches are not practical.
Notes
- The matrix is quite sparse in terms of meaningful structure — most pixels are near zero, while the desired lines are made of high positive values often surrounded by slightly negative ones.
- Lines can occasionally pass through small noisy or zero-value regions, as long as the overall solution is globally optimal.
- The lines are always one pixel thick and must span the full image width.
- Ideally the number of lines does not have to be fixed beforehand — the solver should determine how many are beneficial based on the grid values and constraints. However consider this as a nice-to-have
What I’ve tried
- A shortest path approach using Dijkstra’s algorithm from left to right works well for detecting a single line.
- Running it iteratively and penalizing previously found paths can detect multiple lines, but that’s not globally optimal — later lines may block globally better configurations.
- I experimented with linear programming and min-cost flow formulations, but the constraints for non-crossing and minimum spacing explode combinatorially and do not scale.
My question
Is there a known optimization model or algorithm that can find the global optimum set of multiple, non-crossing, spaced paths across a grid like this?
I suspect this is related to:
- multi-commodity or disjoint path problems,
- layered graph flow models,
- or dynamic programming with separation constraints.
But I haven’t found a formulation that handles an unknown number of lines and minimum spacing efficiently.
Any advice on a suitable mathematical formulation, relaxation, or practical solver approach would be greatly appreciated.






