From 651c3f7c944abeeee188325e4ec83ea76372c510 Mon Sep 17 00:00:00 2001 From: WayneCui Date: Fri, 15 Mar 2019 21:04:35 +0800 Subject: [PATCH] FEAT: add dsf by JS --- javascript/43_topological_sorting/dsf.js | 91 ++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 javascript/43_topological_sorting/dsf.js diff --git a/javascript/43_topological_sorting/dsf.js b/javascript/43_topological_sorting/dsf.js new file mode 100644 index 00000000..0f9016b1 --- /dev/null +++ b/javascript/43_topological_sorting/dsf.js @@ -0,0 +1,91 @@ + +function Graph() { + var graph = { + adj: new Map(), + addEdge: function (from, to){ + if(!this.adj.get(from)) { + this.adj.set(from, [ to ]); + } else { + this.adj.get(from).push(to); + } + }, + sortingByDsf: function(){ + var inverseAdj = new Map(); + var keys = this.adj.keys(); + for(let key of keys) { + let blk = this.adj.get(key); + if(blk) { + for(let v of blk) { + if(!inverseAdj.get(v)) { + inverseAdj.set(v, [key]); + } else { + inverseAdj.get(v).push(key); + } + } + } + } + + let inKeys = inverseAdj.keys(); + let vertexes = new Set([...keys, ...inKeys]); + let visited = []; + for(let vertex of vertexes) { + if(!visited.includes(vertex)) { + visited.push(vertex); + this.dsf(vertex, inverseAdj, visited); + } + } + }, + dsf: function(vertex, inverseAdj, visited) { + if(!inverseAdj.get(vertex)) { + inverseAdj.set(vertex, []); + } + + for(let v of inverseAdj.get(vertex)) { + if(visited.includes(v)) { + continue; + } + + visited.push(v); + + this.dsf(v, inverseAdj, visited); + } + + console.log("->" + vertex); + } + } + + return graph; +} + +var dag = new Graph(); +dag.addEdge(2, 1); +dag.addEdge(3, 2); +dag.addEdge(2, 4); +dag.addEdge(4, 1); +dag.sortingByDsf(); + + +var dag2 = new Graph(); +dag2.addEdge("main", "parse_options"); +dag2.addEdge("main", "tail_file"); +dag2.addEdge("main", "tail_forever"); +dag2.addEdge("tail_file", "pretty_name"); +dag2.addEdge("tail_file", "write_header"); +dag2.addEdge("tail_file", "tail"); +dag2.addEdge("tail_forever", "recheck"); +dag2.addEdge("tail_forever", "pretty_name"); +dag2.addEdge("tail_forever", "write_header"); +dag2.addEdge("tail_forever", "dump_remainder"); +dag2.addEdge("tail", "tail_lines"); +dag2.addEdge("tail", "tail_bytes"); +dag2.addEdge("tail_lines", "start_lines"); +dag2.addEdge("tail_lines", "dump_remainder"); +dag2.addEdge("tail_lines", "file_lines"); +dag2.addEdge("tail_lines", "pipe_lines"); +dag2.addEdge("tail_bytes", "xlseek"); +dag2.addEdge("tail_bytes", "start_bytes"); +dag2.addEdge("tail_bytes", "dump_remainder"); +dag2.addEdge("tail_bytes", "pipe_bytes"); +dag2.addEdge("file_lines", "dump_remainder"); +dag2.addEdge("recheck", "pretty_name"); +dag2.sortingByDsf();