topsort + cycle detector