Finding the partitions of a set which respect to some rules
Using PostgreSQL

WITH t AS (
        SELECT f1, array_agg(f2) AS p
        FROM (
                VALUES
                 ( 'a',1 ),
                 ( 'a',2 ),
                 ( 'b',1 ),
                 ( 'b',2 ),
                 ( 'c',3 ),
                 ( 'd',4 ),
                 ( 'd',5 )
        ) AS t(f1,f2)
        GROUP BY f1
)
SELECT array_to_string(p1, '') || array_to_string(p2, '') AS partitions
FROM (
        SELECT array_agg(DISTINCT t2.f1) AS p1, p AS p2
        FROM t AS t1
        INNER JOIN t AS t2 USING (p)
        GROUP BY p
) AS t;


Result:

 partitions 
------------
 ab12
 c3
 d45
(3 rows)