3
$\begingroup$

Imagine we have a list:

in = {{1, 1}, {2, 2}, {3, 1}, {3, 3}, {4, 1}, {4, 4}, {5, 2}, {5, 5}, {6, 6}}

Needed (general approach for matrixes like 100 x 100):

out = {{1, 3, 4}, {2, 5}, {6}}

Explanation: there are positions of elements in a square matrix (in lower triangle). I need to group rows indexes such that elements that interact with other indexes should group with those indexes in sublists. Note for example that 6 is not in any other position except {6, 6} and so it stays alone.

What for? Just a step in q-analysis as of vintage Atkin(1972). Stuck a bit to get a nice solution.

$\endgroup$
0

1 Answer 1

9
$\begingroup$

If I understood you correctly you are looking for connected components of a graph:

ConnectedComponents[Graph[UndirectedEdge @@@ in]]

{{1, 3, 4}, {2, 5}, {6}}

$\endgroup$
1
  • $\begingroup$ this also works WeaklyConnectedComponents[Graph[Rule @@@ in]] $\endgroup$
    – garej
    Commented Jun 8, 2017 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.