# Why I can't filter vertices from graph using subgraph by custom properties?

I wanted to have a way to filter the graph using vertices and edges custom properties in Mathematica 11.1. But this code doesn't work the way I expected:

g = GridGraph[{25, 50}];
PropertyValue[{g, #}, "myprop"] = "myval"; & /@ Range[0, 1250, 7];
f = ! PropertyValue[{g, #}, "myprop"] === "myval" &;
sg2 = Subgraph[g, _?f]
Sort[VertexList[sg2]] == Select[Range[25*50], Mod[#, 7] != 0 &]


It returns False and the graph looks like this: Interestingly if I run:

VertexList[sg2]


I see that the vertices that should be removed are in the end of the list. So somehow Subgraph processed them, but didn't remove.

On the other hand if I use this code, where I remove every 7th vertex by its id:

g = GridGraph[{25, 50}];
sg = Subgraph[g, _?(Mod[#, 7] != 0 &)]
Sort[VertexList[sg]] == Select[Range[25*50], Mod[#, 7] != 0 &]


The result is True and the graph looks like this: Is this a bug or am I doing something wrong?

• Property handling is unfortunately terribly buggy. But this is not a bug. It's your function f returning True for all edges. Apr 19, 2017 at 16:26

Subgraph[g,patt] gives the subgraph generated by the vertices and edges that match the pattern patt.
Your function f is returning True for all edges. Use
f = VertexQ[g, #] && PropertyValue[{g, #}, "myprop"] =!= "myval" &;