Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robrighter committed Feb 26, 2012
1 parent 7942608 commit 85677c9
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions topological-sort.js
@@ -1,68 +1,66 @@
function topologicalSort(graph){ function topologicalSort(graph) {
var numberOfNodes = graph.length; var numberOfNodes = graph.length;
var processed = []; var processed = [];
var unprocessed = []; var unprocessed = [];
var queue = []; var queue = [];
populateIndegreesAndUnprocessed();
processList(); function iterate(arr, callback){
return processed; var i;
for(i=0;i<arr.length;i++){
callback(arr[i], i);
}
}


function processList(){ function processList(){
var toBeRemoved = [];
for(var i=0; i<unprocessed.length; i++){ for(var i=0; i<unprocessed.length; i++){
var nodeid = unprocessed[i]; var nodeid = unprocessed[i];
if(graph[nodeid].indegrees === 0){ if(graph[nodeid].indegrees === 0){
queue.push(nodeid); queue.push(nodeid);
console.log('About to remove edge ' + i + ' from the graph.');
unprocessed.splice(i, 1); //Remove this node, its all done. unprocessed.splice(i, 1); //Remove this node, its all done.
i--;//decrement i since we just removed that index from the iterated list; i--;//decrement i since we just removed that index from the iterated list;
} }
} }


console.log(unprocessed);
console.log('\n\n');

processStartingPoint(queue.shift()); processStartingPoint(queue.shift());
if(processed.length<numberOfNodes){ if(processed.length<numberOfNodes){
processList(); processList();
} }
} }




function processStartingPoint(i){ function processStartingPoint(nodeId){
if(i == undefined){ if(nodeId == undefined){
throw "You have a cycle!!"; throw "You have a cycle!!";
} }
for( var t=0; t<graph[i].edges.length; t++){ iterate(graph[nodeId].edges, function(e){
var e = graph[i].edges[t];
graph[e].indegrees--; graph[e].indegrees--;
}; });
processed.push(i); processed.push(nodeId);
} }




function populateIndegreesAndUnprocessed(){ function populateIndegreesAndUnprocessed(){
for(var i=0; i<graph.length; i++){ iterate(graph, function(node, nodeId){
unprocessed.push(i); unprocessed.push(nodeId);
if(!graph[i].hasOwnProperty('indegrees')){ if(!node.hasOwnProperty('indegrees')){
graph[i].indegrees = 0 node.indegrees = 0
} }
for( var t=0; t<graph[i].edges.length; t++){ iterate(node.edges, function(e){
var e = graph[i].edges[t];
if(!graph[e].hasOwnProperty('indegrees')){ if(!graph[e].hasOwnProperty('indegrees')){
graph[e].indegrees = 1 graph[e].indegrees = 1
} }
else{ else{
graph[e].indegrees = graph[e].indegrees + 1; graph[e].indegrees = graph[e].indegrees + 1;
} }
} });
} });
} }



populateIndegreesAndUnprocessed();
processList();
return processed;
} }



if(module){ if(module){
module.exports.topologicalSort = topologicalSort; module.exports.topologicalSort = topologicalSort;
} }

0 comments on commit 85677c9

Please sign in to comment.