- Design and Analysis of Algorithms
- Home
- Basics of Algorithms
- DAA - Introduction
- DAA - Analysis of Algorithms
- DAA - Methodology of Analysis
- Asymptotic Notations & Apriori Analysis
- Time Complexity
- Master’s Theorem
- DAA - Space Complexities
- Divide & Conquer
- DAA - Divide & Conquer
- DAA - Max-Min Problem
- DAA - Merge Sort
- DAA - Binary Search
- Strassen’s Matrix Multiplication
- Karatsuba Algorithm
- Towers of Hanoi
- Greedy Algorithms
- DAA - Greedy Method
- Travelling Salesman Problem
- Prim's Minimal Spanning Tree
- Kruskal’s Minimal Spanning Tree
- Dijkstra’s Shortest Path Algorithm
- Map Colouring Algorithm
- DAA - Fractional Knapsack
- DAA - Job Sequencing with Deadline
- DAA - Optimal Merge Pattern
- Dynamic Programming
- DAA - Dynamic Programming
- Matrix Chain Multiplication
- Floyd Warshall Algorithm
- DAA - 0-1 Knapsack
- Longest Common Subsequence
- Travelling Salesman Problem | Dynamic Programming
- Randomized Algorithms
- Randomized Algorithms
- Randomized Quick Sort
- Karger’s Minimum Cut
- Fisher-Yates Shuffle
- Approximation Algorithms
- Approximation Algorithms
- Vertex Cover Problem
- Set Cover Problem
- Travelling Salesperson Approximation Algorithm
- Graph Theory
- DAA - Spanning Tree
- DAA - Shortest Paths
- DAA - Multistage Graph
- Optimal Cost Binary Search Trees
- Heap Algorithms
- DAA - Binary Heap
- DAA - Insert Method
- DAA - Heapify Method
- DAA - Extract Method
- Sorting Techniques
- DAA - Bubble Sort
- DAA - Insertion Sort
- DAA - Selection Sort
- DAA - Shell Sort
- DAA - Heap Sort
- DAA - Bucket Sort
- DAA - Counting Sort
- DAA - Radix Sort
- Searching Techniques
- Searching Techniques Introduction
- DAA - Linear Search
- DAA - Binary Search
- DAA - Interpolation Search
- DAA - Jump Search
- DAA - Exponential Search
- DAA - Fibonacci Search
- DAA - Sublist Search
- Complexity Theory
- Deterministic vs. Nondeterministic Computations
- DAA - Max Cliques
- DAA - Vertex Cover
- DAA - P and NP Class
- DAA - Cook’s Theorem
- NP Hard & NP-Complete Classes
- DAA - Hill Climbing Algorithm
- DAA Useful Resources
- DAA - Quick Guide
- DAA - Useful Resources
- DAA - Discussion

# Design and Analysis - Set Cover Problem

The set cover algorithm provides solution to many real-world resource allocating problems. For instance, consider an airline assigning crew members to each of their airplanes such that they have enough people to fulfill the requirements for the journey. They take into account the flight timings, the duration, the pit-stops, availability of the crew to assign them to the flights. This is where set cover algorithm comes into picture.

Given a universal set U, containing few elements which are all divided into subsets. Considering the collection of these subsets as S = {S_{1}, S_{2}, S_{3}, S_{4}... S_{n}}, the set cover algorithm finds the minimum number of subsets such that they cover all the elements present in the universal set.

As shown in the above diagram, the dots represent the elements present in the universal set U that are divided into different sets, S = {S_{1}, S_{2}, S_{3}, S_{4}, S_{5}, S_{6}}. The minimum number of sets that need to be selected to cover all the elements will be the optimal output = {S_{1}, S_{2}, S_{3}}.

## Set Cover Algorithm

The set cover takes the collection of sets as an input and and returns the minimum number of sets required to include all the universal elements.

The set cover algorithm is an NP-Hard problem and a 2-approximation greedy algorithm.

### Algorithm

**Step 1 **− Initialize Output = {} where Output represents the output set of elements.

**Step 2 **− While the Output set does not include all the elements in the universal set, do the following −

Find the cost-effectiveness of every subset present in the universal set using the formula, $\frac{Cost\left ( S_{i} \right )}{S_{i}-Output}$

Find the subset with minimum cost effectiveness for each iteration performed. Add the subset to the Output set.

**Step 3 **− Repeat Step 2 until there is no elements left in the universe. The output achieved is the final Output set.

### Pseudocode

APPROX-GREEDY-SET_COVER(X, S) U = X OUTPUT = ф while U ≠ ф select S_{i}Є S which has maximum |S_{i}∩U| U = U – S OUTPUT = OUTPUT∪ {S_{i}} return OUTPUT

### Analysis

assuming the overall number of elements equals the overall number of sets (|X| = |S|), the code runs in time O(|X|3)

### Example

Let us look at an example that describes the approximation algorithm for the set covering problem in more detail

S_{1}= {1, 2, 3, 4} cost(S_{1}) = 5 S_{2}= {2, 4, 5, 8, 10} cost(S_{2}) = 10 S_{3}= {1, 3, 5, 7, 9, 11, 13} cost(S_{3}) = 20 S_{4}= {4, 8, 12, 16, 20} cost(S_{4}) = 12 S_{5}= {5, 6, 7, 8, 9} cost(S_{5}) = 15

**Step 1**

The output set, Output = ф

Find the cost effectiveness of each set for no elements in the output set,

S_{1}= cost(S_{1}) / (S_{1}– Output) = 5 / (4 – 0) S_{2}= cost(S_{2}) / (S_{2}– Output) = 10 / (5 – 0) S_{3}= cost(S_{3}) / (S_{3}– Output) = 20 / (7 – 0) S_{4}= cost(S_{4}) / (S_{4}– Output) = 12 / (5 – 0) S_{5}= cost(S_{5}) / (S_{5}– Output) = 15 / (5 – 0)

The minimum cost effectiveness in this iteration is achieved at S_{1}, therefore, the subset added to the output set, Output = {S_{1}} with elements {1, 2, 3, 4}

**Step 2**

Find the cost effectiveness of each set for the new elements in the output set,

S_{2}= cost(S_{2}) / (S_{2}– Output) = 10 / (5 – 4) S_{3}= cost(S_{3}) / (S_{3}– Output) = 20 / (7 – 4) S_{4}= cost(S_{4}) / (S_{4}– Output) = 12 / (5 – 4) S_{5}= cost(S_{5}) / (S_{5}– Output) = 15 / (5 – 4)

The minimum cost effectiveness in this iteration is achieved at S_{3}, therefore, the subset added to the output set, Output = {S_{1}, S_{3}} with elements {1, 2, 3, 4, 5, 7, 9, 11, 13}.

**Step 3**

Find the cost effectiveness of each set for the new elements in the output set,

S_{2}= cost(S_{2}) / (S_{2}– Output) = 10 / |(5 – 9)| S_{4}= cost(S_{4}) / (S_{4}– Output) = 12 / |(5 – 9)| S_{5}= cost(S_{5}) / (S_{5}– Output) = 15 / |(5 – 9)|

The minimum cost effectiveness in this iteration is achieved at S_{2}, therefore, the subset added to the output set, Output = {S_{1}, S_{3}, S_{2}} with elements {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13}

**Step 4**

Find the cost effectiveness of each set for the new elements in the output set,

S_{4}= cost(S_{4}) / (S_{4}– Output) = 12 / |(5 – 11)| S_{5}= cost(S_{5}) / (S_{5}– Output) = 15 / |(5 – 11)|

The minimum cost effectiveness in this iteration is achieved at S_{4}, therefore, the subset added to the output set, Output = {S_{1}, S_{3}, S_{2}, S_{4}} with elements {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 16, 20}

**Step 5**

Find the cost effectiveness of each set for the new elements in the output set,

S_{5}= cost(S_{5}) / (S_{5}– Output) = 15 / |(5 – 14)|

The minimum cost effectiveness in this iteration is achieved at S_{5}, therefore, the subset added to the output set, Output = {S_{1}, S_{3}, S_{2}, S_{4}, S_{5}} with elements {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, 20}

The final output that covers all the elements present in the universal finite set is, Output = {S_{1}, S_{3}, S_{2}, S_{4}, S_{5}}.