As of version 11, this is built in:
GraphData["SierpinskiCarpet"]
(* {{"Cycle", 8}, {"SierpinskiCarpet", 2}, {"SierpinskiCarpet", 3}, {"SierpinskiCarpet", 4}} *)
GraphData /@ %
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]
]
]
How about a 3D one?
With[{mesh = MengerMesh[2, 3]},
IGMeshCellAdjacencyGraph[mesh, 3,
VertexCoordinates -> PropertyValue[{mesh, {3, All}}, MeshCellCentroid]
]
]