2
$\begingroup$

I want to generate a list of edges from a list:
(the size of the list can be changed, just 4 for this example)

enter image description here

From the list above, I can generate 24 permutations like this.

mylist = Permutations[{{a, b}, {c, d}, {e, f}, {g, h}}]

Now I want to convert each list in mylist into graph as I mentioned above. I could do this by writing a function like this:

myfunc[{{a_, b_}, {c_, d_}, {e_, f_}, {g_, 
     h_}}] := {b \[UndirectedEdge] c, d \[UndirectedEdge] e, 
   f \[UndirectedEdge] g, h \[UndirectedEdge] a};

However, I'm looking for something which is fast, efficient and can work with any number of elements (not just 4 as above).
Any idea to implement this?

$\endgroup$

4 Answers 4

4
$\begingroup$
Clear[myfunc];

myfunc[list_] := 
  UndirectedEdge @@@ Partition[RotateLeft[Flatten[list], 1], 2]

myfunc /@ mylist
(* { {b \[UndirectedEdge] c, d \[UndirectedEdge] e, f \[UndirectedEdge] g,
      h \[UndirectedEdge] a}, ...} *)
$\endgroup$
4
  • $\begingroup$ I think you misread my rule to convert the list to graph. {{a, b}, {c, d}} would be converted to {b-c, d-a} with- is for undirected edge. $\endgroup$
    – internet
    Nov 3, 2022 at 22:27
  • $\begingroup$ Oh, sorry I had two functions with the same name so it was confusing. $\endgroup$
    – internet
    Nov 3, 2022 at 22:32
  • $\begingroup$ It does that, but it was meant to accept the whole mylist. I changed it now to a single-list function. $\endgroup$
    – Domen
    Nov 3, 2022 at 22:32
  • 1
    $\begingroup$ Simplifying: UndirectedEdge @@@ Partition[RotateLeft@Catenate@#, 2] & $\endgroup$
    – Alan
    Nov 4, 2022 at 21:51
3
$\begingroup$

Or using SubsetMap. After we get one graph, we use Permutations to act on it to get anoter cases.

Clear[list,graph];
list = {{a, b}, {c, d}, {e, f}, {g, h}};
graph = UndirectedEdge @@@ 
   Reverse /@ SubsetMap[RotateLeft, list, {All, 1}]

graph // Permutations

enter image description here

$\endgroup$
1
$\begingroup$

just for fun.

{{a, b}, {c, d}, {e, f}, {g, h}} //
# /. {a_, e___} -> {a, e, a}& //
# //.{x___UndirectedEdge, {a_,b_},{c_,d_},e___} -> {x, UndirectedEdge[b,c], {c,d} ,e}& //
# /. {e___, a_} -> {e}&

 {UndirectedEdge[b, c], UndirectedEdge[d, e], UndirectedEdge[f, g], UndirectedEdge[h, a]}

$\endgroup$
1
$\begingroup$
list = {{a, b}, {c, d}, {e, f}, {g, h}};

MapThread[UndirectedEdge]@Reverse@MapAt[RotateRight, 2]@Transpose@list

Visualizing the code with Echo:

MapThread[UndirectedEdge]@
 Echo@Reverse@Echo@MapAt[RotateRight, 2]@Echo@Transpose@Echo@list

list

$$ \left( \begin{array}{cc} a & b \\ c & d \\ e & f \\ g & h \\ \end{array} \right) $$

Transpose

$$ \left( \begin{array}{cccc} a & c & e & g \\ b & d & f & h \\ \end{array} \right) $$

MapAt[RotateRight, 2]

$$ \left( \begin{array}{cccc} a & c & e & g \\ h & b & d & f \\ \ & \longrightarrow \end{array} \right) $$

Reverse

$$ \left( \updownarrow \begin{array}{cccc} h & b & d & f \\ a & c & e & g \\ \end{array} \right) $$

MapThread[UndirectedEdge]

$$ \{h\longleftrightarrow a,b\longleftrightarrow c,d\longleftrightarrow e,f\longleftrightarrow g\} $$

Note : I used /. UndirectedEdge -> LongLeftRightArrow as UndirectedEdge does not render well here.

$\endgroup$

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.