1
$\begingroup$

I have graph whose edge list is given by

edges = {1 \[UndirectedEdge] 1, 1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 3, 1 \[UndirectedEdge] 4, 1 \[UndirectedEdge] 5}

I'm finding that the 'If' statement

If[(1 \[UndirectedEdge] 1) ∈ edges,1,0]

does not return 1, as expected, or even return zero. It simply spits back the full conditional,

If[1 \[UndirectedEdge] 1 ∈ {1 \[UndirectedEdge] 1, 
   1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 3, 
   1 \[UndirectedEdge] 4, 1 \[UndirectedEdge] 5}, 1, 0]

Why is this happening?

$\endgroup$
  • 2
    $\begingroup$ try If[MemberQ[edges, 1 \[UndirectedEdge] 1], 1, 0]`? $\endgroup$ – kglr Aug 27 '19 at 15:33
  • $\begingroup$ also Boole @ EdgeQ[Graph@edges,1 \[UndirectedEdge] 1] $\endgroup$ – kglr Aug 27 '19 at 16:24
3
$\begingroup$

Element[a,dom] tests whether a is an element of the domain Dom. The documentation for the function lists what those domains might be, things like Reals and Integers, but not lists that you have defined yourself. So, for example

1 \[Element] {1, 2, 3}

also returns unevaluated whereas

1 \[Element] Integers

returns

True.

And I see that @kglr has already told you what to do, use MemberQ to test membership of arbitrary lists, thusly

If[MemberQ[edges, 1 \[UndirectedEdge] 1], 1, 0]

which returns

True.

$\endgroup$
  • $\begingroup$ ah, thanks a lot! $\endgroup$ – Joe Aug 27 '19 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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