Python icon indicating copy to clipboard operation
Python copied to clipboard

Add greedy set cover approximation algorithm

Open benchaddha opened this issue 2 months ago • 0 comments

Describe your change:

This PR implements the greedy approximation algorithm for the minimum set cover problem, a fundamental NP-complete problem in computer science with applications in resource allocation, scheduling, and combinatorial optimization.

Implementation highlights:

  • Standard greedy heuristic with $H_d$ approximation guarantee ($\leq \ln|U| + 1$)
  • Comprehensive input validation and error handling
  • Full doctest coverage (17 tests) including edge cases
  • Detailed theoretical documentation with references
  • Time complexity: $O(|U| × |S|)$, Space complexity: $O(|U| + |S|)$
  • [x] Add an algorithm?
  • [ ] Fix a bug or typo in an existing algorithm?
  • [ ] Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • [ ] Documentation change?

Checklist:

  • [x] I have read CONTRIBUTING.md.
  • [x] This pull request is all my own work -- I have not plagiarized.
  • [x] I know that pull requests will not be merged if they fail the automated tests.
  • [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • [x] All new Python files are placed inside an existing directory.
  • [x] All filenames are in all lowercase characters with no spaces or dashes.
  • [x] All functions and variable names follow Python naming conventions.
  • [x] All function parameters and return values are annotated with Python type hints.
  • [x] All functions have doctests that pass the automated testing.
  • [x] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • [ ] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

References:

  • Wikipedia: Set Cover Problem
  • Vazirani, Vijay V. Approximation Algorithms. Berlin: Springer, 2001.
  • Karp, R. M. (1972). "Reducibility Among Combinatorial Problems"
  • Johnson, D. S. (1974). "Approximation algorithms for combinatorial problems"
  • Feige, U. (1998). "A Threshold of ln n for Approximating Set Cover", Journal of the ACM, 45(4), 634-652
  • Cormen et al., "Introduction to Algorithms", Chapter 35.3: The Set-Covering Problem

Testing:

All automated tests pass locally:

python -m doctest -v greedy_methods/greedy_set_cover.py  
ruff check greedy_methods/greedy_set_cover.py            
black --check greedy_methods/greedy_set_cover.py        
mypy --ignore-missing-imports greedy_methods/greedy_set_cover.py  

benchaddha avatar Nov 12 '25 03:11 benchaddha