Fix topological sort

Fixes https://github.com/django/django/pull/3525
use { } set comprehension as in b2aad7b836bfde012756cca69291c14d2fdbd334