def topological_sort(graph):
    """Runs a topological sort on a graph.

    Source: http://www.logarithmic.net/pfh-files/blog/01208083168/sort.py

    :param dict graph: mapping of tasks to lists of successor tasks
    :return: A list of all tasks in the graph sorted according to ther dependencies
    :rtype: list
    """
    count = {}
    for node in graph:
        count[node] = 0
    for node in graph:
        for successor in graph[node]:
            count[successor] += 1

    ready = [node for node in graph if count[node] == 0]

    result = []
    while ready:
        node = ready.pop(-1)
        result.append(node)

        for successor in graph[node]:
            count[successor] -= 1
            if count[successor] == 0:
                ready.append(successor)

    return result