def topological_sort(self):
result = []<EOL>alist = {}<EOL>for k, v in self.adjacency_list.items():<EOL><INDENT>alist[k] = v[:]<EOL><DEDENT>while True:<EOL><INDENT>to_remove = []<EOL>for k, v in list(alist.items())[:]:<EOL><INDENT>if not v:<EOL><INDENT>to_remove.append(k)<EOL>del alist[k]<EOL><DEDENT><DEDENT>if not to_remove:<EOL><INDENT>break<EOL><DEDENT>for k, v in alist.items():<EOL><INDENT>alist[k] = [(d, r) for d, r in v if d not in to_remove]<EOL><DEDENT>logger.debug('<STR_LIT>',<EOL>['<STR_LIT>' % (d.name, d.version) for d in to_remove])<EOL>result.extend(to_remove)<EOL><DEDENT>return result, list(alist.keys())<EOL>
Perform a topological sort of the graph.
:return: A tuple, the first element of which is a topologically sorted
         list of distributions, and the second element of which is a
         list of distributions that cannot be sorted because they have
         circular dependencies and so form a cycle.