-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDigraph.js
More file actions
70 lines (69 loc) · 1.48 KB
/
Digraph.js
File metadata and controls
70 lines (69 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Digraph {
constructor(V = 0) {
this.V = V
this.E = 0
this.indeg = new Array(V).fill(0)
this.values = []
this.adj = new Array(V)
for (let v = 0; v < V; v++) {
this.adj[v] = []
}
}
addEdge(v, w) {
// Filter out parallel edges.
if (v !== w) {
// If objects were passed, convert them to value cache index numbers
// that will also serve as vertex numbers.
if (typeof v !== 'number') {
v = this.valueIndex(v)
}
if (typeof w !== 'number') {
w = this.valueIndex(w)
}
// Filter out self edges.
if (this.adj[v].includes(w) === false) {
this.adj[v].unshift(w)
this.indeg[w]++
this.E++
}
}
}
indegree(v) {
return this.indeg[v]
}
outdegree(v) {
return this.adj[v].length
}
toString() {
let buf = ''
buf += `${this.V} vertices, ${this.E} edges\n`
for (let v = 0; v < this.V; v++) {
buf += `${v}: `
for (let w of this.adj[v]) {
buf += `${w} `
}
buf += '\n'
}
return buf
}
reverse() {
let reverse = new Digraph(this.V)
for (let v = 0; v < this.V; v++) {
for (let w of this.adj[v]) {
reverse.addEdge(w, v)
}
}
return reverse
}
valueIndex(x) {
let i = this.values.indexOf(x)
if (i < 0) {
// Add a vertex associated with the value.
i = this.values.push(x) - 1
this.V++
this.adj[i] = []
}
return i
}
}
module.exports = Digraph