The Wayback Machine - https://web.archive.org/web/20241125042103/https://www.geeksforgeeks.org/greedy-approximate-algorithm-for-set-cover-problem/
Open In App

Greedy Approximate Algorithm for Set Cover Problem

Last Updated : 14 Jun, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a universe U of n elements, a collection of subsets of U say S = {S1, S2…,Sm} where every subset Si has an associated cost. Find a minimum cost subcollection of S that covers all elements of U.  

Example:

   U = {1,2,3,4,5}
   S = {S1,S2,S3}
   
   S1 = {4,1,3},    Cost(S1) = 5
   S2 = {2,5},      Cost(S2) = 10
   S3 = {1,4,3,2},  Cost(S3) = 3

Output: Minimum cost of set cover is 13 and 
        set cover is {S2, S3}

There are two possible set covers {S1, S2} with cost 15
and {S2, S3} with cost 13.

Why is it useful? 

It was one of Karp’s NP-complete problems, shown to be so in 1972. Other applications: edge covering, vertex cover Interesting example: IBM finds computer viruses (wikipedia) Elements- 5000 known viruses Sets- 9000 substrings of 20 or more consecutive bytes from viruses, not found in ‘good’ code. A set cover of 180 was found. It suffices to search for these 180 substrings to verify the existence of known computer viruses. 

Another example: 

Consider General Motors needs to buy a certain amount of varied supplies and there are suppliers that offer various deals for different combinations of materials (Supplier A: 2 tons of steel + 500 tiles for $x; Supplier B: 1 ton of steel + 2000 tiles for $y; etc.). You could use set covering to find the best way to get all the materials while minimizing cost 

Source: http://math.mit.edu/~goemans/18434S06/setcover-tamara.pdf 

Set Cover is NP-Hard: There is no polynomial time solution available for this problem as the problem is a known NP-Hard problem. There is a polynomial time Greedy approximate algorithm, the greedy algorithm provides a Logn approximate algorithm. 

2-Approximate Greedy Algorithm: Let U be the universe of elements, {S1, S2, … Sm} be collection of subsets of U and Cost(S1), C(S2), … Cost(Sm) be costs of subsets.

1) Let I represents set of elements included so far.  Initialize I = {}

2) Do following while I is not same as U.
    a) Find the set Si in {S1, S2, ... Sm} whose cost effectiveness is 
       smallest, i.e., the ratio of cost C(Si) and number of newly added 
       elements is minimum. 
       Basically we pick the set for which following value is minimum.
           Cost(Si) / |Si - I|
    b) Add elements of above picked Si to I, i.e.,  I = I U Si

Example: Let us consider the above example to understand Greedy Algorithm. 

First Iteration: I = {} The per new element cost for S1 = Cost(S1)/|S1 – I| = 5/3 

The per new element cost for S2 = Cost(S2)/|S2 – I| = 10/2 

The per new element cost for S3 = Cost(S3)/|S3 – I| = 3/4

Since S3 has minimum value S3 is added, I becomes {1,4,3,2}. 

Second Iteration: I = {1,4,3,2} The per new element cost for S1 = Cost(S1)/|S1 – I| = 5/0 Note that S1 doesn’t add any new element to I. The per new element cost for S2 = Cost(S2)/|S2 – I| = 10/1 Note that S2 adds only 5 to I. The greedy algorithm provides the optimal solution for above example, but it may not provide optimal solution all the time. 

Consider the following example.

S1 = {1, 2}
S2 = {2, 3, 4, 5}
S3 = {6, 7, 8, 9, 10, 11, 12, 13}
S4 = {1, 3, 5, 7, 9, 11, 13}
S5 = {2, 4, 6, 8, 10, 12, 13}

Let the cost of every set be same.

The greedy algorithm produces result as {S3, S2, S1}

The optimal solution is {S4, S5} 

Proof that the above greedy algorithm is Logn approximate. 

Let OPT be the cost of optimal solution. Say (k-1) elements are covered before an iteration of above greedy algorithm. The cost of the k’th element <= OPT / (n-k+1) (Note that cost of an element is evaluated by cost of its set divided by number of elements added by its set). How did we get this result? Since k’th element is not covered yet, there is a Si that has not been covered before the current step of greedy algorithm and it is there in OPT. Since greedy algorithm picks the most cost effective Si, per-element-cost in the picked set must be smaller than OPT divided by remaining elements. Therefore cost of k’th element <= OPT/|U-I| (Note that U-I is set of not yet covered elements in Greedy Algorithm). 

The value of |U-I| is n – (k-1) which is n-k+1.

Cost of Greedy Algorithm = Sum of costs of n elements 
                        [putting k = 1, 2..n in above formula]
                         <= (OPT/n + OPT(n-1) + ... + OPT/n) 
                         <= OPT(1 + 1/2 + ...... 1/n)
                        [Since 1 + 1/2 + .. 1/n ? Log n]
                         <= OPT * Logn

Source: http://math.mit.edu/~goemans/18434S06/setcover-tamara.pdf

The Set Cover problem is a classic NP-hard problem that involves finding the minimum number of sets that cover all elements in a given universe. In other words, given a universe U and a collection S of subsets of U, the Set Cover problem is to find a subset C of S such that every element in U is contained in at least one set in C and the size of C is minimized.

One approach to solving the Set Cover problem is to use a greedy algorithm, which iteratively selects the set that covers the most uncovered elements until all elements are covered. Here’s how the greedy algorithm works:

Initialize an empty set C to be the cover.
While there are uncovered elements:
a. Select the set S that covers the most uncovered elements.
b. Add S to C.
c. Remove all covered elements from the set of uncovered elements.
Return C as the cover.
This algorithm provides an approximate solution to the Set Cover problem. The approximation factor is ln(n), where n is the number of elements in the universe U. In other words, the greedy algorithm will always find a cover that is at most ln(n) times larger than the optimal cover.

Advantages:

  1. The greedy algorithm is simple and easy to implement.
  2. It runs in polynomial time, with a time complexity of O(nm), where n is the number of elements in U and m is the number of sets in S.
  3. The approximation factor of ln(n) is a proven guarantee, so we know that the solution is at most ln(n) times larger than the optimal solution.

Disadvantages:

  1. The greedy algorithm may not always find the optimal solution, so it is only an approximation algorithm.
  2. The greedy algorithm relies heavily on the initial ordering of the sets, which can affect the quality of the solution.
  3. The approximation factor of ln(n) can still be large, especially for small values of n.

Previous Article
Next Article

Similar Reads

Greedy Approximate Algorithm for K Centers Problem
Given n cities and distances between every pair of cities, select k cities to place warehouses (or ATMs or Cloud Server) such that the maximum distance of a city to a warehouse (or ATM or Cloud Server) is minimized. For example consider the following four cities, 0, 1, 2, and 3, and the distances between them, how to place 2 ATMs among these 4 citi
9 min read
Introduction and Approximate Solution for Vertex Cover Problem
A vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the graph, either 'u' or 'v' is in the vertex cover. Although the name is Vertex Cover, the set covers all edges of the given graph. Given an undirected graph, the vertex cover problem is to find minimum size vertex cover. The following are some exa
10 min read
Approximate solution for Travelling Salesman Problem using MST
We introduced Travelling Salesman Problem and discussed Naive and Dynamic Programming Solutions for the problem in the previous post,. Both of the solutions are infeasible. In fact, there is no polynomial time solution available for this problem as the problem is a known NP-Hard problem. There are approximate algorithms to solve the problem though.
3 min read
Introduction to Exact Cover Problem and Algorithm X
If you have ever tried to create a program for solving Sudoku, you might have come across the Exact Cover problem. In this article, we will discuss what is the exact cover problem and an algorithm “Algorithm X” proposed by Donald Knuth to solve this problem. Given a collection S of subsets of set X, an exact cover is the subset S* of S such that ea
6 min read
Implementation of Exact Cover Problem and Algorithm X using DLX
In the article Exact Cover Problem and Algorithm X | Set 1 we discussed the Exact Cover problem and Algorithm X to solve the Exact cover problem. In this article, we’ll discuss the implementation details of Algorithm X using Dancing Links Technique (DLX) proposed by Dr Donald E. Knuth in his paper “Dancing Links” Dancing Link Technique Dancing link
10 min read
Shortest Superstring Problem | Set 2 (Using Set Cover)
Given a set of n strings S, find the smallest string that contains each string in the given set as substring. We may assume that no string in arr[] is substring of another string. Examples: Input: S = {"001", "01101", "010"} Output: 0011010 Input: S = {"geeks", "quiz", "for"} Output: geeksquizfor Input: S = {"catg", "ctaagt", "gcta", "ttca", "atgca
3 min read
Difference between Greedy Algorithm and Divide and Conquer Algorithm
Greedy algorithm and divide and conquer algorithm are two common algorithmic paradigms used to solve problems. The main difference between them lies in their approach to solving problems. Greedy Algorithm:The greedy algorithm is an algorithmic paradigm that follows the problem-solving heuristic of making the locally optimal choice at each stage wit
3 min read
Randomized Algorithms | Set 3 (1/2 Approximate Median)
We strongly recommend to refer below articles as a prerequisite of this. Randomized Algorithms | Set 1 (Introduction and Analysis) Randomized Algorithms | Set 2 (Classification and Applications) In this post, a Monte Carlo algorithm is discussed. Problem Statement : Given an unsorted array A[] of n numbers and ε > 0, compute an element w
4 min read
Trapezoidal Rule for Approximate Value of Definite Integral
In the field of numerical analysis, Trapezoidal rule is used to find the approximation of a definite integral. The basic idea in Trapezoidal rule is to assume the region under the graph of the given function to be a trapezoid and calculate its area. It follows that:[Tex]{\displaystyle \int _{a}^{b}f(x)\,dx\approx (b-a)\left[{\frac {f(a)+f(b)}{2}}\r
6 min read
Vertex Cover Problem (Dynamic Programming Solution for Tree)
A vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the graph, either ‘u’ or ‘v’ is in vertex cover. Although the name is Vertex Cover, the set covers all edges of the given graph. The problem to find minimum size vertex cover of a graph is NP complete. But it can be solved in polynomial time for tre
15+ min read
Activity Selection Problem | Greedy Algo-1
You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. Examples: Input: start[] = {10, 12, 20}, finish[] = {20, 25, 30}Output: 0Explanation: A person can perform at most one activities. Input:
15+ min read
Boruvka's algorithm | Greedy Algo-9
We have discussed the following topics on Minimum Spanning Tree.Applications of Minimum Spanning Tree Problem Kruskal’s Minimum Spanning Tree Algorithm Prim’s Minimum Spanning Tree AlgorithmIn this post, Boruvka's algorithm is discussed. Like Prim's and Kruskal's, Boruvka’s algorithm is also a Greedy algorithm. Below is a complete algorithm. 1) Inp
15+ min read
What is Greedy Algorithm in DSA?
A Greedy Algorithm is defined as a problem-solving strategy that makes the locally optimal choice at each step of the algorithm, with the hope that this will lead to a globally optimal solution. In other words, a greedy algorithm always chooses the option that seems the best at the moment, without considering the future consequences or possibilitie
4 min read
Minimum number of subsequences required to convert one string to another using Greedy Algorithm
Given two strings A and B consists of lowercase letters, the task to find the minimum number of subsequence required to form A from B. If it is impossible to form, print -1.Examples: Input: A = "aacbe", B = "aceab" Output: 3 Explanation: The minimum number of subsequences required for creating A from B is "aa", "cb" and "e".Input: A = "geeks", B =
13 min read
Comparison among Greedy, Divide and Conquer and Dynamic Programming algorithm
Greedy algorithm, divide and conquer algorithm, and dynamic programming algorithm are three common algorithmic paradigms used to solve problems. Here's a comparison among these algorithms: Approach:Greedy algorithm: Makes locally optimal choices at each step with the hope of finding a global optimum.Divide and conquer algorithm: Breaks down a probl
4 min read
Graph Coloring Using Greedy Algorithm
We introduced graph coloring and applications in previous post. As discussed in the previous post, graph coloring is widely used. Unfortunately, there is no efficient algorithm available for coloring a graph with minimum number of colors as the problem is a known NP Complete problem. There are approximate algorithms to solve the problem though. Fol
12 min read
Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
We recommend reading the following two posts as a prerequisite for this post. Greedy Algorithms | Set 7 (Dijkstra’s shortest path algorithm) Graph and its representations We have discussed Dijkstra's algorithm and its implementation for adjacency matrix representation of graphs. The time complexity for the matrix representation is O(V^2). In this p
15+ min read
Greedy Algorithm for Egyptian Fraction
Every positive fraction can be represented as sum of unique unit fractions. A fraction is unit fraction if numerator is 1 and denominator is a positive integer, for example 1/3 is a unit fraction. Such a representation is called Egyptian Fraction as it was used by ancient Egyptians. Following are a few examples: Egyptian Fraction Representation of
11 min read
Greedy Algorithm to find Minimum number of Coins
Given a value of V Rs and an infinite supply of each of the denominations {1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes, The task is to find the minimum number of coins and/or notes needed to make the change? Examples: Input: V = 70Output: 2Explanation: We need a 50 Rs note and a 20 Rs note. Input: V = 121Output: 3Explanation: We need a
12 min read
Greedy Best first search algorithm
What is the Greedy-Best-first search algorithm?Greedy Best-First Search is an AI search algorithm that attempts to find the most promising path from a given starting point to a goal. It prioritizes paths that appear to be the most promising, regardless of whether or not they are actually the shortest path. The algorithm works by evaluating the cost
5 min read
Is Dijkstra a greedy algorithm?
In the world of computer science and algorithms, there's a lot of talk about Dijkstra's algorithm and its classification as a "greedy" algorithm. In this article, we will explore what Dijkstra's algorithm is, understand the concept of a greedy algorithm, and discuss whether or not Dijkstra's algorithm qualifies as a greedy algorithm. Understanding
4 min read
Count number of ways to cover a distance | Set 2
Given a distance N. The task is to count the total number of ways to cover the distance with 1, 2 and 3 steps.Examples: Input: N = 3 Output: 4 All the required ways are (1 + 1 + 1), (1 + 2), (2 + 1) and (3).Input: N = 4 Output: 7 Approach: In previous article, a recursive and dynamic programming based approach has been discussed. Here we will reduc
5 min read
Set cover is NP Complete
Problem: Given a ground Set X, an integer k, and a collection of subsets Si of X, the problem is to identify if there exists a collection of subsets whose union is X, with size at most k. Proof: An instance of the problem is an input specified to the problem. An instance of the Set Cover problem is a Ground set X, an integer k and a collection of s
3 min read
Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
Please go through this article first. This article will discuss degeneracy in transportation problem through an explained example. Solution: This problem is balanced transportation problem as total supply is equal to total demand. Initial basic feasible solution: Least Cost Cell Method will be used here to find the initial basic feasible solution.
3 min read
Hungarian Algorithm for Assignment Problem | Set 2 (Implementation)
Given a 2D array, arr of size N*N where arr[i][j] denotes the cost to complete the jth job by the ith worker. Any worker can be assigned to perform any job. The task is to assign the jobs such that exactly one worker can perform exactly one job in such a way that the total cost of the assignment is minimized. Example Input: arr[][] = {{3, 5}, {10,
7 min read
Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
Let there be n agents and n tasks. Any agent can be assigned to perform any task, incurring some cost that may vary depending on the agent-task assignment. It is required to perform all tasks by assigning exactly one agent to each task and exactly one task to each agent in such a way that the total cost of the assignment is minimized. Example: You
15+ min read
Minimum count of subintervals to cover a given time duration
Given an array of intervals[], the task is to find the minimum count of subintervals needed to cover a given time duration. Each interval represents a segment of time that starts at a specific point in time (start time) and ends at another specific point in time (end time). The intervals may overlap and have varying lengths. If it's impossible to c
8 min read
Count number of steps to cover a distance if steps can be taken in powers of 2
Given a distance K to cover, the task is to find the minimum steps required to cover the distance if steps can be taken in powers of 2 like 1, 2, 4, 8, 16........Examples : Input : K = 9 Output : 2 Input : K = 343 Output : 6 The minimum steps required can be calculated by reducing K by the highest power of 2 in each step which can be obtained by co
3 min read
Proof that vertex cover is NP complete
Prerequisite - Vertex Cover Problem, NP-Completeness Problem - Given a graph G(V, E) and a positive integer k, the problem is to find whether there is a subset V’ of vertices of size at most k, such that every edge in the graph is connected to some vertex in V’. Explanation - First let us understand the notion of an instance of a problem. An instan
5 min read
Program to Calculate the Edge Cover of a Graph
Given the number of vertices N of a graph. The task is to determine the Edge cover.Edge Cover: Minimum number of edges required to cover all vertex is known as Edge Cover. Examples: Input : N = 5 Output : 3 Input : N = 4 Output : 2 Example 1: For N = 5 vertices, Edge Cover is: 3 (Choosing the edges marked in Red, all of the vertices will get covere
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg