(di)graph topological sort