Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7942608
commit 85677c9
Showing
1 changed file
with
23 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | ||
} | } |