2

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.

DavidC
  • 16,724
  • 1
  • 42
  • 94

3 Answers3

5

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

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
4

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.

bill s
  • 68,936
  • 4
  • 101
  • 191
1

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

ybeltukov
  • 43,673
  • 5
  • 108
  • 212