
 Xi                 @   s   d  d   Z  d d   Z d S)c             #   s   |  j    } x | r d d   | j   D     sh t d j d j d d   | j   D     n    V  f d d   | j   D } q Wd	 S)
a[  Variation of Kahn's algorithm (1962) that returns sets.

    Takes a dependency graph as a dictionary of node => dependencies.

    Yields sets of items in topological order, where the first set contains
    all nodes without dependencies, and each following set contains all
    nodes that depend on the nodes in the previously yielded sets.
    c             S   s.   h  |  ]$ \ } } t  |  d  k r |  q S)    )len).0nodedeps r   S/home/ubuntu/projects/ifolica/build/django/django/db/migrations/topological_sort.py	<setcomp>   s   	 z+topological_sort_as_sets.<locals>.<setcomp>zCyclic dependency in graph: {}z, c             s   s   |  ] } t  |  Vq d  S)N)repr)r   xr   r   r   	<genexpr>   s    z+topological_sort_as_sets.<locals>.<genexpr>c                s/   i  |  ]% \ } } |   k r |   |  q Sr   r   )r   r   Zdependencies)currentr   r   
<dictcomp>   s   	 	z,topological_sort_as_sets.<locals>.<dictcomp>N)copyitems
ValueErrorformatjoin)dependency_graphtodor   )r   r   topological_sort_as_sets   s    			+r   c             C   sN   g  } xA t  |  D]3 } x* |  D]" } | | k r  | j |  q  q  Wq W| S)N)r   append)lr   resultZlayerr   r   r   r   stable_topological_sort   s    r   N)r   r   r   r   r   r   <module>   s   