Algorithm to find non-dominated pairs
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding non-dominated pairs means finding the points that are not strictly worse than some other point on every objective. This shows up in Pareto optimization, skyline queries, and ranking systems where you care about trade-offs instead of a single score.
For a two-dimensional minimization problem, a pair p = (x, y) is dominated by q = (a, b) when a <= x and b <= y, with at least one strict improvement. The goal is to return every pair that survives that test.
What Dominance Means
Suppose each pair represents cost and latency, and smaller values are better. If one option is cheaper and no slower, then the slower, more expensive option should never appear in the final answer.
For example, in this list:
the pair (4, 7) is dominated by (3, 5) because 3 < 4 and 5 < 7. The pair (2, 9) is not dominated by (3, 5) because it is cheaper, even though it is slower. Non-dominated points form the Pareto frontier.
Naive Algorithm
The simplest algorithm compares every point against every other point.
This is easy to understand and correct for small inputs, but it costs O(n^2) comparisons. That becomes expensive fast if you are processing thousands of points.
Faster Algorithm for Two Dimensions
In two dimensions, there is a standard optimization. Sort by the first coordinate, then sweep from best to worst while tracking the smallest second coordinate seen so far.
For minimization:
- Sort by
xascending. - If two points have the same
x, sort byyascending. - Walk left to right.
- Keep a running minimum of
y. - A point is non-dominated if its
yis smaller than everyyseen before with smaller or equalx.
This runs in O(n log n) because the sort dominates the cost. The scan itself is linear.
The result for the example is:
Each of those points improves one objective without being worse on the other than some earlier survivor.
Why the Sweep Works
After sorting by x, every later point has x greater than or equal to the current point. That means the only question left is whether an earlier point already had a y that was less than or equal to the current y. If yes, the current point is dominated. If no, it starts a new step on the Pareto frontier.
This logic is specific to the two-dimensional case. Once you move to three or more dimensions, a single running minimum is no longer enough. Then you need data structures such as range trees, divide-and-conquer skyline algorithms, or full non-dominated sorting.
Handling Duplicates and Ties
Ties matter. If the same pair appears twice, you may want to:
- keep both, if the input is a multiset and duplicates are meaningful
- keep one, if you only care about unique coordinate values
If two points share the same x, you should sort by y ascending so the better candidate comes first. Otherwise your sweep can keep the wrong representative and misclassify the rest.
Here is a version that returns unique frontier points:
Common Pitfalls
The most common mistake is forgetting whether the problem is a minimization or maximization problem. If larger values are better, the comparison signs must flip.
Another mistake is using a strict comparison in both coordinates. Dominance requires "better or equal" on all objectives and "strictly better" on at least one. If you require both coordinates to be strictly better, some dominated points will incorrectly survive.
Tied first coordinates are another source of bugs. If you sort only by the first coordinate and leave equal values in arbitrary order, the sweep can depend on input order. Sort by both coordinates explicitly.
Finally, do not overuse the O(n^2) version. It is fine for debugging and unit tests, but production code should use the O(n log n) sweep for two dimensions when possible.
Summary
- A non-dominated pair is not worse than another pair on every objective.
- The naive algorithm compares every point with every other point and costs
O(n^2). - In two dimensions, sorting plus a sweep gives an
O(n log n)solution. - Correct dominance checks use "better or equal" in all coordinates and "strictly better" in at least one.
- Ties and duplicate points need an explicit policy so the frontier is stable and predictable.

