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$
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}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ this also works WeaklyConnectedComponents[Graph[Rule @@@ in]] $\endgroup$ – garej Jun 8 '17 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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