3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ "while preserving the structure." <- What do you mean by "structure"? $\endgroup$ – Szabolcs Jul 9 '16 at 20:46
  • $\begingroup$ @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. $\endgroup$ – hhh Jul 9 '16 at 20:48
  • 4
    $\begingroup$ If you mean vertex coordinates, do g = SetProperty[g, VertexCoordinates -> GraphEmbedding[g]] before deleting vertices. $\endgroup$ – Szabolcs Jul 9 '16 at 20:48
  • $\begingroup$ @Szabolcs Thank you! Over 4 months, I haven't been able to do this and now I learnt it :D $\endgroup$ – hhh Jul 9 '16 at 20:50
8
$\begingroup$

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.

$\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.