3

Given a grid graph:

enter image description here

and delete the vertices {3, 4, 5, 11, 14, 15, 16, 17, 18, 19, 21, 23, 26, 27, 30} and preserve the structure. There is the VertexDelete command but I am unable to get it working while preserving the structure.

SeedRandom[10801];
dimension = 5;
coDimension = 10;
percProbability = 0.7;
deleteMe = 
 Pick[Table[i, {i, 1, 30}], Table[RandomReal[] > 0.5, {i, 30}]]

G = GridGraph[{dimension, coDimension}, VertexLabels -> "Name", 
  ImagePadding -> 30]

How can you delete a list of vertices from a GridGraph?

p.s. Related to this percolation puzzle here for which I want to solve this.

hhh
  • 2,603
  • 2
  • 19
  • 30
  • "while preserving the structure." <- What do you mean by "structure"? – Szabolcs Jul 09 '16 at 20:46
  • @Szabolcs I want to get the vertex-induced graph of the grid-graph above where the vertices of the list and the incident edges deleted -- and I want to see the graph as a grid, not all vertices jammed together. – hhh Jul 09 '16 at 20:48
  • 4
    If you mean vertex coordinates, do g = SetProperty[g, VertexCoordinates -> GraphEmbedding[g]] before deleting vertices. – Szabolcs Jul 09 '16 at 20:48
  • @Szabolcs Thank you! Over 4 months, I haven't been able to do this and now I learnt it :D – hhh Jul 09 '16 at 20:50

1 Answers1

8

Big Thank You to Szalbocs! You need to have

G = SetProperty[G, VertexCoordinates -> GraphEmbedding[G]];

just before

VertexDelete[G, deleteMe]

enter image description here

SeedRandom[10801];
dimension = 5;
coDimension = 10;
percProbability = 0.7;
deleteMe = 
  Pick[Table[i, {i, 1, 30}], Table[RandomReal[] > 0.5, {i, 30}]];
G = GridGraph[{dimension, coDimension}, VertexLabels -> "Name", 
  ImagePadding -> 30]
G = SetProperty[G, VertexCoordinates -> GraphEmbedding[G]];
VertexDelete[G, deleteMe]

and on the percolation example with more efficient deteleMe as given by m_goldberg

SeedRandom[10801];
dimension = 5;
coDimension = 10;
percProbability = 0.7;
deleteMe = 
 With[{n = dimension coDimension}, 
  Pick[Range[n], Thread[RandomReal[1., n] > percProbability]]]
G = GridGraph[{dimension, coDimension}, VertexLabels -> "Name", 
  ImagePadding -> 30]
G = SetProperty[G, VertexCoordinates -> GraphEmbedding[G]];
VertexDelete[G, deleteMe]

where now removing vertices and its incident edges with percolation probability equal 0.7 over all edges (instead of just a subset)

enter image description here

The two examples demonstrate the SetProperty to preserve the structure.

hhh
  • 2,603
  • 2
  • 19
  • 30