Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select some from many binary sequences so that the result of "or" them together is 1111111111....111

Tags:

algorithm

I have N binary sequences of length L, where N and L maybe very large, and those sequences maybe very sparse, say have much more 0s then 1s.

I want to select M sequences from them, namely b_1, b_2, b_3..., such that

b_1 | b_2 | b_3 ... | b_M = 1111...11 (L 1s)

Is there an algorithm to achieve it?

My idea is:

STEP1: for position from 1 to L, count the total number of sequences which has 1 at that position. Name it 'owning number'

STEP2: consider the position having minimum owning number, and choose the sequence having the maximum number of 1s from the owning sequence of that position.

STEP3: ignore the chosen sequence, update owning number and go back to STEP2.

I believe that my method cannot generate the best answer.

Does anyone has a better idea?

like image 849
HanXu Avatar asked Dec 20 '22 16:12

HanXu


1 Answers

This is the well known set cover problem. It is NP-hard — in fact, its decision version is one of the canonical NP-complete problems and was among the 21 problems included in Karp's 1972 paper — and so no efficient algorithm is known for solving it.

The algorithm you describe in your question is known as the "greedy algorithm" and (unless your problem has some special features that you are not telling us) it's essentially the best known approach. It finds a collection of sets that is no more than O(log |N|) times the size of the smallest such collection.

like image 74
Gareth Rees Avatar answered May 24 '23 12:05

Gareth Rees