import { each , IterableOrArrayLike } from './iter' ; param edges returns export function topologicSort < T > ( edges ) { let sorted = [ ] ; let visited = new Set < T > ( ) ; let graph = new Map < T , T [ ] > ( ) ; each ( edges , addEdge ) ; graph . forEach ( ( v , k ) => { visit ( k ) ; } ) ; return sorted ; function addEdge ( edge ) { let [ fromNode , toNode ] = edge ; let children = graph . get ( toNode ) ; if ( children ) { children . push ( fromNode ) ; } else { graph . set ( toNode , [ fromNode ] ) ; } } function visit ( node ) { if ( visited . has ( node ) ) { return ; } visited . add ( node ) ; let children = graph . get ( node ) ; if ( children ) { children . forEach ( visit ) ; } sorted . push ( node ) ; } }