'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.'

def topological_sort(self):

    result = []
    alist = {}
    for (k, v) in self.adjacency_list.items():
        alist[k] = v[:]
    while True:
        to_remove = []
        for (k, v) in list(alist.items())[:]:
            if (not v):
                to_remove.append(k)
                del alist[k]
        if (not to_remove):
            break
        for (k, v) in alist.items():
            alist[k] = [(d, r) for (d, r) in v if (d not in to_remove)]
        logger.debug(u'Moving    to    result:    %s', [(u'%s    (%s)' % (d.name, d.version)) for d in to_remove])
        result.extend(to_remove)
    return (result, list(alist.keys()))
