# Why is UndirectedEdge\[a, b\] === UndirectedEdge\[b, a\] false?

**URL:** <https://community.wolfram.com/t/why-is-undirectededge-a-b-undirectededge-b-a-false/9391>\
**Category:** Questions\
**Tags:** graphs-and-networks\
**Created:** [October 8, 2016, 7:21pm UTC](https://community.wolfram.com/t/why-is-undirectededge-a-b-undirectededge-b-a-false/9391 "2016-10-08T19:21:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chase](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/chase/32/19787_2.png) [@chase](https://community.wolfram.com/u/chase)\
**Post date:** [October 8, 2016, 7:21pm UTC](https://community.wolfram.com/t/why-is-undirectededge-a-b-undirectededge-b-a-false/9391/1 "2016-10-08T19:21:47Z")

</div>

I expected the following assertion to return a value of True but Mathematica 11 returns a value of False

```
UndirectedEdge[a, b] === UndirectedEdge[b, a]

```

If the above is “correct” to return a value of False, then my followup question is to ask what function do I apply so as to reduce a list UndirectedEdges holding what I believe to be duplicates into a minimal list of UndirectedEdge elements? e.g., what aFunction is applied so that the following would return a value of True?

```
aFunction[{UndirectedEdge[a, b], UndirectedEdge[b, a], UndirectedEdge[c, d], UndirectedEdge[d,c]}] === {UndirectedEdge[a, b], UndirectedEdge[c, d]}

```

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [October 8, 2016, 9:31pm UTC](https://community.wolfram.com/t/why-is-undirectededge-a-b-undirectededge-b-a-false/9391/2 "2016-10-08T21:31:46Z")

</div>

This is because **UndirectedEdge** does not have the property **Orderless** :

```
Attributes[UndirectedEdge]

```

However you can add it:

```
SetAttributes[UndirectedEdge, Orderless]

```

And when you do so:

```
UndirectedEdge[a, b] === UndirectedEdge[b, a]

```

it will return True… It might affect other pieces of Mathematica as well, so be careful when changing the Attributes of built-in functions!  
Otherwise make your own **SameQ** function, that checks explicitly both permutations.

---

<div class="post-metadata">

**Author:** ![chase](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/chase/32/19787_2.png) [@chase](https://community.wolfram.com/u/chase)\
**Post date:** [October 8, 2016, 9:38pm UTC](https://community.wolfram.com/t/why-is-undirectededge-a-b-undirectededge-b-a-false/9391/3 "2016-10-08T21:38:58Z")

</div>

Thank you – both for the workaround and warning about possible side effects on other parts of the Mathematica system!

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [October 9, 2016, 11:51am UTC](https://community.wolfram.com/t/why-is-undirectededge-a-b-undirectededge-b-a-false/9391/4 "2016-10-09T11:51:04Z")

</div>

You misunderstood what `SameQ` does. `SameQ` tests for structural equality. It doesn’t care about the meaning of the expressions at all. If their full form looks the same, then they’re the same. Otherwise not.

While adding the `Orderless` attribute to `UndirectedEdge` works in this simple test, I _absolutely do not recommend modifying builtin functions_ like this, especially if you are a beginner. Sooner or later one of these modified functions is going to come back and bite you hard [when you least expect it](http://mathematica.stackexchange.com/a/1504/12).

If you are certain that such a modification is the best solution, always isolate it using `Block` or `Internal`InheritedBlock`, and make sure that it has an effect only on a small piece of simple code.

```
 Internal`InheritedBlock[{UndirectedEdge},
   SetAttributes[UndirctedEdge, Orderless];
   <<your code here>>
 ]

```

For your application, I would bring the lists to a canonical form in the spirit described [here](http://mathematica.stackexchange.com/a/128216/12):

```
canonical[edgeList_] := Union[Sort /@ edgeList]

```

Test if two edge lists are the same using `canonical[edgeList1] === canonical[edgeList2]`.
