5
$\begingroup$

I want to draw the stacked (or generalized) prism graph nicely.

g = PlanarGraph[ GraphProduct[CycleGraph[8], PathGraph[{1, 2, 3, 4}], "Cartesian"], VertexLabels -> None] 

The Mathematica gives me the following:

enter image description here

Currently, I am not very satisfied with two things: first, the spacing between the circles, from the inner to the outer ones, is inconsistent; second, I would like it to be more aesthetically pleasing. I hope one of the edges aligns with the horizontal line, like in the example below (from Stacked Prism Graph).

enter image description here


It seems that the built-in GraphData[{"StackedPrism", {8, 4}}] meets my requirements. However, I still want to learn how to handle this programmatically.

$\endgroup$
1
  • 1
    $\begingroup$ Just a comment on the title of the question. I think the plot is consistent in the sense that the 3D cylinder is viewed in perspective. You might use "not equally spaced". (^_^) $\endgroup$ Commented Dec 27, 2024 at 4:05

2 Answers 2

10
$\begingroup$

Edit

  • Directly construct the graph.
  • To build the cycles,we using Partition[#, 2, 1, 1],and to buid the radial lines,we using Partition[#, 2, 1].
Clear[ptss]; n = 8; k = 4; c = 1/(k + 1); ptss = Table[CirclePoints[1 - c*k, n], {k, 1, k}]; Graph[{UndirectedEdge @@@ Partition[#, 2, 1, 1] & /@ ptss, UndirectedEdge @@@ Partition[#, 2, 1] & /@ Transpose@ptss} // Flatten, VertexCoordinates -> Flatten[ptss, 1]] 

enter image description here

  • A starting point.
n = 8; k = 4; c = 1/(k + 1); ptss = Table[ Append[#, First@#] &@CirclePoints[{1 - c*k, 0}, n], {k, 1, k}]; g = Graphics[{Line /@ ptss, Line /@ Partition[#, 2, 1] & /@ Transpose@ptss}] // DiscretizeGraphics // MeshConnectivityGraph 

enter image description here

$\endgroup$
5
  • $\begingroup$ The code is cute. $\endgroup$ Commented Dec 25, 2024 at 9:39
  • 1
    $\begingroup$ By: ptss = Map[{{Cos[Pi/8], Sin[Pi/8]}, {-Sin[Pi/8], Cos[Pi/8]}} . # &, ptss, {2}]; you can turn the graph so that some lines are parallel to the x axis. $\endgroup$ Commented Dec 25, 2024 at 11:41
  • $\begingroup$ @DanielHuber I deliberately set CirclePoints[{1 - c*k, 0}, n] instead of CirclePoints[1 - c*k, n] according to mathworld.wolfram.com/StackedPrismGraph.html $\endgroup$ Commented Dec 25, 2024 at 11:49
  • $\begingroup$ @cvgmt valuable update. Always find it helpful to see variants. Had already +1 :) $\endgroup$ Commented Dec 26, 2024 at 1:50
  • $\begingroup$ @ubpdqn Thansks again :-) $\endgroup$ Commented Dec 26, 2024 at 1:51
3
$\begingroup$

Another variation:

stackedPrismGraph[{n_, m_}, opts___] := Block[{coord, g}, coord = Flatten[Table[CirclePoints[r/m, n], {r, m}], 1]; g = GraphProduct[CycleGraph[n],PathGraph[Range[m]],"Cartesian"]; Graph[g, VertexCoordinates -> coord, opts] ] stackedPrismGraph[#, VertexStyle -> Directive[EdgeForm[Red], Red], EdgeStyle -> Black, VertexSize -> {.03, .03}] & /@ {{8, 3}, {8, 2}, {5, 5}} 

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ neat , maintaining GraphProduct and specifying coordinates upfront +1:) $\endgroup$ Commented Dec 27, 2024 at 1:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.