Set Cover or Hitting Set; Numpy, Least element combinations to make up full set
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Set cover and hitting set are classic combinatorial optimization problems. In set cover, you choose the fewest subsets whose union covers the universe. In hitting set, you choose the fewest elements that intersect every subset. They are dual problems and both are NP-hard, so practical solutions often use greedy approximations or integer programming.
With NumPy, you can model subsets as boolean incidence matrices and implement fast greedy heuristics. This gives good results for medium-size instances where exact search is expensive.
Core Sections
1. Encode subsets as a boolean matrix
Let rows represent subsets and columns represent elements in the universe.
For set cover with candidate subsets, transpose representation as needed.
2. Greedy approximation for set cover
At each step choose the subset that covers the most uncovered elements.
This is fast and often near-optimal, though not guaranteed minimal.
3. Greedy approximation for hitting set
For hitting set, choose elements that hit the most currently unhit subsets.
4. Exact solution with ILP when needed
If optimality matters and instance size is manageable, solve via integer linear programming (for example OR-Tools or PuLP). Use greedy result as baseline and upper bound.
5. Add weighting and costs
Real systems often assign cost to subsets/elements. Modify greedy score from raw coverage count to gain-per-cost ratio.
Common Pitfalls
- Expecting greedy output to always be the minimal exact set cover/hitting set.
- Building incidence arrays with inconsistent indexing between subsets and elements.
- Ignoring infeasible cases where some universe elements are never coverable.
- Using Python loops for large matrices instead of vectorized NumPy operations.
- Forgetting weighted variants when subset/element costs differ significantly.
Summary
Set cover and hitting set are hard optimization problems, but NumPy-based greedy heuristics provide practical solutions quickly. Model data as boolean incidence matrices, choose maximum gain at each step, and validate coverage after selection. For strict optimality, escalate to ILP on manageable instances. This workflow balances computational cost and solution quality for many real engineering use cases.
To make this guidance robust in day-to-day engineering work, treat it as an executable checklist instead of one-time reading material. Capture the expected environment, dependency versions, runtime flags, and validation commands in your repository so every contributor can reproduce the same behavior from a clean setup. This is especially important when onboarding new developers, rotating on-call ownership, or debugging incidents under time pressure. Documentation that includes concrete commands, expected outputs, and failure interpretation prevents repeat confusion and shortens recovery time.
It is also worth adding at least one automated guardrail in CI that validates the highest-risk assumption described in the article. Depending on the topic, that guardrail may be a smoke test, policy check, schema validation, benchmark threshold, import check, or integration assertion against a minimal fixture. The goal is to fail fast when environment drift or configuration changes reintroduce old errors. Teams that convert troubleshooting knowledge into small, repeatable checks reduce operational noise and keep this class of issue from returning every sprint.
Related reading
- Set markers for individual points on a line
- Set Matplotlib colorbar size to match graph
- Set value for particular cell in pandas DataFrame using index
- Setting all negative values of a tensor to zero in tensorflow
- Set every cell in matrix to 0 if that row or column contains a 0
- Set time and speed complexity
- Set inputType for an EditText Programmatically?
- set_model missing 1 required positional argument 'model

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.