Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Take a simple GridGraph:

g = GridGraph[{3, 3}, VertexLabels -> "Name", ImageSize -> 150, ImagePadding -> 10]


I am puzzled by the following behavior:

Try to delete vertex 2:

h=VertexDelete[g, 2]


Vertex 2 was deleted, but so were several edges that did not have a node at vertex 2. In addition, Vertex 1 appears to be connected to vertex 3; it wasn't before.

Now look at the remaining edges:


{1 [UndirectedEdge] 4, 3 [UndirectedEdge] 6, 4 [UndirectedEdge] 5, 4 [UndirectedEdge] 7, 5 [UndirectedEdge] 6, 5 [UndirectedEdge] 8, 6 [UndirectedEdge] 9, 7 [UndirectedEdge] 8, 8 [UndirectedEdge] 9}

According to the edge list, Vertex 1 is not connected to vertex 3 (even though it was draw as connected to 3); but vertex 1 is connected to vertex 4 (even though it was not so drawn).

Try deleting vertices 1 and 3:

VertexDelete[g, {1, 3}]


Either I am misunderstanding something or Mathematica is erring. Can anyone explain which is the case?


share|improve this question
up vote 3 down vote accepted

Bill's explanation is good. I would also recommend preserving the original grid layout to see things clear:

g = GridGraph[{3, 3}, VertexLabels -> "Name", ImagePadding -> 15]
h = SetProperty[VertexDelete[g, #], VertexCoordinates -> Delete[GraphEmbedding[g], #]] &@2

enter image description here

share|improve this answer
Useful suggestion. I'm hoping to delete several vertices and then add some extra edges to the grid graph. Not sure how to proceed. – DavidC Jan 5 '14 at 5:50
@David Related thread: Keeping vertexcoordinates after adding a new vertex – István Zachar Jan 6 '14 at 13:48
Thanks, István. The thread seems very useful. – DavidC Jan 6 '14 at 13:54
I tested this on a graph, removal of many vertices and structure not preserved so problems: can you preview it here? Thank you in advance. – hhh Feb 1 at 11:06

You can see what is going on by changing the arrangement of the vertices. Here is your g with a circular structure:

g = GridGraph[{3, 3}, VertexLabels -> "Name", ImageSize -> 150, 
    ImagePadding -> 10, GraphLayout -> "CircularEmbedding", 
    PlotLabel -> "CircularEmbedding"]

enter image description here

Now for the h:

h = VertexDelete[g, 2]

enter image description here

Now the picture more accurately reflects the structure you expect to see from the EdgeList. The problem is basically that the straight lines connecting 1 and 4 happen to pass through 3 in the default arrangement.

share|improve this answer
So, I was seeing an optical illusion! Thanks for clearing up the mystery. – DavidC Jan 5 '14 at 5:36

More detailed explanation:

GridGraph produces a graph with GraphLayout property

g = GridGraph[{3, 3}, VertexLabels -> "Name", ImageSize -> 150, ImagePadding -> 10];
PropertyValue[g, GraphLayout]

{"GridEmbedding", "Dimension" -> {3, 3}}

VertexDelete conserved this property

h = VertexDelete[g, 2];
PropertyValue[h, GraphLayout]

{"GridEmbedding", "Dimension" -> {3, 3}}

However, now this embedding is incorrect. You can save vertex positions as in Vitaliy Kaurov's answer. Another method is deleting GraphLayout property:

SetProperty[h, GraphLayout -> Automatic]

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.