1
$\begingroup$

I am attempting to build I-graphs in MMA, i.e. the non-Generalized Petersen graphs, and I found some code from Eric Weisstein, but this is now outdated and doesn't run. I have attempted to switch the old commands from Combinatorica with the new versions, but it is not working. Can anyone help? Here is the old code:

Initialization

<< MathWorld`Graphs`

Code

IGraph[n_, j_, k_] := Module[{uu, u, vv, v},
  uu = Array[u, n, 0];
  vv = Array[v, n, 0];
  MakeUndirected[ChangeVertices[MakeGraph[Join[uu, vv], Switch[Head /@ {##},
   {u, u}, Mod[#1[[1]] - #2[[1]] - j, n] == 0,
   {v, v}, Mod[#1[[1]] - #2[[1]] - k, n] == 0,
   {u, v}, #1[[1]] == #2[[1]]
   ] &,
 Type -> Directed
 ], Flatten[Table[r Through[{Cos, Sin}[2 Pi s/n]], {r, 2}, {s, 0, n - 1}],
  1]]
   ]
  ]
$\endgroup$
5
  • 1
    $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey May 12 '15 at 2:00
  • $\begingroup$ Could you show your updated version, and mention where and why the update is failing? $\endgroup$ – MarcoB May 12 '15 at 15:03
  • $\begingroup$ @MarcoB, I changed MakeUndirected to UndirectedGraph. IGraph[n_, j_, k_] := Module[{uu, u, vv, v}, uu = Array[u, n, 0]; vv = Array[v, n, 0]; UndirectedGraph[ ChangeVertices[MakeGraph[Join[uu, vv], Switch[Head /@ {##}, {u, u}, Mod[#1[[1]] - #2[[1]] - j, n] == 0, {v, v}, Mod[#1[[1]] - #2[[1]] - k, n] == 0, {u, v}, #1[[1]] == #2[[1]] ] &, Type -> Directed ], Flatten[ Table[r Through[{Cos, Sin}[2 Pi s/n]], {r, 2}, {s, 0, n - 1}], 1]] ] ] IGraph[12, 3, 4] $\endgroup$ – Mat May 12 '15 at 23:51
  • $\begingroup$ @MarcoB UndirectedGraph::graph: A graph object is expected at position 1 in UndirectedGraph[ChangeVertices(<<1>>,(1 0 Sqrt[3]/2 1/2 1/2 Sqrt[3]/2 0 1 -(1/2) Sqrt[3]/2 -(Sqrt[3]/2) 1/2 -1 0 -(Sqrt[3]/2) -(1/2) -(1/2) -(Sqrt[3]/2) 0 -1 1/2 -(Sqrt[3]/2) Sqrt[3]/2 -(1/2) 2 0 Sqrt[3] 1 1 Sqrt[3] 0 2 -1 Sqrt[3] -Sqrt[3] 1 -2 0 -Sqrt[3] -1 -1 -Sqrt[3] 0 -2 1 -Sqrt[3] Sqrt[3] -1 ))]. >> $\endgroup$ – Mat May 12 '15 at 23:52
  • $\begingroup$ A temporary solution that might also help you in figuring out what changes to make would be to work with the old functions from Combinatorica. Let me see how that might work... $\endgroup$ – MarcoB May 13 '15 at 4:19
3
$\begingroup$

You can still use the old functions from Combinatorica, either to tide you over while you figure out the new ones, or as a semi-permanent solution. WRI has also provided a handy guide to transitioning from that package to the new built-in functions: "Upgrading from Combinatorica"

For example, in the case of the obsolete code you mentioned:

<< Combinatorica`

IGraph[n_, j_, k_] := Module[
  {uu, u, vv, v},
  uu = Array[u, n, 0];
  vv = Array[v, n, 0];
  Combinatorica`MakeUndirected[
   Combinatorica`ChangeVertices[
    Combinatorica`MakeGraph[
     Join[uu, vv],
     Switch[Head /@ {##},
       {u, u}, Mod[#1[[1]] - #2[[1]] - j, n] == 0,
       {v, v}, Mod[#1[[1]] - #2[[1]] - k, n] == 0,
       {u, v}, #1[[1]] == #2[[1]]
       ] &,
     Type -> Combinatorica`Directed
     ],
    Flatten[
     Table[r Through[{Cos, Sin}[2 Pi s/n]], {r, 2}, {s, 0, n - 1}], 1]
    ]
   ]
  ]

When you load in Combinatorica, the system will warn you:

General::compat: Combinatorica Graph and Permutations functionality has been superseded by preloaded functionality. The package now being loaded may conflict with this. Please see the Compatibility Guide for details.

... but it will still function. In order to avoid conflicts, I added explicit contexts to the old functions from the Combinatorica package, so you know exactly what you are using.

Having done that, you can now obtain the sample graph you mentioned in the comments:

Combinatorica`ShowGraph@IGraph[12, 3, 4]

Mathematica graphics

$\endgroup$
3
$\begingroup$

Just using builtin graph functions:

IGraph[n_, j_, k_, opts : OptionsPattern[]] := 
 Block[{vert, u, v}, vert = Join[Array[u, n, 0], Array[v, n, 0]];
  AdjacencyGraph[vert, 
   Boole[Table[
     Switch[Head /@ {ii, jj}, {u, u}, 
      Mod[ii[[1]] - jj[[1]] - j, n] == 0, {v, v}, 
      Mod[ii[[1]] - jj[[1]] - k, n] == 0, {u, v}, 
      ii[[1]] == jj[[1]], _, False], {ii, vert}, {jj, vert}]], opts, 
   DirectedEdges -> False, 
   VertexCoordinates -> 
    Flatten[Table[
      r Through[{Cos, Sin}[2 Pi s/n]], {r, 2}, {s, 0, n - 1}], 1]]]

IGraph[12, 3, 4, PlotTheme -> "Web"]

enter image description here

$\endgroup$

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.