EdgeQ
Examples
open all close allBasic Examples (2)
Scope (8)
EdgeQ works with undirected graphs:
EdgeQ[[image], 411]EdgeQ[[image], 411]EdgeQ[[image], 12]EdgeQ[[image], 23]EdgeQ returns False for items that are not edge expressions:
EdgeQ[[image], "foo"]Rules can be used to test for directed edges:
g = [image];EdgeQ[g, #]& /@ {7 -> 1, 7 -> 3, 5 -> 6}When g is not a graph, EdgeQ[g,e] evaluates to False:
g = Graph[{1, 2}, {12, "asdf"}]EdgeQ[g, 12]Verify that g is not a graph using GraphQ:
GraphQ[g]EdgeQ works with large graphs:
g = GridGraph[{10, 10, 10, 10}];Timing[EdgeQ[g, 1234554321]]EdgeCount[g]Compare performance with a test based on EdgeList:
Timing[MemberQ[EdgeList[g], 1234554321 | 5432112345]]Applications (3)
Test whether 5 is adjacent to 1:
EdgeQ[[image], 15]styleOfEdge[g_ ? GraphQ, e_] /; EdgeQ[g, e] := AnnotationValue[{g, e}, EdgeStyle]{styleOfEdge[[image], 34], styleOfEdge[[image], 35]}Take correct action depending on whether an item is an edge or a vertex:
g = CompleteGraph[4, VertexSize -> Small]makeRed[item_ /; EdgeQ[g, item]] :=
AnnotationValue[{g, item}, EdgeStyle] = RedmakeRed[item_ /; VertexQ[g, item]] := AnnotationValue[{g, item}, VertexStyle] = RedmakeRed[23];
gmakeRed[1];
gProperties & Relations (4)
EdgeQ[g,uv] is equivalent to MemberQ[EdgeList[g],uv]:
g = Graph[{12, 23}];{MemberQ[EdgeList[g], 12], EdgeQ[g, 12]}The performance of EdgeQ is typically higher:
g = GridGraph[{10, 10, 10, 10}, DirectedEdges -> True];{Timing[MemberQ[EdgeList[g], 21]], Timing[EdgeQ[g, 21]]}EdgeQ[g,uv] is equivalent to MemberQ[EdgeList[g],uv|vu]:
g = Graph[{12, 23}];{MemberQ[EdgeList[g], 21 | 12], EdgeQ[g, 12], EdgeQ[g, 21]}The performance of EdgeQ is typically higher:
g = GridGraph[{10, 10, 10, 10}];{Timing[MemberQ[EdgeList[g], 12 | 21]], Timing[EdgeQ[g, 12]]}Use EdgeIndex to find the position of an edge in EdgeList[g]:
g = Graph[{34, 12, 24, 23}]EdgeIndex[g, 24]EdgeIndex is typically faster than Position:
g = GridGraph[{10, 10, 10, 10}];{Timing[EdgeIndex[g, 86318731]],
Timing[Position[EdgeList[g], 86318731 | 87318631]]}Use VertexQ to test whether a vertex is part of a graph:
VertexQ[Graph[{12, 23}], 2]Possible Issues (1)
Some edges 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}]]EdgeQ[g, 1.02.0]Membership is tested using SameQ rather than Equal:
{v[[1]]v[[2]] === 1.02.0, v[[1]]v[[2]] == 1.02.0}By using identical expressions it can still be tested:
EdgeQ[g, v[[1]]v[[2]]]See Also
UndirectedEdge DirectedEdge EdgeList EdgeCount EdgeIndex Graph VertexQ GraphQ
Function Repository: EdgeBetweenQ
Related Guides
History
Text
Wolfram Research (2010), EdgeQ, Wolfram Language function, https://reference.wolfram.com/language/ref/EdgeQ.html.
CMS
Wolfram Language. 2010. "EdgeQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/EdgeQ.html.
APA
Wolfram Language. (2010). EdgeQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/EdgeQ.html
BibTeX
@misc{reference.wolfram_2026_edgeq, author="Wolfram Research", title="{EdgeQ}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/EdgeQ.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_edgeq, organization={Wolfram Research}, title={EdgeQ}, year={2010}, url={https://reference.wolfram.com/language/ref/EdgeQ.html}, note=[Accessed: 12-June-2026]}