EdgeQ¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
EdgeQ[g, e]
gives True if e is an edge of the graph g. u->v means DirectedEdge[u,v] and u\<->v UndirectedEdge[u,v]; an undirected edge matches in either orientation, and direction must agree.
Examples (12)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (5)¶
In[1]:= EdgeQ[CycleGraph[3], 2<->1]
Out[1]= True
In[2]:= EdgeQ[CycleGraph[3], 1->2]
Out[2]= False
In[3]:= EdgeQ[Graph[{1->2}], DirectedEdge[1,2]]
Out[3]= True
In[4]:= EdgeQ[Graph[{1->2}], 2->1]
Out[4]= False
In[5]:= EdgeQ[5, 1->2]
Out[5]= False
Applications (7)¶
An undirected edge present
Undirected edges match in either orientation
No such edge in the cycle
A directed edge in its own orientation
Direction is never blurred
An undirected query does not match a directed edge
A non-graph gives False
Algorithm¶
membership.c - VertexQ[g, v] and EdgeQ[g, e].
Membership is structural (SameQ, via expr_eq), as in the Wolfram Language: a vertex 1 is not matched by 1.0.
EdgeQ accepts the same edge sugar the constructor does -- u -> v means DirectedEdge[u, v], u <-> v means UndirectedEdge[u, v]. An undirected query matches an UndirectedEdge in either orientation; a directed query matches only a DirectedEdge with the same ordered endpoints. Direction is never blurred: u -> v is not an edge of Graph[{u, v}, {u <-> v}].
Both give False for anything that is not a valid graph. Both are O(1) hash probes into the validated-graph memo (graph_util.c), which already holds the vertex index and edge-key set that validating g built.
Memory (SPEC section 4): returns a fresh symbol; the evaluator frees res.
Implementation notes¶
Algorithm. builtin_edge_q takes EdgeQ[g, e]. It returns False when g is not a valid graph, or when e is not a two-argument edge. query_kind accepts DirectedEdge and Rule (u -> v) as directed, and UndirectedEdge and TwoWayRule (u <-> v) as undirected. It then calls graph_has_edge(g, u, v, directed). An undirected query matches an UndirectedEdge in either orientation. A directed query matches only a DirectedEdge with the same ordered endpoints, so 1 -> 2 is not an edge of a graph whose only edge is 1 <-> 2.
Data structures. The graph is Graph[List, List]. Membership is structural (expr_eq), so vertex 1 does not match 1.0. The probe is an O(1) hash lookup in the validated-graph memo, which already holds the vertex index and edge-key set that validating g built.
Complexity / limits. O(1) per query once g has been validated. The first query on a new graph pays the validation cost, which is O(V + E). The result is always True or False, never unevaluated, except for a wrong argument count. An edge whose endpoints are not vertices of g is False.
Protected. Accepts the constructor's sugar (u -> visDirectedEdge[u, v],u <-> visUndirectedEdge[u, v]).- An undirected edge matches in either orientation; direction must agree, so
u -> vis not an edge ofGraph[{u, v}, {u <-> v}]. Falsefor a non-graph (seeUndirectedGraphQ).
Attributes: Protected.
References¶
See also: UndirectedGraphQ
- Source:
src/graph/membership.c - Specification:
docs/spec/builtins/graphs.md - Tests:
tests/test_graph.c - Tests:
tests/test_graph_ops.c
Notes & additional examples¶
Notes¶
EdgeQ accepts the edge sugar u -> v (directed) and u <-> v (undirected). An undirected query matches either orientation, but direction is never blurred between the two kinds. Membership is structural, so vertex 1 does not match 1.0. Anything that is not a valid graph gives False.