Skip to content

Commit

Permalink
topological sort
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Aug 17, 2015
1 parent dee87ae commit c260465
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions js/src/directed/predecessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

const predecessors = function ( G , pred ) {

for ( let u of G.vertices( ) ) pred[u] = 0 ;
for ( let [ _ , v ] of G.edges( ) ) ++pred[v] ;

} ;

exports.predecessors = predecessors ;
30 changes: 30 additions & 0 deletions js/src/directed/topologicalsort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

const topologicalsort = function* ( G , pred , queue ) {

predecessors( G , pred ) ;

for ( let u of G.vertices( ) ) {

if ( pred[u] === 0 ) queue.push( u ) ;

}

while ( !queue.empty( ) ) {

let u = queue.pop( ) ;

yield u ;

for ( let v of G.neighbours( u ) ) {

--pred[v] ;

if ( pred[v] === 0 ) queue.push( v ) ;

}

}

} ;

exports.topologicalsort = topologicalsort ;

0 comments on commit c260465

Please sign in to comment.