I have a list of pairs of objects. Objects can appear in the pair in either order. What is the most efficient algorithm (and implementation?) to find all bags (ie sets with duplicates permitted) of pairs between the same objects. For my purpose the object references can be assumed to be pointers, or names or some similar convenient, short, useful representation. The individual pairs are identifiable. There are no pairs which have the same object in both parts of the pair.
So given a list of pairs (Oid is an object reference; Pid a pair reference)
O1-P1-O2
O3-P2-O4
O5-P3-O1
O1-P4-O2
O2-P5-O1
O1-P6-O5
O7-P7-O8
should return:
P1;P4;P5 and P3;P6