ExamplesBy LevelBy TopicLearning Paths
847 Advanced

Approximation Algorithm — Set Cover

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Approximation Algorithm — Set Cover" functional Rust example. Difficulty level: Advanced. Key concepts covered: Functional Programming. Set cover is NP-hard: given a universe U of elements and a collection S of subsets, find the minimum number of subsets whose union equals U. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Set cover is NP-hard: given a universe U of elements and a collection S of subsets, find the minimum number of subsets whose union equals U. The greedy approximation picks the subset covering the most uncovered elements at each step, achieving an O(log n) approximation — proven optimal assuming P ≠ NP. This approximation is used in: network monitoring (minimum sensor placement to cover all traffic), advertising (minimum campaigns to reach all demographics), feature selection in machine learning, and compiler register allocation. Understanding approximation algorithms — which sacrifice optimality for polynomial time — is essential for NP-hard optimization.

🎯 Learning Outcomes

  • • Implement the greedy set cover approximation: at each step, pick the set covering most uncovered elements
  • • Understand the O(ln n) approximation ratio: greedy covers at least 1/OPT fraction at each step
  • • Recognize when approximation is appropriate: NP-hard problems in practice
  • • Compare greedy approximation ratio against brute-force optimal on small instances
  • • Apply the technique to vertex cover, dominating set, and other NP-hard problems with similar guarantees
  • Code Example

    #![allow(clippy::all)]
    //! Placeholder

    Key Differences

    AspectRustOCaml
    Universe setHashSet<u32>Set.Make(Int).t or Hashtbl
    Intersection count.intersection().count()Set.inter \|> Set.cardinal
    Best set selectionmax_by_keyList.fold_left with max
    Uncovered removalretain(|e| !contains)Set.diff uncovered chosen
    Approximation ratioO(ln n) provableSame
    Brute force comparisonExponential searchSame

    OCaml Approach

    OCaml implements greedy set cover with a Hashtbl for the uncovered set or a Set.Make(Int) balanced BST. List.fold_left finds the maximum-coverage set. Set.inter uncovered s |> Set.cardinal counts intersections. OCaml's Set.diff uncovered chosen_set removes covered elements immutably. The while loop or tail recursion drives iteration. List.rev restores selection order if building the list by prepending.

    Full Source

    #![allow(clippy::all)]
    //! Placeholder

    Deep Comparison

    OCaml vs Rust: Approximation Set Cover

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Implement brute-force optimal set cover (exponential) for small instances and compare with greedy approximation ratio.
  • Implement the LP relaxation of set cover and show its integrality gap matches the greedy bound.
  • Apply greedy set cover to a sensor placement problem: cover all n points in a plane with minimum unit-radius circles.
  • Implement a weighted set cover: each subset has a cost, minimize total cost using the cost-effectiveness greedy.
  • Measure approximation quality empirically: for random instances, how close does greedy come to optimal?
  • Open Source Repos