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: enter image description here

Interestingly if I run:


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: enter image description here

Is this a bug or am I doing something wrong?

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

1 Answer 1


This is not a bug.

From the documentation:

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" &;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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