SetCoverByGreedyFast

Top | recent changes | Preferences

How to implement the greedy algorithm for unweighted set cover in linear time

1. Maintain a priority queue of sets, with each set queued by the number of not-yet-covered elements it contains.

2. Choose each set to add by choosing the set with max key in the queue.

3. After choosing a set S, delete S and the elements it contains. Specifically, for each not-yet-covered element e in S, reduce the key of each set S' containing e by 1.


How to implement the priority queue

Notes: The key of each set in the queue is in the range 1..n. The keys only decrease.

Implement the queue as an array of buckets A[1..n] where A[i] holds the sets with key i. Keep an index I which is an upper bound of the maximum index of a non-empty bucket. Initially I=n.

Initializing takes linear time.

To decrease the key of a set from i to i', remove the set from bucket A[i] and insert it into bucket A[i']. This is O(1) time.

To find a set with maximum key: { while(A[I] is empty and I>0) I--; return set in A[I] or nil if empty } (Total time spent doing this is O(n).)


Time spent updating keys is linear, since each update corresponds to deletion of an (elt, set) relation. That is, the total time is e (# sets e is in), which is linear.



Top | recent changes | Preferences
This page is read-only | View other revisions
Last edited June 9, 2004 7:01 pm by Neal (diff)
Search: