I have a first list of pairs like this one:
list1= {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}, {13, 14}, {15, 16}, {17, 18}, {19, 20}, {21, 22}, {23, 24}, {25, 26}, {27, 28}, {29, 30}, {31, 32}, {33, 34}, {35, 36}, {37, 38}, {39, 40}, {41, 42}, {43, 44}, {45, 46}, {47, 48}, {49, 50}}
And a second one containing information about how the elements of the first list are related:
list2 = {{1, 46}, {5, 27}, {8, 42}, {21, 26, 29}, {31, 45}, {33, 36}, {35, 37}, {47, 49}}
How can I gather elements of the first list using list2 as a reference to get groups like these:
{{{1, 2}, {45, 46}, {31, 32}}, {{5, 6}, {27, 28}}, {{7, 8}, {41, 42}}, {{21, 22}, {25, 26}, {29, 30}}, {{31, 32}, {45, 46}}, {{33, 34}, {35, 36}, {37, 38}}, {{47, 48}, {49, 50}}}
So what it's happening here is that, element {1, 46}
of list2 is telling that the pair containing element "1" ({1, 2}
) of list1 is linked to element containing element "46" ({45, 46}
) and so on.
Note that when you use list 2 element {35, 37}
to gather {{35, 36}, {37, 38}}
you should consider that list 2 element {33, 36}
already linked {{33, 34}, {35, 36}}
so you will have a three element group {{33, 34}, {35, 36}, {37, 38}}
.
I tried to use graph related functions but couldn't make it work. Any ideas?