Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Take a simple GridGraph:

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

g

I am puzzled by the following behavior:

Try to delete vertex 2:

h=VertexDelete[g, 2]

vd

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:

EdgeList[h]

{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}]

vd2

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

Thanks.

share|improve this question

3 Answers 3

up vote 2 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. –  David Carraher Jan 5 at 5:50
    
@David Related thread: Keeping vertexcoordinates after adding a new vertex –  István Zachar Jan 6 at 13:48
    
Thanks, István. The thread seems very useful. –  David Carraher Jan 6 at 13:54

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. –  David Carraher Jan 5 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

 
discard

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.