VertexQ
Examples
open all close allBasic Examples (2)
Test whether an item is a vertex of a graph:
CompleteGraph[5, VertexShapeFunction -> "Name"]VertexQ[%, 4]PetersenGraph[7, 2, VertexShapeFunction -> "Name"]{VertexQ[%, 15], VertexQ[%, "foo"], VertexQ[%, 12]}Scope (5)
VertexQ works with undirected graphs:
VertexQ[[image], 15]VertexQ[[image], 2]VertexQ[[image], 2]VertexQ[[image], 2]VertexQ works with large graphs:
g = GridGraph[{10, 10, 10, 10}];Timing[VertexQ[g, 10^6]]VertexCount[g]Compare performance with a test based on VertexList:
Timing[MemberQ[VertexList[g], 10^4]]Applications (3)
styleOfVertex[g_ ? GraphQ, v_] /; VertexQ[g, v] := AnnotationValue[{g, v}, VertexStyle]{styleOfVertex[[image], 2], styleOfVertex[[image], 5]}Take the correct action depending on whether an item is a vertex or an edge:
g = CompleteGraph[4, VertexSize -> Small]makeRed[item_ /; VertexQ[g, item]] := AnnotationValue[{g, item}, VertexStyle] = RedmakeRed[item_ /; EdgeQ[g, item]] :=
AnnotationValue[{g, item}, EdgeStyle] = RedmakeRed[1];
gmakeRed[23];
gUse Graph and VertexQ for fast set membership tests:
elements = RandomInteger[10000, {1000}];
g = Graph[elements, {}];Timing[Count[Table[VertexQ[g, i], {i, 10000}], True]]Compare with MemberQ:
Timing[Count[Table[MemberQ[elements, i], {i, 10000}], True]]Properties & Relations (3)
VertexQ[g,v] is equivalent to MemberQ[VertexList[g],v]:
g = Graph[{12, 23}];{MemberQ[VertexList[g], 2], VertexQ[g, 2]}The performance of VertexQ is typically higher:
g = GridGraph[{10, 10, 10, 10}];{Timing[MemberQ[VertexList[g], 2]], Timing[VertexQ[g, 2]]}Use VertexIndex to find the position of a vertex in VertexList[g]:
g = Graph[{34, 12, 24, 23}]VertexIndex[g, 2]VertexIndex is typically faster than Position:
g = GridGraph[{10, 10, 10, 10}];{Timing[VertexIndex[g, 2]], Timing[Position[VertexList[g], 2]]}Use EdgeQ to test whether an edge is part of a graph:
EdgeQ[Graph[{12, 23}], 23]Possible Issues (1)
Some vertices do not seem to be recognized:
v = Block[{ϵ = 10 ^ -15}, Range[1.0, 10.0] + ϵ]g = Graph[v, Table[v[[i]]v[[i + 1]], {i, 1, 9}]]VertexQ[g, 1.0]Membership is tested using SameQ rather than Equal:
{v[[1]] === 1.0, v[[1]] == 1.0}By using identical expressions it can still be tested:
VertexQ[g, v[[1]]]See Also
Related Guides
History
Text
Wolfram Research (2010), VertexQ, Wolfram Language function, https://reference.wolfram.com/language/ref/VertexQ.html.
CMS
Wolfram Language. 2010. "VertexQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/VertexQ.html.
APA
Wolfram Language. (2010). VertexQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/VertexQ.html
BibTeX
@misc{reference.wolfram_2026_vertexq, author="Wolfram Research", title="{VertexQ}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/VertexQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_vertexq, organization={Wolfram Research}, title={VertexQ}, year={2010}, url={https://reference.wolfram.com/language/ref/VertexQ.html}, note=[Accessed: 13-June-2026]}