Tag Info

New answers tagged

1

s = SparseArray[# -> ConstantArray[1, Length@#]] &@ Union@Flatten[Tuples[#, 2] & /@ conn, 1]; s = s - IdentityMatrix[Length@s]; Flatten[Position[#, 1]] & /@ s {{3, 5}, {4, 6}, {1, 4, 5}, {2, 3, 5, 6}, {1, 3, 4, 6}, {2, 4, 5}} Alternatively written: SparseArray[Union @@ (Tuples[#, 2] & /@ conn) -> 1] // SparseArray[# - ...


3

You could always use MMA's graph algorithms: cycle = PathGraph[Append[#, First[#]]] &; g = GraphUnion @@ (cycle /@ conn) AdjacencyList[g, #] & /@ Range[6] {{3, 5}, {4, 6}, {1, 4, 5}, {2, 3, 5, 6}, {1, 3, 4, 6}, {2, 4, 5}} Sadly, it's not very efficient: (g = GraphUnion @@ (cycle /@ big); AdjacencyList[g, #] & /@ Range[999];) // ...


2

I rather like Sow and Reap for these things. conn = {{1, 3, 5}, {3, 4, 5}, {4, 5, 6}, {2, 4, 6}}; Reap[Sow[#, #] & ~Scan~ conn, Union @@ conn, DeleteCases[Union @@ #2, #] &][[2, All, 1]] {{3, 5}, {4, 6}, {1, 4, 5}, {2, 3, 5, 6}, {1, 3, 4, 6}, {2, 4, 5}} A method using newer functionality: Permutations /@ Rule @@@ Subsets[#, {2}] & /@ ...



Top 50 recent answers are included