Skip to main content
2 of 3
added 847 characters in body
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

As of version 11, this is built in:

GraphData["SierpinskiCarpet"]
(* {{"Cycle", 8}, {"SierpinskiCarpet", 2}, {"SierpinskiCarpet", 3}, {"SierpinskiCarpet", 4}} *)

GraphData /@ %

Mathematica graphics


The latest version of IGraph/M incorporates Henrik Schumacher's mesh/graph conversion functions. This way we can easily obtain the face-adjacency graph of a MengerMesh, and add the appropriate vertex coordinates.

With[{mesh = MengerMesh[4]},
 IGMeshCellAdjacencyGraph[mesh, 2,
  VertexCoordinates -> PropertyValue[{mesh, {2, All}}, MeshCellCentroid]
 ]
]

Mathematica graphics

How about a 3D one?

With[{mesh = MengerMesh[2, 3]},
 IGMeshCellAdjacencyGraph[mesh, 3,
  VertexCoordinates -> PropertyValue[{mesh, {3, All}}, MeshCellCentroid]
 ]
]

Mathematica graphics

Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k