Skip to content

Topological Sort - Kahn's Algorithm #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"bfs": "BFS",
"dijkstra": "Dijkstra",
"bellman_ford": "Bellman-Ford",
"floyd_warshall": "Floyd-Warshall"
"floyd_warshall": "Floyd-Warshall",
"topological_sort": "Topological-Sort"
}
},
"search": {
Expand Down
21 changes: 21 additions & 0 deletions algorithm/graph_search/topological_sort/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Topological-Sort": "Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG. NOTE: when the graph is represented as an Adjacency Matrix, the Calculation of in-degree Array becomes O(|V|<sup>2</sup>)",
"Applications": [
"Job Scheduling",
"Instruction Scheduling",
"Logic Synthesis",
"Determining the order of compilation tasks to perform in makefiles",
"Data serialization"
],
"Complexity": {
"time": "worst O(|V|+|E|)",
"space": "worst O(|V|)"
},
"References": [
"<a href='http://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/'>GeeksForGeeks</a>",
"<a href='http://www.geeksforgeeks.org/topological-sorting/'>GeeksForGeeks</a>"
],
"files": {
"kahn_algorithm": "Performing Topological Sort using Queue Data Structure & an array of In-degrees"
}
}
59 changes: 59 additions & 0 deletions algorithm/graph_search/topological_sort/kahn_algorithm/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
tracer._pace(100);
tracer._sleep(1000);

(function topologicalSort () {
var inDegrees = Array.apply (null, Array (G.length)).map (Number.prototype.valueOf, 0); //create an Array of G.length number of 0s
var Q = [], iter = 0, i;

tracer._print ('Calculating in-degrees for each Node...');
for (var currNode = 0; currNode < G.length; currNode++) {
for (var currNodeNeighbor = 0; currNodeNeighbor < G.length; currNodeNeighbor++) {
if (G [currNode] [currNodeNeighbor]) {
tracer._print (currNodeNeighbor + ' has an incoming edge from ' + currNode);
tracer._visit (currNodeNeighbor, currNode);
inDegrees [currNodeNeighbor]++;
tracer._leave (currNodeNeighbor, currNode);
}
}
}
tracer._print ('Done. In-Degrees are: [ ' + String (inDegrees) + ' ]');
tracer._print ('');

tracer._print ('Initializing queue with all the sources (nodes with no incoming edges)');
inDegrees.map (function (indegrees, node) {
tracer._visit (node);
if (!indegrees) {
tracer._print (node + ' is a source');
Q.push (node);
}
tracer._leave (node);
});
tracer._print ('Done. Initial State of Queue: [ ' + String (Q) + ' ]');
tracer._print ('');

//begin topological sort (kahn)
while (Q.length > 0) {
tracer._print ('Iteration #' + iter + '. Queue state: [ ' + String (Q) + ' ]');
currNode = Q.shift (1);
tracer._visit (currNode);

for (i = 0; i < G.length; i++) {
if (G [currNode] [i]) {
tracer._print (i + ' has an incoming edge from ' + currNode + '. Decrementing ' + i + '\'s in-degree by 1.');
tracer._visit (i, currNode);
inDegrees [i]--;
tracer._leave (i, currNode);

if (!inDegrees [i]) {
tracer._print (i + '\'s in-degree is now 0. Enqueuing ' + i);
Q.push (i);
}
}
}
tracer._leave (currNode);
tracer._print ('In-degrees are: [' + String (inDegrees) + ' ]');
tracer._print ('-------------------------------------------------------------------');

iter++;
}
}) ();
12 changes: 12 additions & 0 deletions algorithm/graph_search/topological_sort/kahn_algorithm/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var tracer = new DirectedGraphTracer ();
// G[i][j] indicates whether the path from the i-th node to the j-th node exists or not. NOTE: The graph must be Directed-Acyclic
var G = [
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
[1, 1, 0, 0, 0, 0]
];

tracer._setData (G);