Push every node to the queue which has no incoming edge
for ( let i = 0 ; i < V ; i ++ ) { if ( indeg [ i ] == 0 ) { q . push ( i ) ; } } let res = [ ] ; while ( q . length > 0 ) { let u = q . shift ( ) ; res . push ( u ) ;