forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
1 changed file
with
91 additions
and
0 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 | Diff line number | Diff line change |
---|---|---|
@@ -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(); |