Fix topological sort

Fixes https://github.com/django/django/pull/3525
The problem with leaving `__eq__` and `__ne__` is that these are the exact reason it won't work in Python 3. Toposort depends on being able to use operations as keys in dicts and elements in sets and that in turn requires hashability that you lose when overriding equality. See #3525 for discussion.