Aim is to group (cluster) objects into non-intersecting sets (clusters) based on the multiple properties. However not by a combination of the properties. Here is the algorithm explained in the human language.
- There is an array of objects.
- There are two special properties in the objects by which all the objects should be grouped/clustered.
- First, group the objects by the first property
p1
. - Thus we have divided the original array into several non-intersecting clusters.
- If any object’s
p2
property is matching any object from the other cluster, the two clusters should be merged into one.
Here is an example:
const items = [
{ a: 'file1', b: 'D1', s: 'S0' },
{ a: 'file2', b: 'D1', s: 'S1' },
{ a: 'file3', b: 'D1', s: 'S2' },
{ a: 'file4', b: 'D1', s: 'S2' },
{ a: 'file5', b: 'D2', s: 'S1' },
{ a: 'file6', b: 'D2', s: 'S5' },
{ a: 'file7', b: 'D3', s: 'S6' },
{ a: 'file8', b: 'D3', s: 'S7' },
];
Here, first property is b
and the second property is s
.
- Grouping by
b
gives us clustersfile1-file4
,file5-file6
,file7-file8
. file2
hass=S1
and sofile5
does. They are in different clusters, so the clusters should be merged together. Now we have unified big clusterfile1-file6
.- Cluster
file7-file8
does not intersect any other cluster in propertys
, so it stays isolated.
Here is the graphic representation of the main specialty of the algorithm:
Q1: Is there any well-known algorithm for such a task/problem?
Q2: What approach would be suggested to implement the described algorithm?
The programming language of the project is Node.js, however answer in any programming language is welcome.