5
$\begingroup$

I have this list :

{0, 1, 2, 3, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 3}, {3, 2}, {0, 3}, {3, 0}} 

I would like to transform it with the following pattern :

{{0,3},0,{0,1},{1,0}, 1, {1,2}, {2,1}, 2, {2,3},{3,2},3,{3,0}} 

Here some more informations about the pattern :

a) the simple figure j is always between two lists of 2 elements in this way {j,i}, j, {j,k} ; in other terms the 2 lists {j,i} and {j,k} around j starts by j

b) the next list after {j,k} is built with a inversion so after {j,k} we have {k,j}

For example,

For the 0 element, we build : {0,3},0,{0,1}

Next, to go to the 1 element, we use : {1,0}

and the list with 2 elements around 1 are {1,0}, 1, {1,2}

The elements can put in a circular pattern. i will try to add a picture so as to help the understanding.

Would you have some ideas to do that ?

I thank you in advance for your help

$\endgroup$
14
  • $\begingroup$ The two lists are identical. It is not clear what you are trying to accomplish. $\endgroup$ – Bob Hanlon May 23 at 5:28
  • 1
    $\begingroup$ The lists do not even contain the same total number of elements when flattened, so it's not just some "sort" operation. Put down the beer, fix the examples, and if needed describe what exactly the intent is here, so as to preclude answers followed by "well, what I really want to do is...". $\endgroup$ – ciao May 23 at 6:08
  • 1
    $\begingroup$ You need to specify some condition how you want to order the list. $\endgroup$ – Daniel Huber May 23 at 7:39
  • 1
    $\begingroup$ Are you sure about the position of {3,0} and 0? $\endgroup$ – Lou May 23 at 7:49
  • 1
    $\begingroup$ The OP should give the ordering guidelines. Unless that is his assignment and I'm not sure if that is a mathematica question $\endgroup$ – Lou May 23 at 7:58
5
$\begingroup$

We can use RelationGraph + FindPath as follows:

relation = UnsameQ @ ## && 
   MatchQ[{##}, {i_, {i_, _}} | {{i_, _}, i_} | {{i_, j_}, {j_, i_}}] &;

l1 = {0, 1, 2, 3, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 3}, {3, 2}, {0, 3}, {3, 0}};

rg = RelationGraph[relation, l1,  PlotTheme -> "NameLabeled", VertexSize -> Large]

enter image description here

FindPath[rg, {1, 0}, {0, 1}] // First
{{1, 0}, 1, {1, 2}, {2, 1}, 2, {2, 3}, {3, 2}, 3, {3, 0}, {0, 3}, 0, {0, 1}}

Alternatively, you can use relation to construct a list of edges and use it with Graph:

edgelist=UndirectedEdge @@@ Select[relation @@ # &] @ Subsets[l1, {2}];

g = Graph[edgelist, PlotTheme -> "NameLabeled", VertexSize -> Large]

same picture as above

$\endgroup$
2
  • $\begingroup$ Thank you it is exactly the résult i wanted. Now would it possible the second list directly with your filter and once you get make a Graph with the Graph fonction. I would prefer a solution without the RelationGraph and especially the FindHamiltonianPath because i need to use it in a code where the Graph function is more used $\endgroup$ – Bendesarts May 23 at 13:50
  • $\begingroup$ @Bendesarts, I updated with an alternative way to get the same graph without using RelationGraph. You can use FindPath instead of FindHamiltonianPath to get the vertex list in desired order. $\endgroup$ – kglr May 23 at 14:57
2
$\begingroup$

An alternative approach using NestWhile to recursively build the sorted list:

ClearAll[sTep, sOrt]
sTep[list_][{a___, {i_, j_}}] := Module[{p = FirstCase[list, {i, Except[j]}]}, 
  {a, {i, j}, i, p, Reverse @ p}]

sOrt[list_, start_] := Most @ NestWhile[sTep[list], {start}, Length@# < Length@list &]

Examples:

l1 = {0, 1, 2, 3, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 3}, {3, 2}, {0, 3}, {3, 0}};

sOrt[l1, {1, 0}]
  {{1, 0}, 1, {1, 2}, {2, 1}, 2, {2, 3}, {3, 2}, 3, {3, 0}, {0, 3}, 0, {0, 1}}

Start with {3, 2} instead of {1, 0}:

sOrt[l1, {3, 2}]
 {{3, 2}, 3, {3, 0}, {0, 3}, 0, {0, 1}, {1, 0}, 1, {1, 2}, {2, 1}, 2, {2, 3}}
$\endgroup$
1
  • $\begingroup$ This solution works but it is quite long. $\endgroup$ – Bendesarts May 24 at 5:37

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