Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping sets algorithm

Need to develop an algorithm to solve the following task

Given:

The N sets with a different number of elements

Expected Result:

The new M sets containing ≥X common elements of the N sets

Example:

N1=[1,2,3,4,5]
N2=[2,3,5]
N3=[1,3,5]
N4=[1,2]

if X=3:

M1=[1] (from N1,3,4)
M2=[2] (from N1,2,4)
M3=[3,5] (from N1,2,3)
like image 749
Roman Koblev Avatar asked Nov 10 '22 03:11

Roman Koblev


1 Answers

Given N sets (noted Ni) of sorted integers, initialize N variablesHiwhich will hold the heads of each set.

While there still exist indexesHithat haven't reached the end of their respectiveNi, iterate over the valuesVi=Ni[Hi]and find the minimum valueVmin, count the number of occurrencesnand store the corresponding indexesj(which you can all do in one loop).

Increment theHj.

Ifn>X, this gives you a new setM = [Vmin] (from Nj).

Up to you to model the data representation accordingly so as to use(from Nj)as a map key.

like image 82
OB1 Avatar answered Nov 29 '22 04:11

OB1