Push every node to the queue which has no incoming edge
for i in range ( V ) : NEW_LINE INDENT if ( indeg [ i ] == 0 ) : NEW_LINE INDENT q . appendleft ( i ) NEW_LINE DEDENT DEDENT res = [ ] NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT u = q . popleft ( ) NEW_LINE res . append ( u ) NEW_LINE DEDENT