08-27-周三_17-09-29
This commit is contained in:
27
node_modules/graphlibrary/lib/alg/components.js
generated
vendored
Normal file
27
node_modules/graphlibrary/lib/alg/components.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
var _ = require('../lodash')
|
||||
|
||||
module.exports = components
|
||||
|
||||
function components (g) {
|
||||
const visited = {}
|
||||
const cmpts = []
|
||||
let cmpt
|
||||
|
||||
function dfs (v) {
|
||||
if (_.has(visited, v)) return
|
||||
visited[v] = true
|
||||
cmpt.push(v)
|
||||
_.each(g.successors(v), dfs)
|
||||
_.each(g.predecessors(v), dfs)
|
||||
}
|
||||
|
||||
_.each(g.nodes(), function (v) {
|
||||
cmpt = []
|
||||
dfs(v)
|
||||
if (cmpt.length) {
|
||||
cmpts.push(cmpt)
|
||||
}
|
||||
})
|
||||
|
||||
return cmpts
|
||||
}
|
Reference in New Issue
Block a user