From 2cbe365f928f4fcb830881b1f1cf2ae928dbb0bc Mon Sep 17 00:00:00 2001 From: "duaraghav8@gmail" Date: Mon, 23 May 2016 16:31:18 +0530 Subject: [PATCH] Topological sort - Kahn's Algorithm --- algorithm/category.json | 3 +- .../graph_search/topological_sort/desc.json | 21 +++++++ .../topological_sort/kahn_algorithm/code.js | 59 +++++++++++++++++++ .../topological_sort/kahn_algorithm/data.js | 12 ++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 algorithm/graph_search/topological_sort/desc.json create mode 100644 algorithm/graph_search/topological_sort/kahn_algorithm/code.js create mode 100644 algorithm/graph_search/topological_sort/kahn_algorithm/data.js diff --git a/algorithm/category.json b/algorithm/category.json index 21d7d53a..528e8276 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -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": { diff --git a/algorithm/graph_search/topological_sort/desc.json b/algorithm/graph_search/topological_sort/desc.json new file mode 100644 index 00000000..591eb6be --- /dev/null +++ b/algorithm/graph_search/topological_sort/desc.json @@ -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|2)", + "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": [ + "GeeksForGeeks", + "GeeksForGeeks" + ], + "files": { + "kahn_algorithm": "Performing Topological Sort using Queue Data Structure & an array of In-degrees" + } +} diff --git a/algorithm/graph_search/topological_sort/kahn_algorithm/code.js b/algorithm/graph_search/topological_sort/kahn_algorithm/code.js new file mode 100644 index 00000000..e21a1a2c --- /dev/null +++ b/algorithm/graph_search/topological_sort/kahn_algorithm/code.js @@ -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++; + } +}) (); \ No newline at end of file diff --git a/algorithm/graph_search/topological_sort/kahn_algorithm/data.js b/algorithm/graph_search/topological_sort/kahn_algorithm/data.js new file mode 100644 index 00000000..5e4cdc79 --- /dev/null +++ b/algorithm/graph_search/topological_sort/kahn_algorithm/data.js @@ -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); \ No newline at end of file