Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54789ac

Browse files
author
zhengwei.hzw
committed
树和多点可达
1 parent 38e42bb commit 54789ac

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

src/Digraph/directedDFS.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
class DirectedDFS {
4+
constructor(G, s) {
5+
this._marked = []
6+
//单点可达
7+
if (!Array.isArray(s))
8+
this._dfs(G, s)
9+
// 多点可达
10+
else {
11+
s.forEach(w => {
12+
if (!this.marked(w))
13+
this._dfs(G, w)
14+
})
15+
}
16+
}
17+
18+
_dfs(G, v) {
19+
if(!G.isV(v))
20+
throw new Error('别犯傻, 这个顶点都不存在')
21+
22+
this._marked.push(v)
23+
24+
~~(G.adj(v) || []).forEach(w => {
25+
if (!this.marked(w))
26+
this._dfs(G, w)
27+
})
28+
29+
}
30+
31+
marked(v) {
32+
return this._marked.includes(v)
33+
}
34+
35+
}
36+
37+
module.exports = DirectedDFS

src/Digraph/index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict'
2+
3+
class Digraph {
4+
constructor() {
5+
this._E = 0
6+
this._V = []
7+
// 如果用对象的形式没法区分以 '1' 和 1 做 key
8+
this._adjK = []
9+
this._adjV = []
10+
}
11+
12+
isV(v) {
13+
return this._V.includes(v)
14+
}
15+
16+
V() {
17+
return Object.keys(this._V).length
18+
}
19+
20+
E() {
21+
return this._E
22+
}
23+
24+
25+
addEdge(v, w) {
26+
27+
let index = this._adjK.indexOf(v)
28+
29+
if (index > -1) {
30+
this._adjV[index].push(w)
31+
} else {
32+
this._adjK.push(v)
33+
this._adjV.push([w])
34+
}
35+
36+
if (!this._V.includes(v))
37+
this._V.push(v)
38+
if (!this._V.includes(w))
39+
this._V.push(w)
40+
41+
this._E++
42+
}
43+
44+
/**
45+
* 由 v 指出的边所连接的所有顶点
46+
* @param v
47+
* @returns {*}
48+
*/
49+
adj(v) {
50+
let index = this._adjK.indexOf(v)
51+
52+
if (index > -1) {
53+
return this._adjV[index]
54+
} else {
55+
return void 0
56+
}
57+
58+
}
59+
60+
reverse() {
61+
let R = new Digraph()
62+
63+
this._adjV.forEach((ws, index) => {
64+
ws.forEach(w => R.addEdge(w, this._adjK(index)))
65+
})
66+
67+
return R
68+
}
69+
}
70+
71+
module.exports = Digraph

src/Digraph/test-case.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict'
2+
3+
4+
let assert = require('assert')
5+
, Digraph = require('./index')
6+
, DirectedDFS = require('./directedDFS')
7+
8+
, testData = [['4', '2']
9+
, ['2', '3']
10+
, ['3', '2']
11+
, ['6', '0']
12+
, ['0', '1']
13+
, ['2', '0']
14+
, ['11', '12']
15+
, ['12', '9']
16+
, ['9', '10']
17+
, ['9', '11']
18+
, ['8', '9']
19+
, ['10', '12']
20+
, ['11', '4']
21+
, ['4', '3']
22+
, ['3', '5']
23+
, ['7', '8']
24+
, ['8', '7']
25+
, ['5', '4']
26+
, ['0', '5']
27+
, ['6', '4']
28+
, ['6', '9']
29+
, ['7', '6']]
30+
31+
// 测试单点可达
32+
function test1() {
33+
let G = new Digraph()
34+
35+
testData.forEach(d => G.addEdge(d[0], d[1]))
36+
assert.deepStrictEqual(G.V(), 13)
37+
assert.deepStrictEqual(G.E(), 22)
38+
39+
let D1 = new DirectedDFS(G, '1')
40+
, D2 = new DirectedDFS(G, '2')
41+
42+
assert.deepStrictEqual(D1.marked('1'), true)
43+
assert.deepStrictEqual(D1.marked(1), false)
44+
45+
~~['0', '1', '2', '3', '4', '5'].forEach(v => {
46+
assert.deepStrictEqual(D2.marked(v), true)
47+
})
48+
49+
50+
}
51+
52+
53+
// 测试多点可达
54+
function test2() {
55+
56+
let G = new Digraph()
57+
58+
testData.forEach(d => G.addEdge(d[0], d[1]))
59+
60+
let D = new DirectedDFS(G, ['1', '2', '6'])
61+
62+
~~['0', '1', '2', '3', '4', '5', '6', '9', '10', '11', '12'].forEach(v => {
63+
assert.deepStrictEqual(D.marked(v), true)
64+
})
65+
}
66+
67+
68+
test1()
69+
test2()
70+
71+
console.log('如果看到这里,恭喜你,测试全过了')

0 commit comments

Comments
 (0)